2 // WebHeaderCollectionTest.cs - NUnit Test Cases for System.Net.WebHeaderCollection
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Gert Driesen (drieseng@users.sourceforge.net)
9 // (C) 2003 Martin Willemoes Hansen
13 using System.Collections;
14 using System.Collections.Specialized;
17 using System.Runtime.Serialization;
18 using System.Runtime.Serialization.Formatters;
19 using System.Runtime.Serialization.Formatters.Binary;
21 using NUnit.Framework;
23 namespace MonoTests.System.Net
26 public class WebHeaderCollectionTest
28 WebHeaderCollection col;
31 public void GetReady ()
33 col = new WebHeaderCollection ();
34 col.Add ("Name1: Value1");
35 col.Add ("Name2: Value2");
44 } catch (ArgumentNullException) { }
48 } catch (ArgumentException) { }
52 } catch (ArgumentException) { }
56 } catch (ArgumentException) { }
60 } catch (ArgumentException) { }
63 col.Add ("XHost: foo");
64 } catch (ArgumentException) {
70 col.Add ("XHost" + ((char) 0xa9) + ": foo");
72 } catch (ArgumentException) { }
74 col.Add ("XHost: foo" + (char) 0xa9);
75 } catch (ArgumentException) {
79 col.Add ("XHost: foo" + (char) 0x7f);
81 } catch (ArgumentException) {
86 col.Add ("XHost", null);
87 } catch (ArgumentException) {
92 } catch (ArgumentException) {
98 // this can only be tested in namespace System.Net
100 WebHeaderCollection col2 = new WebHeaderCollection (true);
101 col2.Add ("Host: foo");
102 Assert.Fail ("#13: should fail according to spec");
103 } catch (ArgumentException) {}
108 [Category ("NotWorking")]
109 public void GetValues ()
111 WebHeaderCollection w = new WebHeaderCollection ();
112 w.Add ("Hello", "H1");
113 w.Add ("Hello", "H2");
114 w.Add ("Hello", "H3,H4");
116 string [] sa = w.GetValues ("Hello");
117 Assert.AreEqual (3, sa.Length, "#1");
118 Assert.AreEqual ("H1, H2,H3,H4", w.Get ("Hello"), "#2");
120 w = new WebHeaderCollection ();
121 w.Add ("Accept", "H1");
122 w.Add ("Accept", "H2");
123 w.Add ("Accept", "H3,H4");
124 Assert.AreEqual (3, w.GetValues (0).Length, "#3a");
125 Assert.AreEqual (4, w.GetValues ("Accept").Length, "#3b");
126 Assert.AreEqual ("H1, H2,H3,H4", w.Get ("Accept"), "#4");
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 Assert.AreEqual (4, sa.Length, "#5");
134 Assert.AreEqual ("H1, H2,H3,H4", w.Get ("Allow"), "#6");
136 w = new WebHeaderCollection ();
137 w.Add ("AUTHorization", "H1, H2, H3");
138 sa = w.GetValues ("authorization");
139 Assert.AreEqual (3, sa.Length, "#9");
141 w = new WebHeaderCollection ();
142 w.Add ("proxy-authenticate", "H1, H2, H3");
143 sa = w.GetValues ("Proxy-Authenticate");
144 Assert.AreEqual (3, sa.Length, "#9");
146 w = new WebHeaderCollection ();
147 w.Add ("expect", "H1,\tH2, H3 ");
148 sa = w.GetValues ("EXPECT");
149 Assert.AreEqual (3, sa.Length, "#10");
150 Assert.AreEqual ("H2", sa [1], "#11");
151 Assert.AreEqual ("H3", sa [2], "#12");
156 } catch (ArgumentNullException) { }
157 Assert.AreEqual (null, w.GetValues (""), "#14");
158 Assert.AreEqual (null, w.GetValues ("NotExistent"), "#15");
162 public void Indexers ()
165 Assert.AreEqual ("Value1", ((NameValueCollection)col)[0], "#1.1");
166 //FIXME: test also HttpRequestHeader and HttpResponseHeader
168 Assert.AreEqual ("Value1", col [0], "#1");
170 Assert.AreEqual ("Value1", col ["Name1"], "#2");
171 Assert.AreEqual ("Value1", col ["NAME1"], "#3");
175 public void Remove ()
177 col.Remove ("Name1");
178 col.Remove ("NameNotExist");
179 Assert.AreEqual (1, col.Count, "#1");
182 // this can only be tested in namespace System.Net
184 WebHeaderCollection col2 = new WebHeaderCollection (true);
185 col2.Add ("Host", "foo");
186 col2.Remove ("Host");
187 Assert.Fail ("#2: should fail according to spec");
188 } catch (ArgumentException) {}
195 col.Add ("Name1", "Value1b");
196 col.Set ("Name1", "\t X \t");
197 Assert.AreEqual ("X", col.Get ("Name1"), "#1");
201 public void IsRestricted ()
203 Assert.IsTrue (!WebHeaderCollection.IsRestricted ("Xhost"), "#1");
204 Assert.IsTrue (WebHeaderCollection.IsRestricted ("Host"), "#2");
205 Assert.IsTrue (WebHeaderCollection.IsRestricted ("HOST"), "#3");
206 Assert.IsTrue (WebHeaderCollection.IsRestricted ("Transfer-Encoding"), "#4");
207 Assert.IsTrue (WebHeaderCollection.IsRestricted ("user-agent"), "#5");
208 Assert.IsTrue (WebHeaderCollection.IsRestricted ("accept"), "#6");
209 Assert.IsTrue (!WebHeaderCollection.IsRestricted ("accept-charset"), "#7");
213 public void ToStringTest ()
215 col.Add ("Name1", "Value1b");
216 col.Add ("Name3", "Value3a\r\n Value3b");
217 col.Add ("Name4", " Value4 ");
218 Assert.AreEqual ("Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString (), "#1");
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")]
227 public void GetObjectData ()
229 SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
230 new FormatterConverter ());
232 WebHeaderCollection headers = new WebHeaderCollection ();
233 headers.Add ("Content-Type", "image/png");
234 headers.Add ("No-Cache:off");
235 headers.Add ("Disposition", "attach");
237 ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
238 Assert.AreEqual (7, si.MemberCount, "#A");
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);
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);
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);
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);
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);
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);
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);
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);
288 //FIXME: include Java serialization compliant tests
289 [Ignore ("The MS compliant binary serialization is not supported")]
291 public void Serialize ()
293 WebHeaderCollection headers = new WebHeaderCollection ();
294 headers.Add ("Content-Type", "image/png");
295 headers.Add ("No-Cache:off");
296 headers.Add ("Disposition", "attach");
298 BinaryFormatter bf = new BinaryFormatter ();
299 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
301 MemoryStream ms = new MemoryStream ();
302 bf.Serialize (ms, headers);
305 byte [] buffer = new byte [ms.Length];
306 ms.Read (buffer, 0, buffer.Length);
307 Assert.AreEqual (_serialized, buffer);
312 //FIXME: include Java serialization compliant tests
313 [Ignore ("The MS compliant binary serialization format is not supported")]
315 public void Deserialize ()
317 MemoryStream ms = new MemoryStream ();
318 ms.Write (_serialized, 0, _serialized.Length);
321 BinaryFormatter bf = new BinaryFormatter ();
322 WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
325 private static readonly byte [] _serialized = new byte [] {
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,
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