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