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