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)
8 // Gonzalo Paniagua Javier (gonzalo@novell.com)
9 // Marek Safar <marek.safar@gmail.com>
11 // (C) 2003 Martin Willemoes Hansen
15 using System.Collections;
16 using System.Collections.Specialized;
19 using System.Runtime.Serialization;
20 using System.Runtime.Serialization.Formatters;
21 using System.Runtime.Serialization.Formatters.Binary;
23 using NUnit.Framework;
25 namespace MonoTests.System.Net
28 public class WebHeaderCollectionTest
30 WebHeaderCollection col;
33 public void GetReady ()
35 col = new WebHeaderCollection ();
36 col.Add ("Name1: Value1");
37 col.Add ("Name2: Value2");
46 } catch (ArgumentNullException) { }
50 } catch (ArgumentException) { }
54 } catch (ArgumentException) { }
58 } catch (ArgumentException) { }
62 } catch (ArgumentException) { }
65 col.Add ("XHost: foo");
66 } catch (ArgumentException) {
72 col.Add ("XHost" + ((char) 0xa9) + ": foo");
74 } catch (ArgumentException) { }
76 col.Add ("XHost: foo" + (char) 0xa9);
77 } catch (ArgumentException) {
81 col.Add ("XHost: foo" + (char) 0x7f);
83 } catch (ArgumentException) {
88 } catch (ArgumentException) {
92 col.Add ("XHost", null);
93 } catch (ArgumentException) {
98 } catch (ArgumentException) {
104 public void AddRequestHeader ()
106 col.Add (HttpRequestHeader.Host, "hh");
109 col.Add (HttpResponseHeader.Age, "aa");
111 } catch (InvalidOperationException) {
116 public void AddRestrictedDisabled ()
118 col.Add ("Accept", "aa");
119 col.Add ("Content-Length", "bb");
120 col.Add ("Keep-Alive", "cc");
121 col.Add ("If-Modified-Since", "dd");
122 col.Add ("aaany", null);
126 public void AddRestricted ()
128 col = CreateRestrictedHeaders ();
131 col.Add ("Accept", "cc");
133 } catch (ArgumentException) {
137 col.Add (HttpRequestHeader.Host, "dd");
139 } catch (ArgumentException) {
144 public void GetValues ()
146 WebHeaderCollection w = new WebHeaderCollection ();
147 w.Add ("Hello", "H1");
148 w.Add ("Hello", "H2");
149 w.Add ("Hello", "H3,H4");
151 string [] sa = w.GetValues ("Hello");
152 Assert.AreEqual (3, sa.Length, "#1");
153 Assert.AreEqual ("H1,H2,H3,H4", w.Get ("Hello"), "#2");
155 w = new WebHeaderCollection ();
156 w.Add ("Accept", "H1");
157 w.Add ("Accept", "H2");
158 w.Add ("Accept", "H3, H4 ");
159 Assert.AreEqual (3, w.GetValues (0).Length, "#3a");
160 Assert.AreEqual (4, w.GetValues ("Accept").Length, "#3b");
161 Assert.AreEqual ("H4", w.GetValues ("Accept")[3], "#3c");
162 Assert.AreEqual ("H1,H2,H3, H4", w.Get ("Accept"), "#4");
164 w = new WebHeaderCollection ();
165 w.Add ("Allow", "H1");
166 w.Add ("Allow", "H2");
167 w.Add ("Allow", "H3,H4");
168 sa = w.GetValues ("Allow");
169 Assert.AreEqual (4, sa.Length, "#5");
170 Assert.AreEqual ("H1,H2,H3,H4", w.Get ("Allow"), "#6");
172 w = new WebHeaderCollection ();
173 w.Add ("AUTHorization", "H1, H2, H3");
174 sa = w.GetValues ("authorization");
175 Assert.AreEqual (3, sa.Length, "#9");
177 w = new WebHeaderCollection ();
178 w.Add ("proxy-authenticate", "H1, H2, H3");
179 sa = w.GetValues ("Proxy-Authenticate");
180 Assert.AreEqual (3, sa.Length, "#9");
182 w = new WebHeaderCollection ();
183 w.Add ("expect", "H1,\tH2, H3 ");
184 sa = w.GetValues ("EXPECT");
185 Assert.AreEqual (3, sa.Length, "#10");
186 Assert.AreEqual ("H2", sa [1], "#11");
187 Assert.AreEqual ("H3", sa [2], "#12");
192 } catch (ArgumentNullException) { }
193 Assert.AreEqual (null, w.GetValues (""), "#14");
194 Assert.AreEqual (null, w.GetValues ("NotExistent"), "#15");
196 w = new WebHeaderCollection ();
197 w.Add ("Accept", null);
198 Assert.AreEqual (1, w.GetValues ("Accept").Length, "#16");
200 w = new WebHeaderCollection ();
201 w.Add ("Accept", ",,,");
202 Assert.AreEqual (3, w.GetValues ("Accept").Length, "#17");
206 public void Indexers ()
208 Assert.AreEqual ("Value1", ((NameValueCollection)col)[0], "#1.1");
209 WebHeaderCollection w = new WebHeaderCollection ();
210 w [HttpRequestHeader.CacheControl] = "Value2";
211 Assert.AreEqual ("Value2", w[HttpRequestHeader.CacheControl], "#1.2");
214 w[HttpResponseHeader.Pragma] = "Value3";
215 Assert.Fail ("#1.3");
216 } catch (InvalidOperationException) {
221 public void Remove ()
223 col.Remove ("Name1");
224 col.Remove ("NameNotExist");
225 Assert.AreEqual (1, col.Count, "#1");
229 public void RemoveRestricted ()
231 col = CreateRestrictedHeaders ();
234 col.Add ("Host", "foo");
236 Assert.Fail ("#2: should fail according to spec");
237 } catch (ArgumentException) {}
243 col.Add ("Name1", "Value1b");
244 col.Set ("Name1", "\t X \t");
245 Assert.AreEqual ("X", col.Get ("Name1"), "#1");
249 public void IsRestricted ()
251 Assert.IsTrue (!WebHeaderCollection.IsRestricted ("Xhost"), "#1");
252 Assert.IsTrue (WebHeaderCollection.IsRestricted ("Host"), "#2");
253 Assert.IsTrue (WebHeaderCollection.IsRestricted ("HOST"), "#3");
254 Assert.IsTrue (WebHeaderCollection.IsRestricted ("Transfer-Encoding"), "#4");
255 Assert.IsTrue (WebHeaderCollection.IsRestricted ("user-agent"), "#5");
256 Assert.IsTrue (WebHeaderCollection.IsRestricted ("accept"), "#6");
257 Assert.IsTrue (!WebHeaderCollection.IsRestricted ("accept-charset"), "#7");
261 public void ToStringTest ()
263 col.Add ("Name1", "Value1b");
264 col.Add ("Name3", "Value3a\r\n Value3b");
265 col.Add ("Name4", " Value4 ");
266 Assert.AreEqual ("Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString (), "#1");
267 WebHeaderCollection w;
268 w = new WebHeaderCollection ();
269 w.Add (HttpResponseHeader.KeepAlive, "Value1");
270 w.Add (HttpResponseHeader.WwwAuthenticate, "Value2");
271 Assert.AreEqual ("Keep-Alive: Value1\r\nWWW-Authenticate: Value2\r\n\r\n", w.ToString (), "#2");
272 w = new WebHeaderCollection ();
273 w.Add (HttpRequestHeader.UserAgent, "Value1");
274 w.Add (HttpRequestHeader.ContentMd5, "Value2");
275 Assert.AreEqual ("User-Agent: Value1\r\nContent-MD5: Value2\r\n\r\n", w.ToString (), "#3");
280 //FIXME: include Java serialization compliant tests - the order of object
281 // in SerializationInfo should stay same to MS format...
282 [Ignore ("The MS compliant binary serialization is not supported")]
284 public void GetObjectData ()
286 SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
287 new FormatterConverter ());
289 WebHeaderCollection headers = new WebHeaderCollection ();
290 headers.Add ("Content-Type", "image/png");
291 headers.Add ("No-Cache:off");
292 headers.Add ("Disposition", "attach");
294 ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
295 Assert.AreEqual (7, si.MemberCount, "#A");
297 foreach (SerializationEntry entry in si) {
298 Assert.IsNotNull (entry.Name, "#B1:" + i);
299 Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
300 Assert.IsNotNull (entry.Value, "#B3:" + i);
304 Assert.AreEqual ("Count", entry.Name, "#B4:" + i);
305 Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
306 Assert.AreEqual (3, entry.Value, "#B6:" + i);
309 Assert.AreEqual ("0", entry.Name, "#B4:" + i);
310 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
311 Assert.AreEqual ("Content-Type", entry.Value, "#B6:" + i);
314 Assert.AreEqual ("3", entry.Name, "#B4:" + i);
315 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
316 Assert.AreEqual ("image/png", entry.Value, "#B6:" + i);
319 Assert.AreEqual ("1", entry.Name, "#B4:" + i);
320 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
321 Assert.AreEqual ("No-Cache", entry.Value, "#B6:" + i);
324 Assert.AreEqual ("4", entry.Name, "#B4:" + i);
325 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
326 Assert.AreEqual ("off", entry.Value, "#B6:" + i);
329 Assert.AreEqual ("2", entry.Name, "#B4:" + i);
330 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
331 Assert.AreEqual ("Disposition", entry.Value, "#B6:" + i);
334 Assert.AreEqual ("5", entry.Name, "#B4:" + i);
335 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
336 Assert.AreEqual ("attach", entry.Value, "#B6:" + i);
345 //FIXME: include Java serialization compliant tests
346 [Ignore ("The MS compliant binary serialization is not supported")]
348 public void Serialize ()
350 WebHeaderCollection headers = new WebHeaderCollection ();
351 headers.Add ("Content-Type", "image/png");
352 headers.Add ("No-Cache:off");
353 headers.Add ("Disposition", "attach");
355 BinaryFormatter bf = new BinaryFormatter ();
356 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
358 MemoryStream ms = new MemoryStream ();
359 bf.Serialize (ms, headers);
362 byte [] buffer = new byte [ms.Length];
363 ms.Read (buffer, 0, buffer.Length);
364 Assert.AreEqual (_serialized, buffer);
369 //FIXME: include Java serialization compliant tests
370 [Ignore ("The MS compliant binary serialization format is not supported")]
372 public void Deserialize ()
374 MemoryStream ms = new MemoryStream ();
375 ms.Write (_serialized, 0, _serialized.Length);
378 BinaryFormatter bf = new BinaryFormatter ();
379 WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
382 private static readonly byte [] _serialized = new byte [] {
383 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
384 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
385 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
387 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x34, 0x2e, 0x30, 0x2e, 0x30,
389 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
391 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
392 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
393 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
394 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
395 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
396 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
397 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
398 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
399 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
400 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
401 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
402 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
403 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
404 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
405 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
406 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
407 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
408 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
409 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
414 public void IsRestricted_InvalidChars_1 ()
426 int [] singles = new int [] { 34, 44, 47, 123, 125 };
427 foreach (int single in singles) {
429 WebHeaderCollection.IsRestricted (new string ((char) single, 1));
430 Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
431 } catch (ArgumentException) {
434 for (int i = 0; i <= 32; i++) {
436 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
437 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
438 } catch (ArgumentException) {
441 for (int i = 39; i <= 41; i++) {
443 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
444 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
445 } catch (ArgumentException) {
448 for (int i = 91; i <= 93; i++) {
450 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
451 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
452 } catch (ArgumentException) {
455 for (int i = 127; i <= 255; i++) {
457 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
458 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
459 } catch (ArgumentException) {
465 public void IsRestricted_InvalidChars_Request_2 ()
477 int [] singles = new int [] { 34, 44, 47, 123, 125 };
478 foreach (int single in singles) {
480 WebHeaderCollection.IsRestricted (new string ((char) single, 1), false);
481 Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
482 } catch (ArgumentException) {
485 for (int i = 0; i <= 32; i++) {
487 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
488 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
489 } catch (ArgumentException) {
492 for (int i = 39; i <= 41; i++) {
494 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
495 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
496 } catch (ArgumentException) {
499 for (int i = 91; i <= 93; i++) {
501 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
502 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
503 } catch (ArgumentException) {
506 for (int i = 127; i <= 255; i++) {
508 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
509 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
510 } catch (ArgumentException) {
516 public void IsRestricted_InvalidChars_Response_2 ()
528 int [] singles = new int [] { 34, 44, 47, 123, 125 };
529 foreach (int single in singles) {
531 WebHeaderCollection.IsRestricted (new string ((char) single, 1), true);
532 Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
533 } catch (ArgumentException) {
536 for (int i = 0; i <= 32; i++) {
538 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
539 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
540 } catch (ArgumentException) {
543 for (int i = 39; i <= 41; i++) {
545 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
546 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
547 } catch (ArgumentException) {
550 for (int i = 91; i <= 93; i++) {
552 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
553 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
554 } catch (ArgumentException) {
557 for (int i = 127; i <= 255; i++) {
559 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
560 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
561 } catch (ArgumentException) {
566 static string [] request_headers = new string [] {
567 "Accept", "Accept-Charset", "Accept-Encoding", "Accept-Language", "Accept-Ranges", "Authorization",
568 "Cache-Control", "Connection", "Cookie", "Content-Length", "Content-Type", "Date",
569 "Expect", "From", "Host", "If-Match", "If-Modified-Since", "If-None-Match",
570 "If-Range", "If-Unmodified-Since", "Max-Forwards", "Pragma", "Proxy-Authorization", "Proxy-Connection",
571 "Range", "Referer", "TE", "Transfer-Encoding", "Upgrade", "User-Agent", "Via", "Warn" };
573 static string [] response_headers = new string [] {
574 "Accept-Ranges", "Age", "Allow", "Cache-Control", "Content-Encoding", "Content-Language",
575 "Content-Length", "Content-Location", "Content-Disposition", "Content-MD5", "Content-Range",
576 "Content-Type", "Date", "ETag", "Expires", "Keep-Alive", "Last-Modified", "Location", "Pragma",
577 "Proxy-Authenticate", "Retry-After", "Server", "Set-Cookie", "Trailer",
578 "Transfer-Encoding", "Vary", "Via", "Warn", "WWW-Authenticate" };
580 static string [] restricted_request_request = new string [] {
581 "Accept", "Connection", "Content-Length", "Content-Type", "Date",
582 "Expect", "Host", "If-Modified-Since", "Proxy-Connection", "Range", "Referer",
583 "Transfer-Encoding", "User-Agent" };
584 static string [] restricted_response_request = new string [] {
585 "Content-Length", "Content-Type", "Date", "Transfer-Encoding" };
587 static string [] restricted_request_response = new string [] {
588 "Content-Length", "Transfer-Encoding" };
589 static string [] restricted_response_response = new string [] {
590 "Content-Length", "Keep-Alive", "Transfer-Encoding", "WWW-Authenticate" };
593 public void IsRestricted_2_0_RequestRequest ()
596 foreach (string str in request_headers) {
597 if (WebHeaderCollection.IsRestricted (str, false)) {
598 Assert.IsTrue (Array.IndexOf (restricted_request_request, str) != -1, "restricted " + str);
601 Assert.IsTrue (Array.IndexOf (restricted_request_request, str) == -1, str);
604 Assert.IsTrue (count == restricted_request_request.Length, "req-req length");
608 public void IsRestricted_2_0_ResponseRequest ()
611 foreach (string str in response_headers) {
612 if (WebHeaderCollection.IsRestricted (str, false)) {
613 Assert.IsTrue (Array.IndexOf (restricted_response_request, str) != -1, "restricted " + str);
616 Assert.IsTrue (Array.IndexOf (restricted_response_request, str) == -1, str);
619 Assert.IsTrue (count == restricted_response_request.Length, "length");
623 public void IsRestricted_2_0_RequestResponse ()
626 foreach (string str in request_headers) {
627 if (WebHeaderCollection.IsRestricted (str, true)) {
628 Assert.IsTrue (Array.IndexOf (restricted_request_response, str) != -1, "restricted " + str);
631 Assert.IsTrue (Array.IndexOf (restricted_request_response, str) == -1, str);
634 Assert.IsTrue (count == restricted_request_response.Length, "length");
638 public void IsRestricted_2_0_ResponseResponse ()
641 foreach (string str in response_headers) {
642 if (WebHeaderCollection.IsRestricted (str, true)) {
643 Assert.IsTrue (Array.IndexOf (restricted_response_response, str) != -1, "restricted " + str);
646 Assert.IsTrue (Array.IndexOf (restricted_response_response, str) == -1, str);
649 Assert.IsTrue (count == restricted_response_response.Length, "length");
652 static WebHeaderCollection CreateRestrictedHeaders ()
654 var factory = Activator.CreateInstance (typeof (IWebRequestCreate).Assembly.GetType ("System.Net.HttpRequestCreator"), true) as IWebRequestCreate;
655 return factory.Create (new Uri ("http://localhost")).Headers;