WebHeaderCollectionTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // WebHeaderCollectionTest.cs - NUnit Test Cases for System.Net.WebHeaderCollection
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. using NUnit.Framework;
  11. using System;
  12. using System.Net;
  13. using System.Collections;
  14. namespace MonoTests.System.Net
  15. {
  16. [TestFixture]
  17. public class WebHeaderCollectionTest
  18. {
  19. WebHeaderCollection col;
  20. [SetUp]
  21. public void GetReady ()
  22. {
  23. col = new WebHeaderCollection ();
  24. col.Add ("Name1: Value1");
  25. col.Add ("Name2: Value2");
  26. }
  27. [Test]
  28. public void Add ()
  29. {
  30. try {
  31. col.Add (null);
  32. Assertion.Fail ("#1");
  33. } catch (ArgumentNullException) {}
  34. try {
  35. col.Add ("");
  36. Assertion.Fail ("#2");
  37. } catch (ArgumentException) {}
  38. try {
  39. col.Add (" ");
  40. Assertion.Fail ("#3");
  41. } catch (ArgumentException) {}
  42. try {
  43. col.Add (":");
  44. Assertion.Fail ("#4");
  45. } catch (ArgumentException) {}
  46. try {
  47. col.Add (" : ");
  48. Assertion.Fail ("#5");
  49. } catch (ArgumentException) {}
  50. try {
  51. col.Add ("XHost: foo");
  52. } catch (ArgumentException) {
  53. Assertion.Fail ("#7");
  54. }
  55. // invalid values
  56. try {
  57. col.Add ("XHost" + ((char) 0xa9) + ": foo");
  58. Assertion.Fail ("#8");
  59. } catch (ArgumentException) {}
  60. try {
  61. col.Add ("XHost: foo" + (char) 0xa9);
  62. } catch (ArgumentException) {
  63. Assertion.Fail ("#9");
  64. }
  65. try {
  66. col.Add ("XHost: foo" + (char) 0x7f);
  67. Assertion.Fail ("#10");
  68. } catch (ArgumentException) {
  69. }
  70. try {
  71. col.Add ("XHost", null);
  72. } catch (ArgumentException) {
  73. Assertion.Fail ("#11");
  74. }
  75. try {
  76. col.Add ("XHost:");
  77. } catch (ArgumentException) {
  78. Assertion.Fail ("#12");
  79. }
  80. // restricted
  81. /*
  82. // this can only be tested in namespace System.Net
  83. try {
  84. WebHeaderCollection col2 = new WebHeaderCollection (true);
  85. col2.Add ("Host: foo");
  86. Assertion.Fail ("#13: should fail according to spec");
  87. } catch (ArgumentException) {}
  88. */
  89. }
  90. [Test]
  91. public void GetValues ()
  92. {
  93. WebHeaderCollection w = new WebHeaderCollection ();
  94. w.Add ("Hello", "H1");
  95. w.Add ("Hello", "H2");
  96. w.Add ("Hello", "H3,H4");
  97. string [] sa = w.GetValues ("Hello");
  98. Assertion.AssertEquals ("#1", 3, sa.Length);
  99. Assertion.AssertEquals ("#2", "H1,H2,H3,H4", w.Get ("Hello"));
  100. w = new WebHeaderCollection ();
  101. w.Add ("Accept", "H1");
  102. w.Add ("Accept", "H2");
  103. w.Add ("Accept", "H3,H4");
  104. Assertion.AssertEquals ("#3a", 3, w.GetValues (0).Length);
  105. Assertion.AssertEquals ("#3b", 4, w.GetValues ("Accept").Length);
  106. Assertion.AssertEquals ("#4", "H1,H2,H3,H4", w.Get ("Accept"));
  107. w = new WebHeaderCollection ();
  108. w.Add ("Allow", "H1");
  109. w.Add ("Allow", "H2");
  110. w.Add ("Allow", "H3,H4");
  111. sa = w.GetValues ("Allow");
  112. Assertion.AssertEquals ("#5", 4, sa.Length);
  113. Assertion.AssertEquals ("#6", "H1,H2,H3,H4", w.Get ("Allow"));
  114. w = new WebHeaderCollection ();
  115. w.Add ("AUTHorization", "H1, H2, H3");
  116. sa = w.GetValues ("authorization");
  117. Assertion.AssertEquals ("#9", 3, sa.Length);
  118. w = new WebHeaderCollection ();
  119. w.Add ("proxy-authenticate", "H1, H2, H3");
  120. sa = w.GetValues ("Proxy-Authenticate");
  121. Assertion.AssertEquals ("#9", 3, sa.Length);
  122. w = new WebHeaderCollection ();
  123. w.Add ("expect", "H1,\tH2, H3 ");
  124. sa = w.GetValues ("EXPECT");
  125. Assertion.AssertEquals ("#10", 3, sa.Length);
  126. Assertion.AssertEquals ("#11", "H2", sa [1]);
  127. Assertion.AssertEquals ("#12", "H3", sa [2]);
  128. try {
  129. w.GetValues (null);
  130. Assertion.Fail ("#13");
  131. } catch (ArgumentNullException) {}
  132. Assertion.AssertEquals ("#14", null, w.GetValues (""));
  133. Assertion.AssertEquals ("#15", null, w.GetValues ("NotExistent"));
  134. }
  135. [Test]
  136. public void Indexers ()
  137. {
  138. Assertion.AssertEquals ("#1", "Value1", col [0]);
  139. Assertion.AssertEquals ("#2", "Value1", col ["Name1"]);
  140. Assertion.AssertEquals ("#3", "Value1", col ["NAME1"]);
  141. }
  142. [Test]
  143. public void Remove ()
  144. {
  145. col.Remove ("Name1");
  146. col.Remove ("NameNotExist");
  147. Assertion.AssertEquals ("#1", 1, col.Count);
  148. /*
  149. // this can only be tested in namespace System.Net
  150. try {
  151. WebHeaderCollection col2 = new WebHeaderCollection (true);
  152. col2.Add ("Host", "foo");
  153. col2.Remove ("Host");
  154. Assertion.Fail ("#2: should fail according to spec");
  155. } catch (ArgumentException) {}
  156. */
  157. }
  158. [Test]
  159. public void Set ()
  160. {
  161. col.Add ("Name1", "Value1b");
  162. col.Set ("Name1", "\t X \t");
  163. Assertion.AssertEquals ("#1", "X", col.Get ("Name1"));
  164. }
  165. [Test]
  166. public void IsRestricted ()
  167. {
  168. Assertion.Assert ("#1", !WebHeaderCollection.IsRestricted ("Xhost"));
  169. Assertion.Assert ("#2", WebHeaderCollection.IsRestricted ("Host"));
  170. Assertion.Assert ("#3", WebHeaderCollection.IsRestricted ("HOST"));
  171. Assertion.Assert ("#4", WebHeaderCollection.IsRestricted ("Transfer-Encoding"));
  172. Assertion.Assert ("#5", WebHeaderCollection.IsRestricted ("user-agent"));
  173. Assertion.Assert ("#6", WebHeaderCollection.IsRestricted ("accept"));
  174. Assertion.Assert ("#7", !WebHeaderCollection.IsRestricted ("accept-charset"));
  175. }
  176. [Test]
  177. public void ToStringTest ()
  178. {
  179. col.Add ("Name1", "Value1b");
  180. col.Add ("Name3", "Value3a\r\n Value3b");
  181. col.Add ("Name4", " Value4 ");
  182. Assertion.AssertEquals ("#1", "Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString ());
  183. }
  184. }
  185. }