2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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 //
9 // (C) 2003 Martin Willemoes Hansen
10 //
11
12 using System;
13 using System.Collections;
14 using System.Collections.Specialized;
15 using System.IO;
16 using System.Net;
17 using System.Runtime.Serialization;
18 using System.Runtime.Serialization.Formatters;
19 using System.Runtime.Serialization.Formatters.Binary;
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System.Net
24 {
25         [TestFixture]
26         public class WebHeaderCollectionTest
27         {
28                 WebHeaderCollection col;
29
30                 [SetUp]
31                 public void GetReady ()
32                 {
33                         col = new WebHeaderCollection ();
34                         col.Add ("Name1: Value1");
35                         col.Add ("Name2: Value2");
36                 }
37
38                 [Test]
39                 public void Add ()
40                 {
41                         try {
42                                 col.Add (null);
43                                 Assertion.Fail ("#1");
44                         } catch (ArgumentNullException) { }
45                         try {
46                                 col.Add ("");
47                                 Assertion.Fail ("#2");
48                         } catch (ArgumentException) { }
49                         try {
50                                 col.Add ("  ");
51                                 Assertion.Fail ("#3");
52                         } catch (ArgumentException) { }
53                         try {
54                                 col.Add (":");
55                                 Assertion.Fail ("#4");
56                         } catch (ArgumentException) { }
57                         try {
58                                 col.Add (" : ");
59                                 Assertion.Fail ("#5");
60                         } catch (ArgumentException) { }
61
62                         try {
63                                 col.Add ("XHost: foo");
64                         } catch (ArgumentException) {
65                                 Assertion.Fail ("#7");
66                         }
67
68                         // invalid values
69                         try {
70                                 col.Add ("XHost" + ((char) 0xa9) + ": foo");
71                                 Assertion.Fail ("#8");
72                         } catch (ArgumentException) { }
73                         try {
74                                 col.Add ("XHost: foo" + (char) 0xa9);
75                         } catch (ArgumentException) {
76                                 Assertion.Fail ("#9");
77                         }
78                         try {
79                                 col.Add ("XHost: foo" + (char) 0x7f);
80                                 Assertion.Fail ("#10");
81                         } catch (ArgumentException) {
82
83                         }
84
85                         try {
86                                 col.Add ("XHost", null);
87                         } catch (ArgumentException) {
88                                 Assertion.Fail ("#11");
89                         }
90                         try {
91                                 col.Add ("XHost:");
92                         } catch (ArgumentException) {
93                                 Assertion.Fail ("#12");
94                         }
95
96                         // restricted
97                         /*
98                         // this can only be tested in namespace System.Net
99                         try {
100                                 WebHeaderCollection col2 = new WebHeaderCollection (true);
101                                 col2.Add ("Host: foo");
102                                 Assertion.Fail ("#13: should fail according to spec");
103                         } catch (ArgumentException) {}          
104                         */
105                 }
106
107                 [Test]
108                 [Category ("NotWorking")]
109                 public void GetValues ()
110                 {
111                         WebHeaderCollection w = new WebHeaderCollection ();
112                         w.Add ("Hello", "H1");
113                         w.Add ("Hello", "H2");
114                         w.Add ("Hello", "H3,H4");
115
116                         string [] sa = w.GetValues ("Hello");
117                         Assertion.AssertEquals ("#1", 3, sa.Length);
118                         Assertion.AssertEquals ("#2", "H1,H2,H3,H4", w.Get ("Hello"));
119
120                         w = new WebHeaderCollection ();
121                         w.Add ("Accept", "H1");
122                         w.Add ("Accept", "H2");
123                         w.Add ("Accept", "H3,H4");
124                         Assertion.AssertEquals ("#3a", 3, w.GetValues (0).Length);
125                         Assertion.AssertEquals ("#3b", 4, w.GetValues ("Accept").Length);
126                         Assertion.AssertEquals ("#4", "H1,H2,H3,H4", w.Get ("Accept"));
127
128                         w = new WebHeaderCollection ();
129                         w.Add ("Allow", "H1");
130                         w.Add ("Allow", "H2");
131                         w.Add ("Allow", "H3,H4");
132                         sa = w.GetValues ("Allow");
133                         Assertion.AssertEquals ("#5", 4, sa.Length);
134                         Assertion.AssertEquals ("#6", "H1,H2,H3,H4", w.Get ("Allow"));
135
136                         w = new WebHeaderCollection ();
137                         w.Add ("AUTHorization", "H1, H2, H3");
138                         sa = w.GetValues ("authorization");
139                         Assertion.AssertEquals ("#9", 3, sa.Length);
140
141                         w = new WebHeaderCollection ();
142                         w.Add ("proxy-authenticate", "H1, H2, H3");
143                         sa = w.GetValues ("Proxy-Authenticate");
144                         Assertion.AssertEquals ("#9", 3, sa.Length);
145
146                         w = new WebHeaderCollection ();
147                         w.Add ("expect", "H1,\tH2,   H3  ");
148                         sa = w.GetValues ("EXPECT");
149                         Assertion.AssertEquals ("#10", 3, sa.Length);
150                         Assertion.AssertEquals ("#11", "H2", sa [1]);
151                         Assertion.AssertEquals ("#12", "H3", sa [2]);
152
153                         try {
154                                 w.GetValues (null);
155                                 Assertion.Fail ("#13");
156                         } catch (ArgumentNullException) { }
157                         Assertion.AssertEquals ("#14", null, w.GetValues (""));
158                         Assertion.AssertEquals ("#15", null, w.GetValues ("NotExistent"));
159                 }
160
161                 [Test]
162                 public void Indexers ()
163                 {
164 #if NET_2_0
165                 Assertion.AssertEquals ("#1.1", "Value1", ((NameValueCollection)col)[0]);
166                 //FIXME: test also HttpRequestHeader and HttpResponseHeader
167 #else
168                         Assertion.AssertEquals ("#1", "Value1", col [0]);
169 #endif
170                         Assertion.AssertEquals ("#2", "Value1", col ["Name1"]);
171                         Assertion.AssertEquals ("#3", "Value1", col ["NAME1"]);
172                 }
173
174                 [Test]
175                 public void Remove ()
176                 {
177                         col.Remove ("Name1");
178                         col.Remove ("NameNotExist");
179                         Assertion.AssertEquals ("#1", 1, col.Count);
180
181                         /*
182                         // this can only be tested in namespace System.Net
183                         try {
184                                 WebHeaderCollection col2 = new WebHeaderCollection (true);
185                                 col2.Add ("Host", "foo");
186                                 col2.Remove ("Host");
187                                 Assertion.Fail ("#2: should fail according to spec");
188                         } catch (ArgumentException) {}
189                         */
190                 }
191
192                 [Test]
193                 public void Set ()
194                 {
195                         col.Add ("Name1", "Value1b");
196                         col.Set ("Name1", "\t  X  \t");
197                         Assertion.AssertEquals ("#1", "X", col.Get ("Name1"));
198                 }
199
200                 [Test]
201                 public void IsRestricted ()
202                 {
203                         Assertion.Assert ("#1", !WebHeaderCollection.IsRestricted ("Xhost"));
204                         Assertion.Assert ("#2", WebHeaderCollection.IsRestricted ("Host"));
205                         Assertion.Assert ("#3", WebHeaderCollection.IsRestricted ("HOST"));
206                         Assertion.Assert ("#4", WebHeaderCollection.IsRestricted ("Transfer-Encoding"));
207                         Assertion.Assert ("#5", WebHeaderCollection.IsRestricted ("user-agent"));
208                         Assertion.Assert ("#6", WebHeaderCollection.IsRestricted ("accept"));
209                         Assertion.Assert ("#7", !WebHeaderCollection.IsRestricted ("accept-charset"));
210                 }
211
212                 [Test]
213                 public void ToStringTest ()
214                 {
215                         col.Add ("Name1", "Value1b");
216                         col.Add ("Name3", "Value3a\r\n Value3b");
217                         col.Add ("Name4", "   Value4   ");
218                         Assertion.AssertEquals ("#1", "Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString ());
219                 }
220
221                 [Test]
222 #if TARGET_JVM
223                 //FIXME: include Java serialization compliant tests - the order of object
224                 // in SerializationInfo should stay same to MS format...
225                 [Ignore ("The MS compliant binary serialization is not supported")]
226 #endif                  
227                 public void GetObjectData ()
228                 {
229                         SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
230                                 new FormatterConverter ());
231
232                         WebHeaderCollection headers = new WebHeaderCollection ();
233                         headers.Add ("Content-Type", "image/png");
234                         headers.Add ("No-Cache:off");
235                         headers.Add ("Disposition", "attach");
236
237                         ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
238                         Assert.AreEqual (7, si.MemberCount, "#A");
239                         int i = 0;
240                         foreach (SerializationEntry entry in si) {
241                                 Assert.IsNotNull (entry.Name, "#B1:" + i);
242                                 Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
243                                 Assert.IsNotNull (entry.Value, "#B3:" + i);
244
245                                 switch (i) {
246                                 case 0:
247                                         Assert.AreEqual ("Count", entry.Name, "#B4:" + i);
248                                         Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
249                                         Assert.AreEqual (3, entry.Value, "#B6:" + i);
250                                         break;
251                                 case 1:
252                                         Assert.AreEqual ("0", entry.Name, "#B4:" + i);
253                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
254                                         Assert.AreEqual ("Content-Type", entry.Value, "#B6:" + i);
255                                         break;
256                                 case 2:
257                                         Assert.AreEqual ("3", entry.Name, "#B4:" + i);
258                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
259                                         Assert.AreEqual ("image/png", entry.Value, "#B6:" + i);
260                                         break;
261                                 case 3:
262                                         Assert.AreEqual ("1", entry.Name, "#B4:" + i);
263                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
264                                         Assert.AreEqual ("No-Cache", entry.Value, "#B6:" + i);
265                                         break;
266                                 case 4:
267                                         Assert.AreEqual ("4", entry.Name, "#B4:" + i);
268                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
269                                         Assert.AreEqual ("off", entry.Value, "#B6:" + i);
270                                         break;
271                                 case 5:
272                                         Assert.AreEqual ("2", entry.Name, "#B4:" + i);
273                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
274                                         Assert.AreEqual ("Disposition", entry.Value, "#B6:" + i);
275                                         break;
276                                 case 6:
277                                         Assert.AreEqual ("5", entry.Name, "#B4:" + i);
278                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
279                                         Assert.AreEqual ("attach", entry.Value, "#B6:" + i);
280                                         break;
281                                 }
282                                 i++;
283                         }
284                 }
285
286                 [Test]
287 #if TARGET_JVM
288                 //FIXME: include Java serialization compliant tests
289                 [Ignore ("The MS compliant binary serialization is not supported")]
290 #endif          
291                 public void Serialize ()
292                 {
293                         WebHeaderCollection headers = new WebHeaderCollection ();
294                         headers.Add ("Content-Type", "image/png");
295                         headers.Add ("No-Cache:off");
296                         headers.Add ("Disposition", "attach");
297
298                         BinaryFormatter bf = new BinaryFormatter ();
299                         bf.AssemblyFormat = FormatterAssemblyStyle.Full;
300
301                         MemoryStream ms = new MemoryStream ();
302                         bf.Serialize (ms, headers);
303                         ms.Position = 0;
304
305                         byte [] buffer = new byte [ms.Length];
306                         ms.Read (buffer, 0, buffer.Length);
307                         Assert.AreEqual (_serialized, buffer);
308                 }
309
310                 [Test]
311 #if TARGET_JVM
312                 //FIXME: include Java serialization compliant tests
313                 [Ignore ("The MS compliant binary serialization format is not supported")]
314 #endif                          
315                 public void Deserialize ()
316                 {
317                         MemoryStream ms = new MemoryStream ();
318                         ms.Write (_serialized, 0, _serialized.Length);
319                         ms.Position = 0;
320
321                         BinaryFormatter bf = new BinaryFormatter ();
322                         WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
323                 }
324
325                 private static readonly byte [] _serialized = new byte [] {
326 #if NET_2_0
327                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
328                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
329                         0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
330                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
331                         0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
332                         0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
333                         0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
334                         0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
335                         0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
336                         0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
337                         0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
338                         0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
339                         0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
340                         0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
341                         0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
342                         0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
343                         0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
344                         0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
345                         0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
346                         0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
347                         0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
348                         0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
349                         0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
350                         0x63, 0x68, 0x0b
351 #else
352                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
353                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
354                         0x4c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
355                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31, 0x2e, 0x30, 0x2e, 0x35,
356                         0x30, 0x30, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
357                         0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
358                         0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
359                         0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
360                         0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
361                         0x05, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65,
362                         0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65,
363                         0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74,
364                         0x69, 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75,
365                         0x6e, 0x74, 0x01, 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01,
366                         0x32, 0x01, 0x35, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08,
367                         0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00,
368                         0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
369                         0x54, 0x79, 0x70, 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69,
370                         0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00,
371                         0x00, 0x00, 0x08, 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65,
372                         0x06, 0x06, 0x00, 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07,
373                         0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69,
374                         0x74, 0x69, 0x6f, 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61,
375                         0x74, 0x74, 0x61, 0x63, 0x68, 0x0b
376 #endif
377                 };
378         }
379 }