WebHeaderCollectionTest.cs 20 KB

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