copied mono-api-diff.cs from mono-2-2 branch so new patch can be applied and history...
[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                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
341                         0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
342                         0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
343                         0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
344                         0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
345                         0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
346                         0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
347                         0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
348                         0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
349                         0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
350                         0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
351                         0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
352                         0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
353                         0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
354                         0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
355                         0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
356                         0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
357                         0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
358                         0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
359                         0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
360                         0x63, 0x68, 0x0b
361 #else
362                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
363                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
364                         0x4c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
365                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31, 0x2e, 0x30, 0x2e, 0x35,
366                         0x30, 0x30, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
367                         0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
368                         0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
369                         0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
370                         0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
371                         0x05, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65,
372                         0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65,
373                         0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74,
374                         0x69, 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75,
375                         0x6e, 0x74, 0x01, 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01,
376                         0x32, 0x01, 0x35, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08,
377                         0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00,
378                         0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
379                         0x54, 0x79, 0x70, 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69,
380                         0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00,
381                         0x00, 0x00, 0x08, 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65,
382                         0x06, 0x06, 0x00, 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07,
383                         0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69,
384                         0x74, 0x69, 0x6f, 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61,
385                         0x74, 0x74, 0x61, 0x63, 0x68, 0x0b
386 #endif
387                 };
388
389                 [Test]
390                 public void IsRestricted_InvalidChars_1 ()
391                 {
392                         // Not allowed:
393                         //      0-32
394                         //      34
395                         //      39-41
396                         //      44
397                         //      47
398                         //      91-93
399                         //      123
400                         //      125
401                         //      >= 127
402                         int [] singles = new int [] { 34, 44, 47, 123, 125 };
403                         foreach (int single in singles) {
404                                 try {
405                                         WebHeaderCollection.IsRestricted (new string ((char) single, 1));
406                                         Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
407                                 } catch (ArgumentException) {
408                                 }
409                         }
410                         for (int i = 0; i <= 32; 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 = 39; i <= 41; 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                         for (int i = 91; i <= 93; i++) {
425                                 try {
426                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1));
427                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
428                                 } catch (ArgumentException) {
429                                 }
430                         }
431                         for (int i = 127; i <= 255; i++) {
432                                 try {
433                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1));
434                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
435                                 } catch (ArgumentException) {
436                                 }
437                         }
438                 }
439 #if NET_2_0
440                 [Test]
441                 public void IsRestricted_InvalidChars_Request_2 ()
442                 {
443                         // Not allowed:
444                         //      0-32
445                         //      34
446                         //      39-41
447                         //      44
448                         //      47
449                         //      91-93
450                         //      123
451                         //      125
452                         //      >= 127
453                         int [] singles = new int [] { 34, 44, 47, 123, 125 };
454                         foreach (int single in singles) {
455                                 try {
456                                         WebHeaderCollection.IsRestricted (new string ((char) single, 1), false);
457                                         Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
458                                 } catch (ArgumentException) {
459                                 }
460                         }
461                         for (int i = 0; i <= 32; 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 = 39; i <= 41; 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                         for (int i = 91; i <= 93; i++) {
476                                 try {
477                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
478                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
479                                 } catch (ArgumentException) {
480                                 }
481                         }
482                         for (int i = 127; i <= 255; i++) {
483                                 try {
484                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
485                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
486                                 } catch (ArgumentException) {
487                                 }
488                         }
489                 }
490
491                 [Test]
492                 public void IsRestricted_InvalidChars_Response_2 ()
493                 {
494                         // Not allowed:
495                         //      0-32
496                         //      34
497                         //      39-41
498                         //      44
499                         //      47
500                         //      91-93
501                         //      123
502                         //      125
503                         //      >= 127
504                         int [] singles = new int [] { 34, 44, 47, 123, 125 };
505                         foreach (int single in singles) {
506                                 try {
507                                         WebHeaderCollection.IsRestricted (new string ((char) single, 1), true);
508                                         Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
509                                 } catch (ArgumentException) {
510                                 }
511                         }
512                         for (int i = 0; i <= 32; i++) {
513                                 try {
514                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
515                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
516                                 } catch (ArgumentException) {
517                                 }
518                         }
519                         for (int i = 39; i <= 41; i++) {
520                                 try {
521                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
522                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
523                                 } catch (ArgumentException) {
524                                 }
525                         }
526                         for (int i = 91; i <= 93; i++) {
527                                 try {
528                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
529                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
530                                 } catch (ArgumentException) {
531                                 }
532                         }
533                         for (int i = 127; i <= 255; i++) {
534                                 try {
535                                         WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
536                                         Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
537                                 } catch (ArgumentException) {
538                                 }
539                         }
540                 }
541
542                 static string [] request_headers = new string [] {
543                         "Accept", "Accept-Charset", "Accept-Encoding", "Accept-Language", "Accept-Ranges", "Authorization", 
544                         "Cache-Control", "Connection", "Cookie", "Content-Length", "Content-Type", "Date", 
545                         "Expect", "From", "Host", "If-Match", "If-Modified-Since", "If-None-Match", 
546                         "If-Range", "If-Unmodified-Since", "Max-Forwards", "Pragma", "Proxy-Authorization", 
547                         "Range", "Referer", "TE", "Upgrade", "User-Agent", "Via", "Warn" };
548
549                 static string [] response_headers = new string [] {
550                         "Accept-Ranges", "Age", "Allow", "Cache-Control", "Content-Encoding", "Content-Language", 
551                         "Content-Length", "Content-Location", "Content-Disposition", "Content-MD5", "Content-Range", 
552                         "Content-Type", "Date", "ETag", "Expires", "Last-Modified", "Location", "Pragma", 
553                         "Proxy-Authenticate", "Retry-After", "Server", "Set-Cookie", "Trailer", 
554                         "Transfer-Encoding", "Vary", "Via", "Warn", "WWW-Authenticate" };
555
556                 static string [] restricted_request_request = new string [] {
557                         "Accept", "Connection", "Content-Length", "Content-Type", "Date",
558                         "Expect", "Host", "If-Modified-Since", "Range", "Referer",
559                         "User-Agent" };
560                 static string [] restricted_response_request = new string [] {
561                         "Content-Length", "Content-Type", "Date", "Transfer-Encoding" };
562
563                 static string [] restricted_request_response = new string [] {
564                          "Content-Length" };
565                 static string [] restricted_response_response = new string [] {
566                          "Content-Length", "Transfer-Encoding", "WWW-Authenticate" };
567
568                 [Test]
569                 public void IsRestricted_2_0_RequestRequest ()
570                 {
571                         int count = 0;
572                         foreach (string str in request_headers) {
573                                 if (WebHeaderCollection.IsRestricted (str, false)) {
574                                         Assert.IsTrue (Array.IndexOf (restricted_request_request, str) != -1, "restricted " + str);
575                                         count++;
576                                 } else {
577                                         Assert.IsTrue (Array.IndexOf (restricted_request_request, str) == -1, str);
578                                 }
579                         }
580                         Assert.IsTrue (count == restricted_request_request.Length, "req-req length");
581                 }
582
583                 [Test]
584                 public void IsRestricted_2_0_ResponseRequest ()
585                 {
586                         int count = 0;
587                         foreach (string str in response_headers) {
588                                 if (WebHeaderCollection.IsRestricted (str, false)) {
589                                         Assert.IsTrue (Array.IndexOf (restricted_response_request, str) != -1, "restricted " + str);
590                                         count++;
591                                 } else {
592                                         Assert.IsTrue (Array.IndexOf (restricted_response_request, str) == -1, str);
593                                 }
594                         }
595                         Assert.IsTrue (count == restricted_response_request.Length, "length");
596                 }
597
598                 [Test]
599                 public void IsRestricted_2_0_RequestResponse ()
600                 {
601                         int count = 0;
602                         foreach (string str in request_headers) {
603                                 if (WebHeaderCollection.IsRestricted (str, true)) {
604                                         Assert.IsTrue (Array.IndexOf (restricted_request_response, str) != -1, "restricted " + str);
605                                         count++;
606                                 } else {
607                                         Assert.IsTrue (Array.IndexOf (restricted_request_response, str) == -1, str);
608                                 }
609                         }
610                         Assert.IsTrue (count == restricted_request_response.Length, "length");
611                 }
612
613                 [Test]
614                 public void IsRestricted_2_0_ResponseResponse ()
615                 {
616                         int count = 0;
617                         foreach (string str in response_headers) {
618                                 if (WebHeaderCollection.IsRestricted (str, true)) {
619                                         Assert.IsTrue (Array.IndexOf (restricted_response_response, str) != -1, "restricted " + str);
620                                         count++;
621                                 } else {
622                                         Assert.IsTrue (Array.IndexOf (restricted_response_response, str) == -1, str);
623                                 }
624                         }
625                         Assert.IsTrue (count == restricted_response_response.Length, "length");
626                 }
627 #endif
628         }
629 }
630