WebHeaderCollectionTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. // Gonzalo Paniagua Javier ([email protected])
  9. //
  10. // (C) 2003 Martin Willemoes Hansen
  11. //
  12. using System;
  13. using System.Collections;
  14. using System.Collections.Specialized;
  15. using System.IO;
  16. using System.Net;
  17. using System.Runtime.Serialization;
  18. using System.Runtime.Serialization.Formatters;
  19. using System.Runtime.Serialization.Formatters.Binary;
  20. using NUnit.Framework;
  21. namespace MonoTests.System.Net
  22. {
  23. [TestFixture]
  24. public class WebHeaderCollectionTest
  25. {
  26. WebHeaderCollection col;
  27. [SetUp]
  28. public void GetReady ()
  29. {
  30. col = new WebHeaderCollection ();
  31. col.Add ("Name1: Value1");
  32. col.Add ("Name2: Value2");
  33. }
  34. [Test]
  35. public void Add ()
  36. {
  37. try {
  38. col.Add (null);
  39. Assert.Fail ("#1");
  40. } catch (ArgumentNullException) { }
  41. try {
  42. col.Add ("");
  43. Assert.Fail ("#2");
  44. } catch (ArgumentException) { }
  45. try {
  46. col.Add (" ");
  47. Assert.Fail ("#3");
  48. } catch (ArgumentException) { }
  49. try {
  50. col.Add (":");
  51. Assert.Fail ("#4");
  52. } catch (ArgumentException) { }
  53. try {
  54. col.Add (" : ");
  55. Assert.Fail ("#5");
  56. } catch (ArgumentException) { }
  57. try {
  58. col.Add ("XHost: foo");
  59. } catch (ArgumentException) {
  60. Assert.Fail ("#7");
  61. }
  62. // invalid values
  63. try {
  64. col.Add ("XHost" + ((char) 0xa9) + ": foo");
  65. Assert.Fail ("#8");
  66. } catch (ArgumentException) { }
  67. try {
  68. col.Add ("XHost: foo" + (char) 0xa9);
  69. } catch (ArgumentException) {
  70. Assert.Fail ("#9");
  71. }
  72. try {
  73. col.Add ("XHost: foo" + (char) 0x7f);
  74. Assert.Fail ("#10");
  75. } catch (ArgumentException) {
  76. }
  77. try {
  78. col.Add ("XHost", null);
  79. } catch (ArgumentException) {
  80. Assert.Fail ("#11");
  81. }
  82. try {
  83. col.Add ("XHost:");
  84. } catch (ArgumentException) {
  85. Assert.Fail ("#12");
  86. }
  87. // restricted
  88. /*
  89. // this can only be tested in namespace System.Net
  90. try {
  91. WebHeaderCollection col2 = new WebHeaderCollection (true);
  92. col2.Add ("Host: foo");
  93. Assert.Fail ("#13: should fail according to spec");
  94. } catch (ArgumentException) {}
  95. */
  96. }
  97. [Test]
  98. [Category ("NotWorking")]
  99. public void GetValues ()
  100. {
  101. WebHeaderCollection w = new WebHeaderCollection ();
  102. w.Add ("Hello", "H1");
  103. w.Add ("Hello", "H2");
  104. w.Add ("Hello", "H3,H4");
  105. string [] sa = w.GetValues ("Hello");
  106. Assert.AreEqual (3, sa.Length, "#1");
  107. Assert.AreEqual ("H1, H2,H3,H4", w.Get ("Hello"), "#2");
  108. w = new WebHeaderCollection ();
  109. w.Add ("Accept", "H1");
  110. w.Add ("Accept", "H2");
  111. w.Add ("Accept", "H3,H4");
  112. Assert.AreEqual (3, w.GetValues (0).Length, "#3a");
  113. Assert.AreEqual (4, w.GetValues ("Accept").Length, "#3b");
  114. Assert.AreEqual ("H1, H2,H3,H4", w.Get ("Accept"), "#4");
  115. w = new WebHeaderCollection ();
  116. w.Add ("Allow", "H1");
  117. w.Add ("Allow", "H2");
  118. w.Add ("Allow", "H3,H4");
  119. sa = w.GetValues ("Allow");
  120. Assert.AreEqual (4, sa.Length, "#5");
  121. Assert.AreEqual ("H1, H2,H3,H4", w.Get ("Allow"), "#6");
  122. w = new WebHeaderCollection ();
  123. w.Add ("AUTHorization", "H1, H2, H3");
  124. sa = w.GetValues ("authorization");
  125. Assert.AreEqual (3, sa.Length, "#9");
  126. w = new WebHeaderCollection ();
  127. w.Add ("proxy-authenticate", "H1, H2, H3");
  128. sa = w.GetValues ("Proxy-Authenticate");
  129. Assert.AreEqual (3, sa.Length, "#9");
  130. w = new WebHeaderCollection ();
  131. w.Add ("expect", "H1,\tH2, H3 ");
  132. sa = w.GetValues ("EXPECT");
  133. Assert.AreEqual (3, sa.Length, "#10");
  134. Assert.AreEqual ("H2", sa [1], "#11");
  135. Assert.AreEqual ("H3", sa [2], "#12");
  136. try {
  137. w.GetValues (null);
  138. Assert.Fail ("#13");
  139. } catch (ArgumentNullException) { }
  140. Assert.AreEqual (null, w.GetValues (""), "#14");
  141. Assert.AreEqual (null, w.GetValues ("NotExistent"), "#15");
  142. }
  143. [Test]
  144. public void Indexers ()
  145. {
  146. Assert.AreEqual ("Value1", ((NameValueCollection)col)[0], "#1.1");
  147. //FIXME: test also HttpRequestHeader and HttpResponseHeader
  148. //FIXME: is this enough?
  149. WebHeaderCollection w = new WebHeaderCollection ();
  150. w [HttpRequestHeader.CacheControl] = "Value2";
  151. Assertion.AssertEquals ("#1.2", "Value2", w [HttpRequestHeader.CacheControl]);
  152. w [HttpResponseHeader.Pragma] = "Value3";
  153. Assertion.AssertEquals ("#1.3", "Value3", w [HttpResponseHeader.Pragma]);
  154. }
  155. [Test]
  156. public void Remove ()
  157. {
  158. col.Remove ("Name1");
  159. col.Remove ("NameNotExist");
  160. Assert.AreEqual (1, col.Count, "#1");
  161. /*
  162. // this can only be tested in namespace System.Net
  163. try {
  164. WebHeaderCollection col2 = new WebHeaderCollection (true);
  165. col2.Add ("Host", "foo");
  166. col2.Remove ("Host");
  167. Assert.Fail ("#2: should fail according to spec");
  168. } catch (ArgumentException) {}
  169. */
  170. }
  171. [Test]
  172. public void Set ()
  173. {
  174. col.Add ("Name1", "Value1b");
  175. col.Set ("Name1", "\t X \t");
  176. Assert.AreEqual ("X", col.Get ("Name1"), "#1");
  177. }
  178. [Test]
  179. public void IsRestricted ()
  180. {
  181. Assert.IsTrue (!WebHeaderCollection.IsRestricted ("Xhost"), "#1");
  182. Assert.IsTrue (WebHeaderCollection.IsRestricted ("Host"), "#2");
  183. Assert.IsTrue (WebHeaderCollection.IsRestricted ("HOST"), "#3");
  184. Assert.IsTrue (WebHeaderCollection.IsRestricted ("Transfer-Encoding"), "#4");
  185. Assert.IsTrue (WebHeaderCollection.IsRestricted ("user-agent"), "#5");
  186. Assert.IsTrue (WebHeaderCollection.IsRestricted ("accept"), "#6");
  187. Assert.IsTrue (!WebHeaderCollection.IsRestricted ("accept-charset"), "#7");
  188. }
  189. [Test]
  190. public void ToStringTest ()
  191. {
  192. col.Add ("Name1", "Value1b");
  193. col.Add ("Name3", "Value3a\r\n Value3b");
  194. col.Add ("Name4", " Value4 ");
  195. Assert.AreEqual ("Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString (), "#1");
  196. WebHeaderCollection w;
  197. w = new WebHeaderCollection ();
  198. w.Add (HttpResponseHeader.KeepAlive, "Value1");
  199. w.Add (HttpResponseHeader.WwwAuthenticate, "Value2");
  200. Assertion.AssertEquals ("#2", "Keep-Alive: Value1\r\nWWW-Authenticate: Value2\r\n\r\n", w.ToString ());
  201. w = new WebHeaderCollection ();
  202. w.Add (HttpRequestHeader.UserAgent, "Value1");
  203. w.Add (HttpRequestHeader.ContentMd5, "Value2");
  204. Assertion.AssertEquals ("#2", "User-Agent: Value1\r\nContent-MD5: Value2\r\n\r\n", w.ToString ());
  205. }
  206. [Test]
  207. #if TARGET_JVM
  208. //FIXME: include Java serialization compliant tests - the order of object
  209. // in SerializationInfo should stay same to MS format...
  210. [Ignore ("The MS compliant binary serialization is not supported")]
  211. #endif
  212. public void GetObjectData ()
  213. {
  214. SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
  215. new FormatterConverter ());
  216. WebHeaderCollection headers = new WebHeaderCollection ();
  217. headers.Add ("Content-Type", "image/png");
  218. headers.Add ("No-Cache:off");
  219. headers.Add ("Disposition", "attach");
  220. ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
  221. Assert.AreEqual (7, si.MemberCount, "#A");
  222. int i = 0;
  223. foreach (SerializationEntry entry in si) {
  224. Assert.IsNotNull (entry.Name, "#B1:" + i);
  225. Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
  226. Assert.IsNotNull (entry.Value, "#B3:" + i);
  227. switch (i) {
  228. case 0:
  229. Assert.AreEqual ("Count", entry.Name, "#B4:" + i);
  230. Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
  231. Assert.AreEqual (3, entry.Value, "#B6:" + i);
  232. break;
  233. case 1:
  234. Assert.AreEqual ("0", entry.Name, "#B4:" + i);
  235. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  236. Assert.AreEqual ("Content-Type", entry.Value, "#B6:" + i);
  237. break;
  238. case 2:
  239. Assert.AreEqual ("3", entry.Name, "#B4:" + i);
  240. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  241. Assert.AreEqual ("image/png", entry.Value, "#B6:" + i);
  242. break;
  243. case 3:
  244. Assert.AreEqual ("1", entry.Name, "#B4:" + i);
  245. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  246. Assert.AreEqual ("No-Cache", entry.Value, "#B6:" + i);
  247. break;
  248. case 4:
  249. Assert.AreEqual ("4", entry.Name, "#B4:" + i);
  250. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  251. Assert.AreEqual ("off", entry.Value, "#B6:" + i);
  252. break;
  253. case 5:
  254. Assert.AreEqual ("2", entry.Name, "#B4:" + i);
  255. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  256. Assert.AreEqual ("Disposition", entry.Value, "#B6:" + i);
  257. break;
  258. case 6:
  259. Assert.AreEqual ("5", entry.Name, "#B4:" + i);
  260. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  261. Assert.AreEqual ("attach", entry.Value, "#B6:" + i);
  262. break;
  263. }
  264. i++;
  265. }
  266. }
  267. [Test]
  268. #if TARGET_JVM
  269. //FIXME: include Java serialization compliant tests
  270. [Ignore ("The MS compliant binary serialization is not supported")]
  271. #endif
  272. public void Serialize ()
  273. {
  274. WebHeaderCollection headers = new WebHeaderCollection ();
  275. headers.Add ("Content-Type", "image/png");
  276. headers.Add ("No-Cache:off");
  277. headers.Add ("Disposition", "attach");
  278. BinaryFormatter bf = new BinaryFormatter ();
  279. bf.AssemblyFormat = FormatterAssemblyStyle.Full;
  280. MemoryStream ms = new MemoryStream ();
  281. bf.Serialize (ms, headers);
  282. ms.Position = 0;
  283. byte [] buffer = new byte [ms.Length];
  284. ms.Read (buffer, 0, buffer.Length);
  285. Assert.AreEqual (_serialized, buffer);
  286. }
  287. [Test]
  288. #if TARGET_JVM
  289. //FIXME: include Java serialization compliant tests
  290. [Ignore ("The MS compliant binary serialization format is not supported")]
  291. #endif
  292. public void Deserialize ()
  293. {
  294. MemoryStream ms = new MemoryStream ();
  295. ms.Write (_serialized, 0, _serialized.Length);
  296. ms.Position = 0;
  297. BinaryFormatter bf = new BinaryFormatter ();
  298. WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
  299. }
  300. private static readonly byte [] _serialized = new byte [] {
  301. #if NET_2_0
  302. 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  303. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
  304. 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
  305. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
  306. 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
  307. 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
  308. 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
  309. 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
  310. 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
  311. 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
  312. 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
  313. 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
  314. 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
  315. 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
  316. 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
  317. 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
  318. 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
  319. 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
  320. 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
  321. 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
  322. 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
  323. 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
  324. 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
  325. 0x63, 0x68, 0x0b
  326. #else
  327. 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  328. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
  329. 0x4c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
  330. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31, 0x2e, 0x30, 0x2e, 0x35,
  331. 0x30, 0x30, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
  332. 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
  333. 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
  334. 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
  335. 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
  336. 0x05, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65,
  337. 0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65,
  338. 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74,
  339. 0x69, 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75,
  340. 0x6e, 0x74, 0x01, 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01,
  341. 0x32, 0x01, 0x35, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08,
  342. 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00,
  343. 0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
  344. 0x54, 0x79, 0x70, 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69,
  345. 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00,
  346. 0x00, 0x00, 0x08, 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65,
  347. 0x06, 0x06, 0x00, 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07,
  348. 0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69,
  349. 0x74, 0x69, 0x6f, 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61,
  350. 0x74, 0x74, 0x61, 0x63, 0x68, 0x0b
  351. #endif
  352. };
  353. [Test]
  354. public void IsRestricted_InvalidChars_1 ()
  355. {
  356. // Not allowed:
  357. // 0-32
  358. // 34
  359. // 39-41
  360. // 44
  361. // 47
  362. // 91-93
  363. // 123
  364. // 125
  365. // >= 127
  366. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  367. foreach (int single in singles) {
  368. try {
  369. WebHeaderCollection.IsRestricted (new string ((char) single, 1));
  370. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  371. } catch (ArgumentException) {
  372. }
  373. }
  374. for (int i = 0; i <= 32; i++) {
  375. try {
  376. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  377. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  378. } catch (ArgumentException) {
  379. }
  380. }
  381. for (int i = 39; i <= 41; i++) {
  382. try {
  383. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  384. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  385. } catch (ArgumentException) {
  386. }
  387. }
  388. for (int i = 91; i <= 93; i++) {
  389. try {
  390. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  391. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  392. } catch (ArgumentException) {
  393. }
  394. }
  395. for (int i = 127; i <= 255; i++) {
  396. try {
  397. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  398. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  399. } catch (ArgumentException) {
  400. }
  401. }
  402. }
  403. #if NET_2_0
  404. [Test]
  405. public void IsRestricted_InvalidChars_Request_2 ()
  406. {
  407. // Not allowed:
  408. // 0-32
  409. // 34
  410. // 39-41
  411. // 44
  412. // 47
  413. // 91-93
  414. // 123
  415. // 125
  416. // >= 127
  417. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  418. foreach (int single in singles) {
  419. try {
  420. WebHeaderCollection.IsRestricted (new string ((char) single, 1), false);
  421. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  422. } catch (ArgumentException) {
  423. }
  424. }
  425. for (int i = 0; i <= 32; i++) {
  426. try {
  427. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  428. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  429. } catch (ArgumentException) {
  430. }
  431. }
  432. for (int i = 39; i <= 41; i++) {
  433. try {
  434. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  435. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  436. } catch (ArgumentException) {
  437. }
  438. }
  439. for (int i = 91; i <= 93; i++) {
  440. try {
  441. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  442. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  443. } catch (ArgumentException) {
  444. }
  445. }
  446. for (int i = 127; i <= 255; i++) {
  447. try {
  448. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  449. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  450. } catch (ArgumentException) {
  451. }
  452. }
  453. }
  454. [Test]
  455. public void IsRestricted_InvalidChars_Response_2 ()
  456. {
  457. // Not allowed:
  458. // 0-32
  459. // 34
  460. // 39-41
  461. // 44
  462. // 47
  463. // 91-93
  464. // 123
  465. // 125
  466. // >= 127
  467. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  468. foreach (int single in singles) {
  469. try {
  470. WebHeaderCollection.IsRestricted (new string ((char) single, 1), true);
  471. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  472. } catch (ArgumentException) {
  473. }
  474. }
  475. for (int i = 0; i <= 32; i++) {
  476. try {
  477. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  478. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  479. } catch (ArgumentException) {
  480. }
  481. }
  482. for (int i = 39; i <= 41; i++) {
  483. try {
  484. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  485. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  486. } catch (ArgumentException) {
  487. }
  488. }
  489. for (int i = 91; i <= 93; i++) {
  490. try {
  491. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  492. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  493. } catch (ArgumentException) {
  494. }
  495. }
  496. for (int i = 127; i <= 255; i++) {
  497. try {
  498. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  499. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  500. } catch (ArgumentException) {
  501. }
  502. }
  503. }
  504. static string [] request_headers = new string [] {
  505. "Accept", "Accept-Charset", "Accept-Encoding", "Accept-Language", "Accept-Ranges", "Authorization",
  506. "Cache-Control", "Connection", "Cookie", "Content-Length", "Content-Type", "Date",
  507. "Expect", "From", "Host", "If-Match", "If-Modified-Since", "If-None-Match",
  508. "If-Range", "If-Unmodified-Since", "Max-Forwards", "Pragma", "Proxy-Authorization",
  509. "Range", "Referer", "TE", "Upgrade", "User-Agent", "Via", "Warn" };
  510. static string [] response_headers = new string [] {
  511. "Accept-Ranges", "Age", "Allow", "Cache-Control", "Content-Encoding", "Content-Language",
  512. "Content-Length", "Content-Location", "Content-Disposition", "Content-MD5", "Content-Range",
  513. "Content-Type", "Date", "ETag", "Expires", "Last-Modified", "Location", "Pragma",
  514. "Proxy-Authenticate", "Retry-After", "Server", "Set-Cookie", "Trailer",
  515. "Transfer-Encoding", "Vary", "Via", "Warn", "WWW-Authenticate" };
  516. static string [] restricted_request_request = new string [] {
  517. "Accept", "Connection", "Content-Length", "Content-Type", "Date",
  518. "Expect", "Host", "If-Modified-Since", "Range", "Referer",
  519. "User-Agent" };
  520. static string [] restricted_response_request = new string [] {
  521. "Content-Length", "Content-Type", "Date", "Transfer-Encoding" };
  522. static string [] restricted_request_response = new string [] {
  523. "Content-Length" };
  524. static string [] restricted_response_response = new string [] {
  525. "Content-Length", "Transfer-Encoding", "WWW-Authenticate" };
  526. [Test]
  527. public void IsRestricted_2_0_RequestRequest ()
  528. {
  529. int count = 0;
  530. foreach (string str in request_headers) {
  531. if (WebHeaderCollection.IsRestricted (str, false)) {
  532. Assert.IsTrue (Array.IndexOf (restricted_request_request, str) != -1, "restricted " + str);
  533. count++;
  534. } else {
  535. Assert.IsTrue (Array.IndexOf (restricted_request_request, str) == -1, str);
  536. }
  537. }
  538. Assert.IsTrue (count == restricted_request_request.Length, "req-req length");
  539. }
  540. [Test]
  541. public void IsRestricted_2_0_ResponseRequest ()
  542. {
  543. int count = 0;
  544. foreach (string str in response_headers) {
  545. if (WebHeaderCollection.IsRestricted (str, false)) {
  546. Assert.IsTrue (Array.IndexOf (restricted_response_request, str) != -1, "restricted " + str);
  547. count++;
  548. } else {
  549. Assert.IsTrue (Array.IndexOf (restricted_response_request, str) == -1, str);
  550. }
  551. }
  552. Assert.IsTrue (count == restricted_response_request.Length, "length");
  553. }
  554. [Test]
  555. public void IsRestricted_2_0_RequestResponse ()
  556. {
  557. int count = 0;
  558. foreach (string str in request_headers) {
  559. if (WebHeaderCollection.IsRestricted (str, true)) {
  560. Assert.IsTrue (Array.IndexOf (restricted_request_response, str) != -1, "restricted " + str);
  561. count++;
  562. } else {
  563. Assert.IsTrue (Array.IndexOf (restricted_request_response, str) == -1, str);
  564. }
  565. }
  566. Assert.IsTrue (count == restricted_request_response.Length, "length");
  567. }
  568. [Test]
  569. public void IsRestricted_2_0_ResponseResponse ()
  570. {
  571. int count = 0;
  572. foreach (string str in response_headers) {
  573. if (WebHeaderCollection.IsRestricted (str, true)) {
  574. Assert.IsTrue (Array.IndexOf (restricted_response_response, str) != -1, "restricted " + str);
  575. count++;
  576. } else {
  577. Assert.IsTrue (Array.IndexOf (restricted_response_response, str) == -1, str);
  578. }
  579. }
  580. Assert.IsTrue (count == restricted_response_response.Length, "length");
  581. }
  582. #endif
  583. }
  584. }