WebHeaderCollectionTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // Gert Driesen ([email protected])
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Specialized;
  14. using System.IO;
  15. using System.Net;
  16. using System.Runtime.Serialization;
  17. using System.Runtime.Serialization.Formatters;
  18. using System.Runtime.Serialization.Formatters.Binary;
  19. using NUnit.Framework;
  20. namespace MonoTests.System.Net
  21. {
  22. [TestFixture]
  23. public class WebHeaderCollectionTest
  24. {
  25. WebHeaderCollection col;
  26. [SetUp]
  27. public void GetReady ()
  28. {
  29. col = new WebHeaderCollection ();
  30. col.Add ("Name1: Value1");
  31. col.Add ("Name2: Value2");
  32. }
  33. [Test]
  34. public void Add ()
  35. {
  36. try {
  37. col.Add (null);
  38. Assertion.Fail ("#1");
  39. } catch (ArgumentNullException) { }
  40. try {
  41. col.Add ("");
  42. Assertion.Fail ("#2");
  43. } catch (ArgumentException) { }
  44. try {
  45. col.Add (" ");
  46. Assertion.Fail ("#3");
  47. } catch (ArgumentException) { }
  48. try {
  49. col.Add (":");
  50. Assertion.Fail ("#4");
  51. } catch (ArgumentException) { }
  52. try {
  53. col.Add (" : ");
  54. Assertion.Fail ("#5");
  55. } catch (ArgumentException) { }
  56. try {
  57. col.Add ("XHost: foo");
  58. } catch (ArgumentException) {
  59. Assertion.Fail ("#7");
  60. }
  61. // invalid values
  62. try {
  63. col.Add ("XHost" + ((char) 0xa9) + ": foo");
  64. Assertion.Fail ("#8");
  65. } catch (ArgumentException) { }
  66. try {
  67. col.Add ("XHost: foo" + (char) 0xa9);
  68. } catch (ArgumentException) {
  69. Assertion.Fail ("#9");
  70. }
  71. try {
  72. col.Add ("XHost: foo" + (char) 0x7f);
  73. Assertion.Fail ("#10");
  74. } catch (ArgumentException) {
  75. }
  76. try {
  77. col.Add ("XHost", null);
  78. } catch (ArgumentException) {
  79. Assertion.Fail ("#11");
  80. }
  81. try {
  82. col.Add ("XHost:");
  83. } catch (ArgumentException) {
  84. Assertion.Fail ("#12");
  85. }
  86. // restricted
  87. /*
  88. // this can only be tested in namespace System.Net
  89. try {
  90. WebHeaderCollection col2 = new WebHeaderCollection (true);
  91. col2.Add ("Host: foo");
  92. Assertion.Fail ("#13: should fail according to spec");
  93. } catch (ArgumentException) {}
  94. */
  95. }
  96. [Test]
  97. [Category ("NotWorking")]
  98. public void GetValues ()
  99. {
  100. WebHeaderCollection w = new WebHeaderCollection ();
  101. w.Add ("Hello", "H1");
  102. w.Add ("Hello", "H2");
  103. w.Add ("Hello", "H3,H4");
  104. string [] sa = w.GetValues ("Hello");
  105. Assertion.AssertEquals ("#1", 3, sa.Length);
  106. Assertion.AssertEquals ("#2", "H1,H2,H3,H4", w.Get ("Hello"));
  107. w = new WebHeaderCollection ();
  108. w.Add ("Accept", "H1");
  109. w.Add ("Accept", "H2");
  110. w.Add ("Accept", "H3,H4");
  111. Assertion.AssertEquals ("#3a", 3, w.GetValues (0).Length);
  112. Assertion.AssertEquals ("#3b", 4, w.GetValues ("Accept").Length);
  113. Assertion.AssertEquals ("#4", "H1,H2,H3,H4", w.Get ("Accept"));
  114. w = new WebHeaderCollection ();
  115. w.Add ("Allow", "H1");
  116. w.Add ("Allow", "H2");
  117. w.Add ("Allow", "H3,H4");
  118. sa = w.GetValues ("Allow");
  119. Assertion.AssertEquals ("#5", 4, sa.Length);
  120. Assertion.AssertEquals ("#6", "H1,H2,H3,H4", w.Get ("Allow"));
  121. w = new WebHeaderCollection ();
  122. w.Add ("AUTHorization", "H1, H2, H3");
  123. sa = w.GetValues ("authorization");
  124. Assertion.AssertEquals ("#9", 3, sa.Length);
  125. w = new WebHeaderCollection ();
  126. w.Add ("proxy-authenticate", "H1, H2, H3");
  127. sa = w.GetValues ("Proxy-Authenticate");
  128. Assertion.AssertEquals ("#9", 3, sa.Length);
  129. w = new WebHeaderCollection ();
  130. w.Add ("expect", "H1,\tH2, H3 ");
  131. sa = w.GetValues ("EXPECT");
  132. Assertion.AssertEquals ("#10", 3, sa.Length);
  133. Assertion.AssertEquals ("#11", "H2", sa [1]);
  134. Assertion.AssertEquals ("#12", "H3", sa [2]);
  135. try {
  136. w.GetValues (null);
  137. Assertion.Fail ("#13");
  138. } catch (ArgumentNullException) { }
  139. Assertion.AssertEquals ("#14", null, w.GetValues (""));
  140. Assertion.AssertEquals ("#15", null, w.GetValues ("NotExistent"));
  141. }
  142. [Test]
  143. public void Indexers ()
  144. {
  145. #if NET_2_0
  146. Assertion.AssertEquals ("#1.1", "Value1", ((NameValueCollection)col)[0]);
  147. //FIXME: test also HttpRequestHeader and HttpResponseHeader
  148. #else
  149. Assertion.AssertEquals ("#1", "Value1", col [0]);
  150. #endif
  151. Assertion.AssertEquals ("#2", "Value1", col ["Name1"]);
  152. Assertion.AssertEquals ("#3", "Value1", col ["NAME1"]);
  153. }
  154. [Test]
  155. public void Remove ()
  156. {
  157. col.Remove ("Name1");
  158. col.Remove ("NameNotExist");
  159. Assertion.AssertEquals ("#1", 1, col.Count);
  160. /*
  161. // this can only be tested in namespace System.Net
  162. try {
  163. WebHeaderCollection col2 = new WebHeaderCollection (true);
  164. col2.Add ("Host", "foo");
  165. col2.Remove ("Host");
  166. Assertion.Fail ("#2: should fail according to spec");
  167. } catch (ArgumentException) {}
  168. */
  169. }
  170. [Test]
  171. public void Set ()
  172. {
  173. col.Add ("Name1", "Value1b");
  174. col.Set ("Name1", "\t X \t");
  175. Assertion.AssertEquals ("#1", "X", col.Get ("Name1"));
  176. }
  177. [Test]
  178. public void IsRestricted ()
  179. {
  180. Assertion.Assert ("#1", !WebHeaderCollection.IsRestricted ("Xhost"));
  181. Assertion.Assert ("#2", WebHeaderCollection.IsRestricted ("Host"));
  182. Assertion.Assert ("#3", WebHeaderCollection.IsRestricted ("HOST"));
  183. Assertion.Assert ("#4", WebHeaderCollection.IsRestricted ("Transfer-Encoding"));
  184. Assertion.Assert ("#5", WebHeaderCollection.IsRestricted ("user-agent"));
  185. Assertion.Assert ("#6", WebHeaderCollection.IsRestricted ("accept"));
  186. Assertion.Assert ("#7", !WebHeaderCollection.IsRestricted ("accept-charset"));
  187. }
  188. [Test]
  189. public void ToStringTest ()
  190. {
  191. col.Add ("Name1", "Value1b");
  192. col.Add ("Name3", "Value3a\r\n Value3b");
  193. col.Add ("Name4", " Value4 ");
  194. Assertion.AssertEquals ("#1", "Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString ());
  195. }
  196. [Test]
  197. #if TARGET_JVM
  198. //FIXME: include Java serialization compliant tests - the order of object
  199. // in SerializationInfo should stay same to MS format...
  200. [Ignore ("The MS compliant binary serialization is not supported")]
  201. #endif
  202. public void GetObjectData ()
  203. {
  204. SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
  205. new FormatterConverter ());
  206. WebHeaderCollection headers = new WebHeaderCollection ();
  207. headers.Add ("Content-Type", "image/png");
  208. headers.Add ("No-Cache:off");
  209. headers.Add ("Disposition", "attach");
  210. ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
  211. Assert.AreEqual (7, si.MemberCount, "#A");
  212. int i = 0;
  213. foreach (SerializationEntry entry in si) {
  214. Assert.IsNotNull (entry.Name, "#B1:" + i);
  215. Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
  216. Assert.IsNotNull (entry.Value, "#B3:" + i);
  217. switch (i) {
  218. case 0:
  219. Assert.AreEqual ("Count", entry.Name, "#B4:" + i);
  220. Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
  221. Assert.AreEqual (3, entry.Value, "#B6:" + i);
  222. break;
  223. case 1:
  224. Assert.AreEqual ("0", entry.Name, "#B4:" + i);
  225. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  226. Assert.AreEqual ("Content-Type", entry.Value, "#B6:" + i);
  227. break;
  228. case 2:
  229. Assert.AreEqual ("3", entry.Name, "#B4:" + i);
  230. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  231. Assert.AreEqual ("image/png", entry.Value, "#B6:" + i);
  232. break;
  233. case 3:
  234. Assert.AreEqual ("1", entry.Name, "#B4:" + i);
  235. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  236. Assert.AreEqual ("No-Cache", entry.Value, "#B6:" + i);
  237. break;
  238. case 4:
  239. Assert.AreEqual ("4", entry.Name, "#B4:" + i);
  240. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  241. Assert.AreEqual ("off", entry.Value, "#B6:" + i);
  242. break;
  243. case 5:
  244. Assert.AreEqual ("2", entry.Name, "#B4:" + i);
  245. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  246. Assert.AreEqual ("Disposition", entry.Value, "#B6:" + i);
  247. break;
  248. case 6:
  249. Assert.AreEqual ("5", entry.Name, "#B4:" + i);
  250. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  251. Assert.AreEqual ("attach", entry.Value, "#B6:" + i);
  252. break;
  253. }
  254. i++;
  255. }
  256. }
  257. [Test]
  258. #if TARGET_JVM
  259. //FIXME: include Java serialization compliant tests
  260. [Ignore ("The MS compliant binary serialization is not supported")]
  261. #endif
  262. public void Serialize ()
  263. {
  264. WebHeaderCollection headers = new WebHeaderCollection ();
  265. headers.Add ("Content-Type", "image/png");
  266. headers.Add ("No-Cache:off");
  267. headers.Add ("Disposition", "attach");
  268. BinaryFormatter bf = new BinaryFormatter ();
  269. bf.AssemblyFormat = FormatterAssemblyStyle.Full;
  270. MemoryStream ms = new MemoryStream ();
  271. bf.Serialize (ms, headers);
  272. ms.Position = 0;
  273. byte [] buffer = new byte [ms.Length];
  274. ms.Read (buffer, 0, buffer.Length);
  275. Assert.AreEqual (_serialized, buffer);
  276. }
  277. [Test]
  278. #if TARGET_JVM
  279. //FIXME: include Java serialization compliant tests
  280. [Ignore ("The MS compliant binary serialization format is not supported")]
  281. #endif
  282. public void Deserialize ()
  283. {
  284. MemoryStream ms = new MemoryStream ();
  285. ms.Write (_serialized, 0, _serialized.Length);
  286. ms.Position = 0;
  287. BinaryFormatter bf = new BinaryFormatter ();
  288. WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
  289. }
  290. private static readonly byte [] _serialized = new byte [] {
  291. #if NET_2_0
  292. 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  293. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
  294. 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
  295. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
  296. 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
  297. 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
  298. 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
  299. 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
  300. 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
  301. 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
  302. 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
  303. 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
  304. 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
  305. 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
  306. 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
  307. 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
  308. 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
  309. 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
  310. 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
  311. 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
  312. 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
  313. 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
  314. 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
  315. 0x63, 0x68, 0x0b
  316. #else
  317. 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  318. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
  319. 0x4c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
  320. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31, 0x2e, 0x30, 0x2e, 0x35,
  321. 0x30, 0x30, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
  322. 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
  323. 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
  324. 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
  325. 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
  326. 0x05, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65,
  327. 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65,
  328. 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74,
  329. 0x69, 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75,
  330. 0x6e, 0x74, 0x01, 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01,
  331. 0x32, 0x01, 0x35, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08,
  332. 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00,
  333. 0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
  334. 0x54, 0x79, 0x70, 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69,
  335. 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00,
  336. 0x00, 0x00, 0x08, 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65,
  337. 0x06, 0x06, 0x00, 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07,
  338. 0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69,
  339. 0x74, 0x69, 0x6f, 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61,
  340. 0x74, 0x74, 0x61, 0x63, 0x68, 0x0b
  341. #endif
  342. };
  343. }
  344. }