WebHeaderCollectionTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 Indexers ()
  182. {
  183. Assert.AreEqual ("Value1", ((NameValueCollection)col)[0], "#1.1");
  184. WebHeaderCollection w = new WebHeaderCollection ();
  185. w [HttpRequestHeader.CacheControl] = "Value2";
  186. Assert.AreEqual ("Value2", w[HttpRequestHeader.CacheControl], "#1.2");
  187. try {
  188. w[HttpResponseHeader.Pragma] = "Value3";
  189. Assert.Fail ("#1.3");
  190. } catch (InvalidOperationException) {
  191. }
  192. }
  193. [Test]
  194. public void Remove ()
  195. {
  196. col.Remove ("Name1");
  197. col.Remove ("NameNotExist");
  198. Assert.AreEqual (1, col.Count, "#1");
  199. }
  200. [Test]
  201. public void RemoveRestricted ()
  202. {
  203. col = CreateRestrictedHeaders ();
  204. try {
  205. col.Add ("Host", "foo");
  206. col.Remove ("Host");
  207. Assert.Fail ("#2: should fail according to spec");
  208. } catch (ArgumentException) {}
  209. }
  210. [Test]
  211. public void Set ()
  212. {
  213. col.Add ("Name1", "Value1b");
  214. col.Set ("Name1", "\t X \t");
  215. Assert.AreEqual ("X", col.Get ("Name1"), "#1");
  216. }
  217. [Test]
  218. public void IsRestricted ()
  219. {
  220. Assert.IsTrue (!WebHeaderCollection.IsRestricted ("Xhost"), "#1");
  221. Assert.IsTrue (WebHeaderCollection.IsRestricted ("Host"), "#2");
  222. Assert.IsTrue (WebHeaderCollection.IsRestricted ("HOST"), "#3");
  223. Assert.IsTrue (WebHeaderCollection.IsRestricted ("Transfer-Encoding"), "#4");
  224. Assert.IsTrue (WebHeaderCollection.IsRestricted ("user-agent"), "#5");
  225. Assert.IsTrue (WebHeaderCollection.IsRestricted ("accept"), "#6");
  226. Assert.IsTrue (!WebHeaderCollection.IsRestricted ("accept-charset"), "#7");
  227. }
  228. [Test]
  229. public void ToStringTest ()
  230. {
  231. col.Add ("Name1", "Value1b");
  232. col.Add ("Name3", "Value3a\r\n Value3b");
  233. col.Add ("Name4", " Value4 ");
  234. Assert.AreEqual ("Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString (), "#1");
  235. WebHeaderCollection w;
  236. w = new WebHeaderCollection ();
  237. w.Add (HttpResponseHeader.KeepAlive, "Value1");
  238. w.Add (HttpResponseHeader.WwwAuthenticate, "Value2");
  239. Assert.AreEqual ("Keep-Alive: Value1\r\nWWW-Authenticate: Value2\r\n\r\n", w.ToString (), "#2");
  240. w = new WebHeaderCollection ();
  241. w.Add (HttpRequestHeader.UserAgent, "Value1");
  242. w.Add (HttpRequestHeader.ContentMd5, "Value2");
  243. Assert.AreEqual ("User-Agent: Value1\r\nContent-MD5: Value2\r\n\r\n", w.ToString (), "#3");
  244. }
  245. [Test]
  246. public void GetObjectData ()
  247. {
  248. SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
  249. new FormatterConverter ());
  250. WebHeaderCollection headers = new WebHeaderCollection ();
  251. headers.Add ("Content-Type", "image/png");
  252. headers.Add ("No-Cache:off");
  253. headers.Add ("Disposition", "attach");
  254. ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
  255. Assert.AreEqual (7, si.MemberCount, "#A");
  256. int i = 0;
  257. foreach (SerializationEntry entry in si) {
  258. Assert.IsNotNull (entry.Name, "#B1:" + i);
  259. Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
  260. Assert.IsNotNull (entry.Value, "#B3:" + i);
  261. switch (i) {
  262. case 0:
  263. Assert.AreEqual ("Count", entry.Name, "#B4:" + i);
  264. Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
  265. Assert.AreEqual (3, entry.Value, "#B6:" + i);
  266. break;
  267. case 1:
  268. Assert.AreEqual ("0", entry.Name, "#B4:" + i);
  269. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  270. Assert.AreEqual ("Content-Type", entry.Value, "#B6:" + i);
  271. break;
  272. case 2:
  273. Assert.AreEqual ("3", entry.Name, "#B4:" + i);
  274. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  275. Assert.AreEqual ("image/png", entry.Value, "#B6:" + i);
  276. break;
  277. case 3:
  278. Assert.AreEqual ("1", entry.Name, "#B4:" + i);
  279. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  280. Assert.AreEqual ("No-Cache", entry.Value, "#B6:" + i);
  281. break;
  282. case 4:
  283. Assert.AreEqual ("4", entry.Name, "#B4:" + i);
  284. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  285. Assert.AreEqual ("off", entry.Value, "#B6:" + i);
  286. break;
  287. case 5:
  288. Assert.AreEqual ("2", entry.Name, "#B4:" + i);
  289. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  290. Assert.AreEqual ("Disposition", entry.Value, "#B6:" + i);
  291. break;
  292. case 6:
  293. Assert.AreEqual ("5", entry.Name, "#B4:" + i);
  294. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  295. Assert.AreEqual ("attach", entry.Value, "#B6:" + i);
  296. break;
  297. }
  298. i++;
  299. }
  300. }
  301. [Test]
  302. public void Serialize ()
  303. {
  304. WebHeaderCollection headers = new WebHeaderCollection ();
  305. headers.Add ("Content-Type", "image/png");
  306. headers.Add ("No-Cache:off");
  307. headers.Add ("Disposition", "attach");
  308. BinaryFormatter bf = new BinaryFormatter ();
  309. bf.AssemblyFormat = FormatterAssemblyStyle.Full;
  310. MemoryStream ms = new MemoryStream ();
  311. bf.Serialize (ms, headers);
  312. ms.Position = 0;
  313. byte [] buffer = new byte [ms.Length];
  314. ms.Read (buffer, 0, buffer.Length);
  315. Assert.AreEqual (_serialized, buffer);
  316. }
  317. [Test]
  318. public void Deserialize ()
  319. {
  320. MemoryStream ms = new MemoryStream ();
  321. ms.Write (_serialized, 0, _serialized.Length);
  322. ms.Position = 0;
  323. BinaryFormatter bf = new BinaryFormatter ();
  324. WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
  325. }
  326. private static readonly byte [] _serialized = new byte [] {
  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. 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
  330. #if MOBILE
  331. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x35,
  332. #elif NET_4_0
  333. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x34, 0x2e, 0x30, 0x2e, 0x30,
  334. #else
  335. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
  336. #endif
  337. 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
  338. 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
  339. 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
  340. #if MOBILE
  341. 0x65, 0x6e, 0x3d, 0x37, 0x63, 0x65, 0x63, 0x38, 0x35, 0x64, 0x37,
  342. 0x62, 0x65, 0x61, 0x37, 0x37, 0x39, 0x38, 0x65, 0x05, 0x01, 0x00,
  343. #else
  344. 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
  345. 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
  346. #endif
  347. 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
  348. 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
  349. 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
  350. 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
  351. 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
  352. 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
  353. 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
  354. 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
  355. 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
  356. 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
  357. 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
  358. 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
  359. 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
  360. 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
  361. 0x63, 0x68, 0x0b
  362. };
  363. [Test]
  364. public void IsRestricted_InvalidChars_1 ()
  365. {
  366. // Not allowed:
  367. // 0-32
  368. // 34
  369. // 39-41
  370. // 44
  371. // 47
  372. // 91-93
  373. // 123
  374. // 125
  375. // >= 127
  376. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  377. foreach (int single in singles) {
  378. try {
  379. WebHeaderCollection.IsRestricted (new string ((char) single, 1));
  380. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  381. } catch (ArgumentException) {
  382. }
  383. }
  384. for (int i = 0; i <= 32; i++) {
  385. try {
  386. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  387. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  388. } catch (ArgumentException) {
  389. }
  390. }
  391. for (int i = 39; i <= 41; i++) {
  392. try {
  393. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  394. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  395. } catch (ArgumentException) {
  396. }
  397. }
  398. for (int i = 91; i <= 93; i++) {
  399. try {
  400. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  401. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  402. } catch (ArgumentException) {
  403. }
  404. }
  405. for (int i = 127; i <= 255; i++) {
  406. try {
  407. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  408. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  409. } catch (ArgumentException) {
  410. }
  411. }
  412. }
  413. [Test]
  414. public void IsRestricted_InvalidChars_Request_2 ()
  415. {
  416. // Not allowed:
  417. // 0-32
  418. // 34
  419. // 39-41
  420. // 44
  421. // 47
  422. // 91-93
  423. // 123
  424. // 125
  425. // >= 127
  426. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  427. foreach (int single in singles) {
  428. try {
  429. WebHeaderCollection.IsRestricted (new string ((char) single, 1), false);
  430. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  431. } catch (ArgumentException) {
  432. }
  433. }
  434. for (int i = 0; i <= 32; i++) {
  435. try {
  436. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  437. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  438. } catch (ArgumentException) {
  439. }
  440. }
  441. for (int i = 39; i <= 41; i++) {
  442. try {
  443. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  444. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  445. } catch (ArgumentException) {
  446. }
  447. }
  448. for (int i = 91; i <= 93; i++) {
  449. try {
  450. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  451. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  452. } catch (ArgumentException) {
  453. }
  454. }
  455. for (int i = 127; i <= 255; i++) {
  456. try {
  457. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  458. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  459. } catch (ArgumentException) {
  460. }
  461. }
  462. }
  463. [Test]
  464. public void IsRestricted_InvalidChars_Response_2 ()
  465. {
  466. // Not allowed:
  467. // 0-32
  468. // 34
  469. // 39-41
  470. // 44
  471. // 47
  472. // 91-93
  473. // 123
  474. // 125
  475. // >= 127
  476. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  477. foreach (int single in singles) {
  478. try {
  479. WebHeaderCollection.IsRestricted (new string ((char) single, 1), true);
  480. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  481. } catch (ArgumentException) {
  482. }
  483. }
  484. for (int i = 0; i <= 32; i++) {
  485. try {
  486. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  487. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  488. } catch (ArgumentException) {
  489. }
  490. }
  491. for (int i = 39; i <= 41; i++) {
  492. try {
  493. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  494. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  495. } catch (ArgumentException) {
  496. }
  497. }
  498. for (int i = 91; i <= 93; i++) {
  499. try {
  500. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  501. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  502. } catch (ArgumentException) {
  503. }
  504. }
  505. for (int i = 127; i <= 255; i++) {
  506. try {
  507. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  508. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  509. } catch (ArgumentException) {
  510. }
  511. }
  512. }
  513. static string [] request_headers = new string [] {
  514. "Accept", "Accept-Charset", "Accept-Encoding", "Accept-Language", "Accept-Ranges", "Authorization",
  515. "Cache-Control", "Connection", "Cookie", "Content-Length", "Content-Type", "Date",
  516. "Expect", "From", "Host", "If-Match", "If-Modified-Since", "If-None-Match",
  517. "If-Range", "If-Unmodified-Since", "Max-Forwards", "Pragma", "Proxy-Authorization", "Proxy-Connection",
  518. "Range", "Referer", "TE", "Transfer-Encoding", "Upgrade", "User-Agent", "Via", "Warn" };
  519. static string [] response_headers = new string [] {
  520. "Accept-Ranges", "Age", "Allow", "Cache-Control", "Content-Encoding", "Content-Language",
  521. "Content-Length", "Content-Location", "Content-Disposition", "Content-MD5", "Content-Range",
  522. "Content-Type", "Date", "ETag", "Expires", "Keep-Alive", "Last-Modified", "Location", "Pragma",
  523. "Proxy-Authenticate", "Retry-After", "Server", "Set-Cookie", "Trailer",
  524. "Transfer-Encoding", "Vary", "Via", "Warn", "WWW-Authenticate" };
  525. static string [] restricted_request_request = new string [] {
  526. "Accept", "Connection", "Content-Length", "Content-Type", "Date",
  527. "Expect", "Host", "If-Modified-Since", "Proxy-Connection", "Range", "Referer",
  528. "Transfer-Encoding", "User-Agent" };
  529. static string [] restricted_response_request = new string [] {
  530. "Content-Length", "Content-Type", "Date", "Transfer-Encoding" };
  531. static string [] restricted_request_response = new string [] {
  532. "Content-Length", "Transfer-Encoding" };
  533. static string [] restricted_response_response = new string [] {
  534. "Content-Length", "Keep-Alive", "Transfer-Encoding", "WWW-Authenticate" };
  535. [Test]
  536. public void IsRestricted_2_0_RequestRequest ()
  537. {
  538. int count = 0;
  539. foreach (string str in request_headers) {
  540. if (WebHeaderCollection.IsRestricted (str, false)) {
  541. Assert.IsTrue (Array.IndexOf (restricted_request_request, str) != -1, "restricted " + str);
  542. count++;
  543. } else {
  544. Assert.IsTrue (Array.IndexOf (restricted_request_request, str) == -1, str);
  545. }
  546. }
  547. Assert.IsTrue (count == restricted_request_request.Length, "req-req length");
  548. }
  549. [Test]
  550. public void IsRestricted_2_0_ResponseRequest ()
  551. {
  552. int count = 0;
  553. foreach (string str in response_headers) {
  554. if (WebHeaderCollection.IsRestricted (str, false)) {
  555. Assert.IsTrue (Array.IndexOf (restricted_response_request, str) != -1, "restricted " + str);
  556. count++;
  557. } else {
  558. Assert.IsTrue (Array.IndexOf (restricted_response_request, str) == -1, str);
  559. }
  560. }
  561. Assert.IsTrue (count == restricted_response_request.Length, "length");
  562. }
  563. [Test]
  564. public void IsRestricted_2_0_RequestResponse ()
  565. {
  566. int count = 0;
  567. foreach (string str in request_headers) {
  568. if (WebHeaderCollection.IsRestricted (str, true)) {
  569. Assert.IsTrue (Array.IndexOf (restricted_request_response, str) != -1, "restricted " + str);
  570. count++;
  571. } else {
  572. Assert.IsTrue (Array.IndexOf (restricted_request_response, str) == -1, str);
  573. }
  574. }
  575. Assert.IsTrue (count == restricted_request_response.Length, "length");
  576. }
  577. [Test]
  578. public void IsRestricted_2_0_ResponseResponse ()
  579. {
  580. int count = 0;
  581. foreach (string str in response_headers) {
  582. if (WebHeaderCollection.IsRestricted (str, true)) {
  583. Assert.IsTrue (Array.IndexOf (restricted_response_response, str) != -1, "restricted " + str);
  584. count++;
  585. } else {
  586. Assert.IsTrue (Array.IndexOf (restricted_response_response, str) == -1, str);
  587. }
  588. }
  589. Assert.IsTrue (count == restricted_response_response.Length, "length");
  590. }
  591. static WebHeaderCollection CreateRestrictedHeaders ()
  592. {
  593. var factory = Activator.CreateInstance (typeof (IWebRequestCreate).Assembly.GetType ("System.Net.HttpRequestCreator"), true) as IWebRequestCreate;
  594. return factory.Create (new Uri ("http://localhost")).Headers;
  595. }
  596. }
  597. }