NameValueWithParametersHeaderValueTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // NameValueWithParametersHeaderValueTest.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using NUnit.Framework;
  32. using System.Net.Http.Headers;
  33. using System.Linq;
  34. namespace MonoTests.System.Net.Http.Headers
  35. {
  36. [TestFixture]
  37. public class NameValueWithParametersHeaderValueTest
  38. {
  39. [Test]
  40. public void Ctor_InvalidArguments ()
  41. {
  42. try {
  43. new NameValueWithParametersHeaderValue (null);
  44. Assert.Fail ("#1");
  45. } catch (ArgumentException) {
  46. }
  47. try {
  48. new NameValueHeaderValue (" ", null);
  49. Assert.Fail ("#2");
  50. } catch (FormatException) {
  51. }
  52. }
  53. [Test]
  54. public void Equals ()
  55. {
  56. var value = new NameValueWithParametersHeaderValue ("ab");
  57. Assert.AreEqual (value, new NameValueWithParametersHeaderValue ("ab"), "#1");
  58. Assert.AreEqual (value, new NameValueWithParametersHeaderValue ("AB"), "#2");
  59. Assert.AreNotEqual (value, new NameValueWithParametersHeaderValue ("AA"), "#3");
  60. var second = new NameValueWithParametersHeaderValue ("AB");
  61. second.Parameters.Add (new NameValueHeaderValue ("pv"));
  62. Assert.AreNotEqual (value, second, "#4");
  63. value.Parameters.Add (new NameValueHeaderValue ("pv"));
  64. Assert.AreEqual (value, second, "#5");
  65. }
  66. [Test]
  67. public void Parse ()
  68. {
  69. var res = NameValueWithParametersHeaderValue.Parse ("c");
  70. Assert.AreEqual ("c", res.Name, "#1");
  71. Assert.AreEqual ("c", res.ToString (), "#1b");
  72. Assert.IsNull (res.Value, "#2");
  73. res = NameValueWithParametersHeaderValue.Parse ("a=2 ; b = 555");
  74. Assert.AreEqual ("a", res.Name, "#3");
  75. Assert.AreEqual ("a=2; b=555", res.ToString (), "#3b");
  76. Assert.AreEqual ("2", res.Value, "#4");
  77. Assert.AreEqual ("b", res.Parameters.First().Name, "#5");
  78. Assert.AreEqual (1, res.Parameters.Count, "#6");
  79. }
  80. [Test]
  81. public void Parse_Invalid ()
  82. {
  83. try {
  84. NameValueWithParametersHeaderValue.Parse (null);
  85. Assert.Fail ("#1");
  86. } catch (FormatException) {
  87. }
  88. try {
  89. NameValueWithParametersHeaderValue.Parse (" ");
  90. Assert.Fail ("#2");
  91. } catch (FormatException) {
  92. }
  93. try {
  94. NameValueWithParametersHeaderValue.Parse ("a=1;");
  95. Assert.Fail ("#3");
  96. } catch (FormatException) {
  97. }
  98. }
  99. [Test]
  100. public void Properties ()
  101. {
  102. var value = new NameValueWithParametersHeaderValue ("s", "p");
  103. Assert.AreEqual ("s", value.Name, "#1");
  104. Assert.AreEqual ("p", value.Value, "#2");
  105. value = new NameValueWithParametersHeaderValue ("s");
  106. Assert.AreEqual ("s", value.Name, "#3");
  107. Assert.IsNull (value.Value, "#4");
  108. value.Value = "bb";
  109. Assert.AreEqual ("bb", value.Value, "#5");
  110. }
  111. [Test]
  112. public void Properties_Invalid ()
  113. {
  114. var value = new NameValueWithParametersHeaderValue ("s");
  115. try {
  116. value.Value = " ";
  117. Assert.Fail ("#1");
  118. } catch (FormatException) {
  119. }
  120. }
  121. [Test]
  122. public void TryParse ()
  123. {
  124. NameValueWithParametersHeaderValue res;
  125. Assert.IsTrue (NameValueWithParametersHeaderValue.TryParse ("a", out res), "#1");
  126. Assert.AreEqual ("a", res.Name, "#2");
  127. Assert.IsNull (res.Value, "#3");
  128. }
  129. [Test]
  130. public void TryParse_Invalid ()
  131. {
  132. NameValueWithParametersHeaderValue res;
  133. Assert.IsFalse (NameValueWithParametersHeaderValue.TryParse ("", out res), "#1");
  134. Assert.IsNull (res, "#2");
  135. }
  136. }
  137. }