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");
43 Assertion.Fail ("#1");
44 } catch (ArgumentNullException) { }
47 Assertion.Fail ("#2");
48 } catch (ArgumentException) { }
51 Assertion.Fail ("#3");
52 } catch (ArgumentException) { }
55 Assertion.Fail ("#4");
56 } catch (ArgumentException) { }
59 Assertion.Fail ("#5");
60 } catch (ArgumentException) { }
63 col.Add ("XHost: foo");
64 } catch (ArgumentException) {
65 Assertion.Fail ("#7");
70 col.Add ("XHost" + ((char) 0xa9) + ": foo");
71 Assertion.Fail ("#8");
72 } catch (ArgumentException) { }
74 col.Add ("XHost: foo" + (char) 0xa9);
75 } catch (ArgumentException) {
76 Assertion.Fail ("#9");
79 col.Add ("XHost: foo" + (char) 0x7f);
80 Assertion.Fail ("#10");
81 } catch (ArgumentException) {
86 col.Add ("XHost", null);
87 } catch (ArgumentException) {
88 Assertion.Fail ("#11");
92 } catch (ArgumentException) {
93 Assertion.Fail ("#12");
98 // this can only be tested in namespace System.Net
100 WebHeaderCollection col2 = new WebHeaderCollection (true);
101 col2.Add ("Host: foo");
102 Assertion.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 Assertion.AssertEquals ("#1", 3, sa.Length);
118 Assertion.AssertEquals ("#2", "H1,H2,H3,H4", w.Get ("Hello"));
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"));
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"));
136 w = new WebHeaderCollection ();
137 w.Add ("AUTHorization", "H1, H2, H3");
138 sa = w.GetValues ("authorization");
139 Assertion.AssertEquals ("#9", 3, sa.Length);
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);
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]);
155 Assertion.Fail ("#13");
156 } catch (ArgumentNullException) { }
157 Assertion.AssertEquals ("#14", null, w.GetValues (""));
158 Assertion.AssertEquals ("#15", null, w.GetValues ("NotExistent"));
162 public void Indexers ()
165 Assertion.AssertEquals ("#1.1", "Value1", ((NameValueCollection)col)[0]);
166 //FIXME: test also HttpRequestHeader and HttpResponseHeader
168 Assertion.AssertEquals ("#1", "Value1", col [0]);
170 Assertion.AssertEquals ("#2", "Value1", col ["Name1"]);
171 Assertion.AssertEquals ("#3", "Value1", col ["NAME1"]);
175 public void Remove ()
177 col.Remove ("Name1");
178 col.Remove ("NameNotExist");
179 Assertion.AssertEquals ("#1", 1, col.Count);
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 Assertion.Fail ("#2: should fail according to spec");
188 } catch (ArgumentException) {}
195 col.Add ("Name1", "Value1b");
196 col.Set ("Name1", "\t X \t");
197 Assertion.AssertEquals ("#1", "X", col.Get ("Name1"));
201 public void IsRestricted ()
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"));
213 public void ToStringTest ()
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 ());
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