2
0

WebHeaderCollectionTest.cs 20 KB

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