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. 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. #if TARGET_JVM
  247. //FIXME: include Java serialization compliant tests - the order of object
  248. // in SerializationInfo should stay same to MS format...
  249. [Ignore ("The MS compliant binary serialization is not supported")]
  250. #endif
  251. public void GetObjectData ()
  252. {
  253. SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
  254. new FormatterConverter ());
  255. WebHeaderCollection headers = new WebHeaderCollection ();
  256. headers.Add ("Content-Type", "image/png");
  257. headers.Add ("No-Cache:off");
  258. headers.Add ("Disposition", "attach");
  259. ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
  260. Assert.AreEqual (7, si.MemberCount, "#A");
  261. int i = 0;
  262. foreach (SerializationEntry entry in si) {
  263. Assert.IsNotNull (entry.Name, "#B1:" + i);
  264. Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
  265. Assert.IsNotNull (entry.Value, "#B3:" + i);
  266. switch (i) {
  267. case 0:
  268. Assert.AreEqual ("Count", entry.Name, "#B4:" + i);
  269. Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
  270. Assert.AreEqual (3, entry.Value, "#B6:" + i);
  271. break;
  272. case 1:
  273. Assert.AreEqual ("0", entry.Name, "#B4:" + i);
  274. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  275. Assert.AreEqual ("Content-Type", entry.Value, "#B6:" + i);
  276. break;
  277. case 2:
  278. Assert.AreEqual ("3", entry.Name, "#B4:" + i);
  279. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  280. Assert.AreEqual ("image/png", entry.Value, "#B6:" + i);
  281. break;
  282. case 3:
  283. Assert.AreEqual ("1", entry.Name, "#B4:" + i);
  284. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  285. Assert.AreEqual ("No-Cache", entry.Value, "#B6:" + i);
  286. break;
  287. case 4:
  288. Assert.AreEqual ("4", entry.Name, "#B4:" + i);
  289. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  290. Assert.AreEqual ("off", entry.Value, "#B6:" + i);
  291. break;
  292. case 5:
  293. Assert.AreEqual ("2", entry.Name, "#B4:" + i);
  294. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  295. Assert.AreEqual ("Disposition", entry.Value, "#B6:" + i);
  296. break;
  297. case 6:
  298. Assert.AreEqual ("5", entry.Name, "#B4:" + i);
  299. Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
  300. Assert.AreEqual ("attach", entry.Value, "#B6:" + i);
  301. break;
  302. }
  303. i++;
  304. }
  305. }
  306. [Test]
  307. #if TARGET_JVM
  308. //FIXME: include Java serialization compliant tests
  309. [Ignore ("The MS compliant binary serialization is not supported")]
  310. #endif
  311. public void Serialize ()
  312. {
  313. WebHeaderCollection headers = new WebHeaderCollection ();
  314. headers.Add ("Content-Type", "image/png");
  315. headers.Add ("No-Cache:off");
  316. headers.Add ("Disposition", "attach");
  317. BinaryFormatter bf = new BinaryFormatter ();
  318. bf.AssemblyFormat = FormatterAssemblyStyle.Full;
  319. MemoryStream ms = new MemoryStream ();
  320. bf.Serialize (ms, headers);
  321. ms.Position = 0;
  322. byte [] buffer = new byte [ms.Length];
  323. ms.Read (buffer, 0, buffer.Length);
  324. Assert.AreEqual (_serialized, buffer);
  325. }
  326. [Test]
  327. #if TARGET_JVM
  328. //FIXME: include Java serialization compliant tests
  329. [Ignore ("The MS compliant binary serialization format is not supported")]
  330. #endif
  331. public void Deserialize ()
  332. {
  333. MemoryStream ms = new MemoryStream ();
  334. ms.Write (_serialized, 0, _serialized.Length);
  335. ms.Position = 0;
  336. BinaryFormatter bf = new BinaryFormatter ();
  337. WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
  338. }
  339. private static readonly byte [] _serialized = new byte [] {
  340. 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  341. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
  342. 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
  343. #if MOBILE
  344. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x35,
  345. #elif NET_4_0
  346. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x34, 0x2e, 0x30, 0x2e, 0x30,
  347. #else
  348. 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
  349. #endif
  350. 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
  351. 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
  352. 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
  353. #if MOBILE
  354. 0x65, 0x6e, 0x3d, 0x37, 0x63, 0x65, 0x63, 0x38, 0x35, 0x64, 0x37,
  355. 0x62, 0x65, 0x61, 0x37, 0x37, 0x39, 0x38, 0x65, 0x05, 0x01, 0x00,
  356. #else
  357. 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
  358. 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
  359. #endif
  360. 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
  361. 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
  362. 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
  363. 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
  364. 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
  365. 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
  366. 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
  367. 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
  368. 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
  369. 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
  370. 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
  371. 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
  372. 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
  373. 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
  374. 0x63, 0x68, 0x0b
  375. };
  376. [Test]
  377. public void IsRestricted_InvalidChars_1 ()
  378. {
  379. // Not allowed:
  380. // 0-32
  381. // 34
  382. // 39-41
  383. // 44
  384. // 47
  385. // 91-93
  386. // 123
  387. // 125
  388. // >= 127
  389. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  390. foreach (int single in singles) {
  391. try {
  392. WebHeaderCollection.IsRestricted (new string ((char) single, 1));
  393. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  394. } catch (ArgumentException) {
  395. }
  396. }
  397. for (int i = 0; i <= 32; 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 = 39; i <= 41; 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 = 91; i <= 93; 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. for (int i = 127; i <= 255; i++) {
  419. try {
  420. WebHeaderCollection.IsRestricted (new string ((char) i, 1));
  421. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  422. } catch (ArgumentException) {
  423. }
  424. }
  425. }
  426. [Test]
  427. public void IsRestricted_InvalidChars_Request_2 ()
  428. {
  429. // Not allowed:
  430. // 0-32
  431. // 34
  432. // 39-41
  433. // 44
  434. // 47
  435. // 91-93
  436. // 123
  437. // 125
  438. // >= 127
  439. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  440. foreach (int single in singles) {
  441. try {
  442. WebHeaderCollection.IsRestricted (new string ((char) single, 1), false);
  443. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  444. } catch (ArgumentException) {
  445. }
  446. }
  447. for (int i = 0; i <= 32; 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 = 39; i <= 41; 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 = 91; i <= 93; 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. for (int i = 127; i <= 255; i++) {
  469. try {
  470. WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
  471. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  472. } catch (ArgumentException) {
  473. }
  474. }
  475. }
  476. [Test]
  477. public void IsRestricted_InvalidChars_Response_2 ()
  478. {
  479. // Not allowed:
  480. // 0-32
  481. // 34
  482. // 39-41
  483. // 44
  484. // 47
  485. // 91-93
  486. // 123
  487. // 125
  488. // >= 127
  489. int [] singles = new int [] { 34, 44, 47, 123, 125 };
  490. foreach (int single in singles) {
  491. try {
  492. WebHeaderCollection.IsRestricted (new string ((char) single, 1), true);
  493. Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
  494. } catch (ArgumentException) {
  495. }
  496. }
  497. for (int i = 0; i <= 32; 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 = 39; i <= 41; 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 = 91; i <= 93; 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. for (int i = 127; i <= 255; i++) {
  519. try {
  520. WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
  521. Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
  522. } catch (ArgumentException) {
  523. }
  524. }
  525. }
  526. static string [] request_headers = new string [] {
  527. "Accept", "Accept-Charset", "Accept-Encoding", "Accept-Language", "Accept-Ranges", "Authorization",
  528. "Cache-Control", "Connection", "Cookie", "Content-Length", "Content-Type", "Date",
  529. "Expect", "From", "Host", "If-Match", "If-Modified-Since", "If-None-Match",
  530. "If-Range", "If-Unmodified-Since", "Max-Forwards", "Pragma", "Proxy-Authorization", "Proxy-Connection",
  531. "Range", "Referer", "TE", "Transfer-Encoding", "Upgrade", "User-Agent", "Via", "Warn" };
  532. static string [] response_headers = new string [] {
  533. "Accept-Ranges", "Age", "Allow", "Cache-Control", "Content-Encoding", "Content-Language",
  534. "Content-Length", "Content-Location", "Content-Disposition", "Content-MD5", "Content-Range",
  535. "Content-Type", "Date", "ETag", "Expires", "Keep-Alive", "Last-Modified", "Location", "Pragma",
  536. "Proxy-Authenticate", "Retry-After", "Server", "Set-Cookie", "Trailer",
  537. "Transfer-Encoding", "Vary", "Via", "Warn", "WWW-Authenticate" };
  538. static string [] restricted_request_request = new string [] {
  539. "Accept", "Connection", "Content-Length", "Content-Type", "Date",
  540. "Expect", "Host", "If-Modified-Since", "Proxy-Connection", "Range", "Referer",
  541. "Transfer-Encoding", "User-Agent" };
  542. static string [] restricted_response_request = new string [] {
  543. "Content-Length", "Content-Type", "Date", "Transfer-Encoding" };
  544. static string [] restricted_request_response = new string [] {
  545. "Content-Length", "Transfer-Encoding" };
  546. static string [] restricted_response_response = new string [] {
  547. "Content-Length", "Keep-Alive", "Transfer-Encoding", "WWW-Authenticate" };
  548. [Test]
  549. public void IsRestricted_2_0_RequestRequest ()
  550. {
  551. int count = 0;
  552. foreach (string str in request_headers) {
  553. if (WebHeaderCollection.IsRestricted (str, false)) {
  554. Assert.IsTrue (Array.IndexOf (restricted_request_request, str) != -1, "restricted " + str);
  555. count++;
  556. } else {
  557. Assert.IsTrue (Array.IndexOf (restricted_request_request, str) == -1, str);
  558. }
  559. }
  560. Assert.IsTrue (count == restricted_request_request.Length, "req-req length");
  561. }
  562. [Test]
  563. public void IsRestricted_2_0_ResponseRequest ()
  564. {
  565. int count = 0;
  566. foreach (string str in response_headers) {
  567. if (WebHeaderCollection.IsRestricted (str, false)) {
  568. Assert.IsTrue (Array.IndexOf (restricted_response_request, str) != -1, "restricted " + str);
  569. count++;
  570. } else {
  571. Assert.IsTrue (Array.IndexOf (restricted_response_request, str) == -1, str);
  572. }
  573. }
  574. Assert.IsTrue (count == restricted_response_request.Length, "length");
  575. }
  576. [Test]
  577. public void IsRestricted_2_0_RequestResponse ()
  578. {
  579. int count = 0;
  580. foreach (string str in request_headers) {
  581. if (WebHeaderCollection.IsRestricted (str, true)) {
  582. Assert.IsTrue (Array.IndexOf (restricted_request_response, str) != -1, "restricted " + str);
  583. count++;
  584. } else {
  585. Assert.IsTrue (Array.IndexOf (restricted_request_response, str) == -1, str);
  586. }
  587. }
  588. Assert.IsTrue (count == restricted_request_response.Length, "length");
  589. }
  590. [Test]
  591. public void IsRestricted_2_0_ResponseResponse ()
  592. {
  593. int count = 0;
  594. foreach (string str in response_headers) {
  595. if (WebHeaderCollection.IsRestricted (str, true)) {
  596. Assert.IsTrue (Array.IndexOf (restricted_response_response, str) != -1, "restricted " + str);
  597. count++;
  598. } else {
  599. Assert.IsTrue (Array.IndexOf (restricted_response_response, str) == -1, str);
  600. }
  601. }
  602. Assert.IsTrue (count == restricted_response_response.Length, "length");
  603. }
  604. static WebHeaderCollection CreateRestrictedHeaders ()
  605. {
  606. var factory = Activator.CreateInstance (typeof (IWebRequestCreate).Assembly.GetType ("System.Net.HttpRequestCreator"), true) as IWebRequestCreate;
  607. return factory.Create (new Uri ("http://localhost")).Headers;
  608. }
  609. }
  610. }