WebHeaderCollectionTest.cs 5.4 KB

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