2
0

ProductInfoHeaderValueTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // ProductInfoHeaderValueTest.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. namespace MonoTests.System.Net.Http.Headers
  34. {
  35. [TestFixture]
  36. public class ProductInfoHeaderValueTest
  37. {
  38. [Test]
  39. public void Ctor_InvalidArguments ()
  40. {
  41. try {
  42. new ProductInfoHeaderValue (null as string);
  43. Assert.Fail ("#1");
  44. } catch (ArgumentException) {
  45. }
  46. try {
  47. new ProductInfoHeaderValue (" ", null);
  48. Assert.Fail ("#2");
  49. } catch (FormatException) {
  50. }
  51. try {
  52. new ProductInfoHeaderValue (null as ProductHeaderValue);
  53. Assert.Fail ("#3");
  54. } catch (ArgumentException) {
  55. }
  56. }
  57. [Test]
  58. public void Equals ()
  59. {
  60. var value = new ProductInfoHeaderValue ("(ab)");
  61. Assert.AreEqual (value, new ProductInfoHeaderValue ("(ab)"), "#1");
  62. Assert.AreNotEqual (value, new ProductInfoHeaderValue ("(AB)"), "#2");
  63. Assert.AreNotEqual (value, new ProductInfoHeaderValue ("(AA)"), "#3");
  64. value = new ProductInfoHeaderValue ("ab", "DD");
  65. Assert.AreEqual (value, new ProductInfoHeaderValue ("Ab", "DD"), "#4");
  66. Assert.AreNotEqual (value, new ProductInfoHeaderValue ("(AB)"), "#5");
  67. Assert.AreEqual (value, new ProductInfoHeaderValue ("Ab", "dd"), "#6");
  68. }
  69. [Test]
  70. public void Parse ()
  71. {
  72. var res = ProductInfoHeaderValue.Parse ("c");
  73. Assert.AreEqual ("c", res.Product.Name, "#1");
  74. Assert.IsNull (res.Product.Version, "#2");
  75. Assert.IsNull (res.Comment, "#3");
  76. Assert.AreEqual ("c", res.ToString (), "#4");
  77. res = ProductInfoHeaderValue.Parse (" b / 6");
  78. Assert.AreEqual ("b", res.Product.Name, "#11");
  79. Assert.AreEqual ("6", res.Product.Version, "#12");
  80. Assert.IsNull (res.Comment, "#13");
  81. Assert.AreEqual ("b/6", res.ToString (), "#14");
  82. res = ProductInfoHeaderValue.Parse (" ( cccc ) ");
  83. Assert.IsNull (res.Product, "#21");
  84. Assert.AreEqual ("( cccc )", res.Comment, "#22");
  85. Assert.AreEqual ("( cccc )", res.ToString (), "#23");
  86. }
  87. [Test]
  88. public void Parse_Invalid ()
  89. {
  90. try {
  91. ProductInfoHeaderValue.Parse (null);
  92. Assert.Fail ("#1");
  93. } catch (FormatException) {
  94. }
  95. try {
  96. ProductInfoHeaderValue.Parse (" ");
  97. Assert.Fail ("#2");
  98. } catch (FormatException) {
  99. }
  100. try {
  101. ProductInfoHeaderValue.Parse ("a;b");
  102. Assert.Fail ("#3");
  103. } catch (FormatException) {
  104. }
  105. }
  106. [Test]
  107. public void Properties ()
  108. {
  109. var value = new ProductInfoHeaderValue ("s", "p");
  110. Assert.AreEqual ("s", value.Product.Name, "#1");
  111. Assert.AreEqual ("p", value.Product.Version, "#2");
  112. Assert.IsNull (value.Comment, "#3");
  113. value = new ProductInfoHeaderValue ("(s)");
  114. Assert.AreEqual ("(s)", value.Comment, "#4");
  115. Assert.IsNull (value.Product, "#5");
  116. }
  117. [Test]
  118. public void TryParse ()
  119. {
  120. ProductInfoHeaderValue res;
  121. Assert.IsTrue (ProductInfoHeaderValue.TryParse ("a", out res), "#1");
  122. Assert.AreEqual ("a", res.Product.Name, "#2");
  123. Assert.IsNull (res.Comment, "#3");
  124. }
  125. [Test]
  126. public void TryParse_Invalid ()
  127. {
  128. ProductInfoHeaderValue res;
  129. Assert.IsFalse (ProductInfoHeaderValue.TryParse ("", out res), "#1");
  130. Assert.IsNull (res, "#2");
  131. }
  132. }
  133. }