ViaHeaderValueTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // ViaHeaderValueTest.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 ViaHeaderValueTest
  37. {
  38. [Test]
  39. public void Ctor_InvalidArguments ()
  40. {
  41. try {
  42. new ViaHeaderValue (null, "rb");
  43. Assert.Fail ("#1");
  44. } catch (ArgumentException) {
  45. }
  46. try {
  47. new ViaHeaderValue (" ", null);
  48. Assert.Fail ("#2");
  49. } catch (FormatException) {
  50. }
  51. try {
  52. new ViaHeaderValue ("a", null);
  53. Assert.Fail ("#3");
  54. } catch (ArgumentException) {
  55. }
  56. try {
  57. new ViaHeaderValue ("a", "b", "::");
  58. Assert.Fail ("#4");
  59. } catch (FormatException) {
  60. }
  61. try {
  62. new ViaHeaderValue ("a", "b", null, "(aaa");
  63. Assert.Fail ("#5");
  64. } catch (FormatException) {
  65. }
  66. }
  67. [Test]
  68. public void Ctor_ValidArguments ()
  69. {
  70. new ViaHeaderValue ("a", "b", null);
  71. new ViaHeaderValue ("a", "b", null, null);
  72. }
  73. [Test]
  74. public void Equals ()
  75. {
  76. var value = new ViaHeaderValue ("ab", "x");
  77. Assert.AreEqual (value, new ViaHeaderValue ("ab", "x"), "#1");
  78. Assert.AreEqual (value, new ViaHeaderValue ("AB", "X"), "#2");
  79. Assert.AreNotEqual (value, new ViaHeaderValue ("AA", "x"), "#3");
  80. value = new ViaHeaderValue ("ab", "DD", "cc");
  81. Assert.AreEqual (value, new ViaHeaderValue ("Ab", "DD", "cC"), "#4");
  82. Assert.AreNotEqual (value, new ViaHeaderValue ("AB", "DD"), "#5");
  83. Assert.AreNotEqual (value, new ViaHeaderValue ("Ab", "dd", "cc", "(c)"), "#6");
  84. }
  85. [Test]
  86. public void Parse ()
  87. {
  88. var res = ViaHeaderValue.Parse ("1.1 nowhere.com");
  89. Assert.IsNull (res.ProtocolName, "#1");
  90. Assert.AreEqual ("nowhere.com", res.ReceivedBy, "#2");
  91. Assert.AreEqual ("1.1", res.ProtocolVersion, "#3");
  92. Assert.AreEqual ("1.1 nowhere.com", res.ToString (), "#4");
  93. res = ViaHeaderValue.Parse ("foo / 1.1 nowhere.com:43 ( lalala ) ");
  94. Assert.AreEqual ("foo", res.ProtocolName, "#10");
  95. Assert.AreEqual ("1.1", res.ProtocolVersion, "#11");
  96. Assert.AreEqual ("nowhere.com:43", res.ReceivedBy, "#12");
  97. Assert.AreEqual ("( lalala )", res.Comment, "#13");
  98. Assert.AreEqual ("foo/1.1 nowhere.com:43 ( lalala )", res.ToString (), "#14");
  99. }
  100. [Test]
  101. public void Parse_Invalid ()
  102. {
  103. try {
  104. ViaHeaderValue.Parse (null);
  105. Assert.Fail ("#1");
  106. } catch (FormatException) {
  107. }
  108. try {
  109. ViaHeaderValue.Parse (" ");
  110. Assert.Fail ("#2");
  111. } catch (FormatException) {
  112. }
  113. try {
  114. ViaHeaderValue.Parse ("a");
  115. Assert.Fail ("#3");
  116. } catch (FormatException) {
  117. }
  118. try {
  119. ViaHeaderValue.Parse ("1 nowhere.com :43");
  120. Assert.Fail ("#4");
  121. } catch (FormatException) {
  122. }
  123. }
  124. [Test]
  125. public void Properties ()
  126. {
  127. var value = new ViaHeaderValue ("s", "p");
  128. Assert.IsNull (value.ProtocolName, "#1");
  129. Assert.AreEqual ("s", value.ProtocolVersion, "#2");
  130. Assert.AreEqual ("p", value.ReceivedBy, "#3");
  131. value = new ViaHeaderValue ("s", "rb", "name");
  132. Assert.AreEqual ("name", value.ProtocolName, "#4");
  133. Assert.AreEqual ("s", value.ProtocolVersion, "#5");
  134. Assert.AreEqual ("rb", value.ReceivedBy, "#6");
  135. value = new ViaHeaderValue ("s", "rb", "name", "(cmt)");
  136. Assert.AreEqual ("name", value.ProtocolName, "#7");
  137. Assert.AreEqual ("s", value.ProtocolVersion, "#8");
  138. Assert.AreEqual ("rb", value.ReceivedBy, "#9");
  139. Assert.AreEqual ("(cmt)", value.Comment, "#10");
  140. }
  141. [Test]
  142. public void TryParse ()
  143. {
  144. ViaHeaderValue res;
  145. Assert.IsTrue (ViaHeaderValue.TryParse ("a b", out res), "#1");
  146. Assert.AreEqual ("b", res.ReceivedBy, "#2");
  147. Assert.AreEqual ("a", res.ProtocolVersion, "#3");
  148. Assert.IsNull (res.Comment, "#4");
  149. Assert.IsNull (res.ProtocolName, "#5");
  150. }
  151. [Test]
  152. public void TryParse_Invalid ()
  153. {
  154. ViaHeaderValue res;
  155. Assert.IsFalse (ViaHeaderValue.TryParse ("", out res), "#1");
  156. Assert.IsNull (res, "#2");
  157. }
  158. }
  159. }