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;
26 namespace MonoTests.System.Net
29 public class WebHeaderCollectionTest
31 WebHeaderCollection col;
34 public void GetReady ()
36 col = new WebHeaderCollection ();
37 col.Add ("Name1: Value1");
38 col.Add ("Name2: Value2");
47 } catch (ArgumentNullException) { }
51 } catch (ArgumentException) { }
55 } catch (ArgumentException) { }
59 } catch (ArgumentException) { }
63 } catch (ArgumentException) { }
66 col.Add ("XHost: foo");
67 } catch (ArgumentException) {
73 col.Add ("XHost" + ((char) 0xa9) + ": foo");
75 } catch (ArgumentException) { }
77 col.Add ("XHost: foo" + (char) 0xa9);
78 } catch (ArgumentException) {
82 col.Add ("XHost: foo" + (char) 0x7f);
84 } catch (ArgumentException) {
89 } catch (ArgumentException) {
93 col.Add ("XHost", null);
94 } catch (ArgumentException) {
99 } catch (ArgumentException) {
105 public void AddRequestHeader ()
107 col.Add (HttpRequestHeader.Host, "hh");
110 col.Add (HttpResponseHeader.Age, "aa");
112 } catch (InvalidOperationException) {
117 public void AddRestrictedDisabled ()
119 col.Add ("Accept", "aa");
120 col.Add ("Content-Length", "bb");
121 col.Add ("Keep-Alive", "cc");
122 col.Add ("If-Modified-Since", "dd");
123 col.Add ("aaany", null);
127 #if FEATURE_NO_BSD_SOCKETS
128 [ExpectedException (typeof (PlatformNotSupportedException))]
130 public void AddRestricted ()
132 col = CreateRestrictedHeaders ();
135 col.Add ("Accept", "cc");
137 } catch (ArgumentException) {
141 col.Add (HttpRequestHeader.Host, "dd");
143 } catch (ArgumentException) {
148 public void GetValues ()
150 WebHeaderCollection w = new WebHeaderCollection ();
151 w.Add ("Hello", "H1");
152 w.Add ("Hello", "H2");
153 w.Add ("Hello", "H3,H4");
155 string [] sa = w.GetValues ("Hello");
156 Assert.AreEqual (3, sa.Length, "#1");
157 Assert.AreEqual ("H1,H2,H3,H4", w.Get ("Hello"), "#2");
159 w = new WebHeaderCollection ();
160 w.Add ("Accept", "H1");
161 w.Add ("Accept", "H2");
162 w.Add ("Accept", "H3, H4 ");
163 Assert.AreEqual (3, w.GetValues (0).Length, "#3a");
164 Assert.AreEqual (4, w.GetValues ("Accept").Length, "#3b");
165 Assert.AreEqual ("H4", w.GetValues ("Accept")[3], "#3c");
166 Assert.AreEqual ("H1,H2,H3, H4", w.Get ("Accept"), "#4");
168 w = new WebHeaderCollection ();
169 w.Add ("Allow", "H1");
170 w.Add ("Allow", "H2");
171 w.Add ("Allow", "H3,H4");
172 sa = w.GetValues ("Allow");
173 Assert.AreEqual (4, sa.Length, "#5");
174 Assert.AreEqual ("H1,H2,H3,H4", w.Get ("Allow"), "#6");
176 w = new WebHeaderCollection ();
177 w.Add ("AUTHorization", "H1, H2, H3");
178 sa = w.GetValues ("authorization");
179 Assert.AreEqual (3, sa.Length, "#9");
181 w = new WebHeaderCollection ();
182 w.Add ("proxy-authenticate", "H1, H2, H3");
183 sa = w.GetValues ("Proxy-Authenticate");
184 Assert.AreEqual (3, sa.Length, "#9");
186 w = new WebHeaderCollection ();
187 w.Add ("expect", "H1,\tH2, H3 ");
188 sa = w.GetValues ("EXPECT");
189 Assert.AreEqual (3, sa.Length, "#10");
190 Assert.AreEqual ("H2", sa [1], "#11");
191 Assert.AreEqual ("H3", sa [2], "#12");
196 } catch (ArgumentNullException) { }
197 Assert.AreEqual (null, w.GetValues (""), "#14");
198 Assert.AreEqual (null, w.GetValues ("NotExistent"), "#15");
200 w = new WebHeaderCollection ();
201 w.Add ("Accept", null);
202 Assert.AreEqual (1, w.GetValues ("Accept").Length, "#16");
204 w = new WebHeaderCollection ();
205 w.Add ("Accept", ",,,");
206 Assert.AreEqual (3, w.GetValues ("Accept").Length, "#17");
210 public void GetValuesForMultipleHeaderManyLines ()
212 WebHeaderCollection w = new WebHeaderCollection ();
213 w.Add ("Pragma", "H1, H2");
214 w.Add ("Pragma", "H3");
215 Assert.AreEqual (3, w.GetValues ("Pragma").Length, "#1");
219 public void Indexers ()
221 Assert.AreEqual ("Value1", ((NameValueCollection)col)[0], "#1.1");
222 WebHeaderCollection w = new WebHeaderCollection ();
223 w [HttpRequestHeader.CacheControl] = "Value2";
224 Assert.AreEqual ("Value2", w[HttpRequestHeader.CacheControl], "#1.2");
227 w[HttpResponseHeader.Pragma] = "Value3";
228 Assert.Fail ("#1.3");
229 } catch (InvalidOperationException) {
234 public void Remove ()
236 col.Remove ("Name1");
237 col.Remove ("NameNotExist");
238 Assert.AreEqual (1, col.Count, "#1");
242 #if FEATURE_NO_BSD_SOCKETS
243 [ExpectedException (typeof (PlatformNotSupportedException))]
245 public void RemoveRestricted ()
247 col = CreateRestrictedHeaders ();
250 col.Add ("Host", "foo");
252 Assert.Fail ("#2: should fail according to spec");
253 } catch (ArgumentException) {}
259 col.Add ("Name1", "Value1b");
260 col.Set ("Name1", "\t X \t");
261 Assert.AreEqual ("X", col.Get ("Name1"), "#1");
265 public void IsRestricted ()
267 Assert.IsTrue (!WebHeaderCollection.IsRestricted ("Xhost"), "#1");
268 Assert.IsTrue (WebHeaderCollection.IsRestricted ("Host"), "#2");
269 Assert.IsTrue (WebHeaderCollection.IsRestricted ("HOST"), "#3");
270 Assert.IsTrue (WebHeaderCollection.IsRestricted ("Transfer-Encoding"), "#4");
271 Assert.IsTrue (WebHeaderCollection.IsRestricted ("user-agent"), "#5");
272 Assert.IsTrue (WebHeaderCollection.IsRestricted ("accept"), "#6");
273 Assert.IsTrue (!WebHeaderCollection.IsRestricted ("accept-charset"), "#7");
277 public void ToStringTest ()
279 col.Add ("Name1", "Value1b");
280 col.Add ("Name3", "Value3a\r\n Value3b");
281 col.Add ("Name4", " Value4 ");
282 Assert.AreEqual ("Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString (), "#1");
283 WebHeaderCollection w;
284 w = new WebHeaderCollection ();
285 w.Add (HttpResponseHeader.KeepAlive, "Value1");
286 w.Add (HttpResponseHeader.WwwAuthenticate, "Value2");
287 Assert.AreEqual ("Keep-Alive: Value1\r\nWWW-Authenticate: Value2\r\n\r\n", w.ToString (), "#2");
288 w = new WebHeaderCollection ();
289 w.Add (HttpRequestHeader.UserAgent, "Value1");
290 w.Add (HttpRequestHeader.ContentMd5, "Value2");
291 Assert.AreEqual ("User-Agent: Value1\r\nContent-MD5: Value2\r\n\r\n", w.ToString (), "#3");
295 public void GetObjectData ()
297 SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
298 new FormatterConverter ());
300 WebHeaderCollection headers = new WebHeaderCollection ();
301 headers.Add ("Content-Type", "image/png");
302 headers.Add ("No-Cache:off");
303 headers.Add ("Disposition", "attach");
305 ((ISerializable) headers).GetObjectData (si, new StreamingContext ());
306 Assert.AreEqual (7, si.MemberCount, "#A");
308 foreach (SerializationEntry entry in si) {
309 Assert.IsNotNull (entry.Name, "#B1:" + i);
310 Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
311 Assert.IsNotNull (entry.Value, "#B3:" + i);
315 Assert.AreEqual ("Count", entry.Name, "#B4:" + i);
316 Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
317 Assert.AreEqual (3, entry.Value, "#B6:" + i);
320 Assert.AreEqual ("0", entry.Name, "#B4:" + i);
321 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
322 Assert.AreEqual ("Content-Type", entry.Value, "#B6:" + i);
325 Assert.AreEqual ("3", entry.Name, "#B4:" + i);
326 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
327 Assert.AreEqual ("image/png", entry.Value, "#B6:" + i);
330 Assert.AreEqual ("1", entry.Name, "#B4:" + i);
331 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
332 Assert.AreEqual ("No-Cache", entry.Value, "#B6:" + i);
335 Assert.AreEqual ("4", entry.Name, "#B4:" + i);
336 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
337 Assert.AreEqual ("off", entry.Value, "#B6:" + i);
340 Assert.AreEqual ("2", entry.Name, "#B4:" + i);
341 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
342 Assert.AreEqual ("Disposition", entry.Value, "#B6:" + i);
345 Assert.AreEqual ("5", entry.Name, "#B4:" + i);
346 Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
347 Assert.AreEqual ("attach", entry.Value, "#B6:" + i);
355 public void Serialize ()
357 WebHeaderCollection headers = new WebHeaderCollection ();
358 headers.Add ("Content-Type", "image/png");
359 headers.Add ("No-Cache:off");
360 headers.Add ("Disposition", "attach");
362 BinaryFormatter bf = new BinaryFormatter ();
363 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
365 MemoryStream ms = new MemoryStream ();
366 bf.Serialize (ms, headers);
369 byte [] buffer = new byte [ms.Length];
370 ms.Read (buffer, 0, buffer.Length);
371 Assert.AreEqual (_serialized, buffer);
375 public void Deserialize ()
377 MemoryStream ms = new MemoryStream ();
378 ms.Write (_serialized, 0, _serialized.Length);
381 BinaryFormatter bf = new BinaryFormatter ();
382 WebHeaderCollection headers = (WebHeaderCollection) bf.Deserialize (ms);
385 private static readonly byte [] _serialized = new byte [] {
386 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
387 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
388 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
390 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x35,
392 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x34, 0x2e, 0x30, 0x2e, 0x30,
394 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
395 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
396 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
398 0x65, 0x6e, 0x3d, 0x37, 0x63, 0x65, 0x63, 0x38, 0x35, 0x64, 0x37,
399 0x62, 0x65, 0x61, 0x37, 0x37, 0x39, 0x38, 0x65, 0x05, 0x01, 0x00,
401 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
402 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
404 0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
405 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
406 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
407 0x07, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
408 0x30, 0x01, 0x33, 0x01, 0x31, 0x01, 0x34, 0x01, 0x32, 0x01, 0x35,
409 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x08, 0x02, 0x00, 0x00,
410 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0c,
411 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
412 0x65, 0x06, 0x04, 0x00, 0x00, 0x00, 0x09, 0x69, 0x6d, 0x61, 0x67,
413 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06, 0x05, 0x00, 0x00, 0x00, 0x08,
414 0x4e, 0x6f, 0x2d, 0x43, 0x61, 0x63, 0x68, 0x65, 0x06, 0x06, 0x00,
415 0x00, 0x00, 0x03, 0x6f, 0x66, 0x66, 0x06, 0x07, 0x00, 0x00, 0x00,
416 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
417 0x6e, 0x06, 0x08, 0x00, 0x00, 0x00, 0x06, 0x61, 0x74, 0x74, 0x61,
422 public void IsRestricted_InvalidChars_1 ()
434 int [] singles = new int [] { 34, 44, 47, 123, 125 };
435 foreach (int single in singles) {
437 WebHeaderCollection.IsRestricted (new string ((char) single, 1));
438 Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
439 } catch (ArgumentException) {
442 for (int i = 0; i <= 32; i++) {
444 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
445 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
446 } catch (ArgumentException) {
449 for (int i = 39; i <= 41; i++) {
451 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
452 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
453 } catch (ArgumentException) {
456 for (int i = 91; i <= 93; i++) {
458 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
459 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
460 } catch (ArgumentException) {
463 for (int i = 127; i <= 255; i++) {
465 WebHeaderCollection.IsRestricted (new string ((char) i, 1));
466 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
467 } catch (ArgumentException) {
473 public void IsRestricted_InvalidChars_Request_2 ()
485 int [] singles = new int [] { 34, 44, 47, 123, 125 };
486 foreach (int single in singles) {
488 WebHeaderCollection.IsRestricted (new string ((char) single, 1), false);
489 Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
490 } catch (ArgumentException) {
493 for (int i = 0; i <= 32; i++) {
495 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
496 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
497 } catch (ArgumentException) {
500 for (int i = 39; i <= 41; i++) {
502 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
503 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
504 } catch (ArgumentException) {
507 for (int i = 91; i <= 93; i++) {
509 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
510 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
511 } catch (ArgumentException) {
514 for (int i = 127; i <= 255; i++) {
516 WebHeaderCollection.IsRestricted (new string ((char) i, 1), false);
517 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
518 } catch (ArgumentException) {
524 public void IsRestricted_InvalidChars_Response_2 ()
536 int [] singles = new int [] { 34, 44, 47, 123, 125 };
537 foreach (int single in singles) {
539 WebHeaderCollection.IsRestricted (new string ((char) single, 1), true);
540 Assert.Fail (String.Format ("{0}: {1}", single, (char) single));
541 } catch (ArgumentException) {
544 for (int i = 0; i <= 32; i++) {
546 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
547 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
548 } catch (ArgumentException) {
551 for (int i = 39; i <= 41; i++) {
553 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
554 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
555 } catch (ArgumentException) {
558 for (int i = 91; i <= 93; i++) {
560 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
561 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
562 } catch (ArgumentException) {
565 for (int i = 127; i <= 255; i++) {
567 WebHeaderCollection.IsRestricted (new string ((char) i, 1), true);
568 Assert.Fail (String.Format ("{0}: {1}", i, (char) i));
569 } catch (ArgumentException) {
574 static string [] request_headers = new string [] {
575 "Accept", "Accept-Charset", "Accept-Encoding", "Accept-Language", "Accept-Ranges", "Authorization",
576 "Cache-Control", "Connection", "Cookie", "Content-Length", "Content-Type", "Date",
577 "Expect", "From", "Host", "If-Match", "If-Modified-Since", "If-None-Match",
578 "If-Range", "If-Unmodified-Since", "Max-Forwards", "Pragma", "Proxy-Authorization", "Proxy-Connection",
579 "Range", "Referer", "TE", "Transfer-Encoding", "Upgrade", "User-Agent", "Via", "Warn" };
581 static string [] response_headers = new string [] {
582 "Accept-Ranges", "Age", "Allow", "Cache-Control", "Content-Encoding", "Content-Language",
583 "Content-Length", "Content-Location", "Content-Disposition", "Content-MD5", "Content-Range",
584 "Content-Type", "Date", "ETag", "Expires", "Keep-Alive", "Last-Modified", "Location", "Pragma",
585 "Proxy-Authenticate", "Retry-After", "Server", "Set-Cookie", "Trailer",
586 "Transfer-Encoding", "Vary", "Via", "Warn", "WWW-Authenticate" };
588 static string [] restricted_request_request = new string [] {
589 "Accept", "Connection", "Content-Length", "Content-Type", "Date",
590 "Expect", "Host", "If-Modified-Since", "Proxy-Connection", "Range", "Referer",
591 "Transfer-Encoding", "User-Agent" };
592 static string [] restricted_response_request = new string [] {
593 "Content-Length", "Content-Type", "Date", "Transfer-Encoding" };
595 static string [] restricted_request_response = new string [] {
596 "Content-Length", "Transfer-Encoding" };
597 static string [] restricted_response_response = new string [] {
598 "Content-Length", "Keep-Alive", "Transfer-Encoding", "WWW-Authenticate" };
601 public void IsRestricted_2_0_RequestRequest ()
604 foreach (string str in request_headers) {
605 if (WebHeaderCollection.IsRestricted (str, false)) {
606 Assert.IsTrue (Array.IndexOf (restricted_request_request, str) != -1, "restricted " + str);
609 Assert.IsTrue (Array.IndexOf (restricted_request_request, str) == -1, str);
612 Assert.IsTrue (count == restricted_request_request.Length, "req-req length");
616 public void IsRestricted_2_0_ResponseRequest ()
619 foreach (string str in response_headers) {
620 if (WebHeaderCollection.IsRestricted (str, false)) {
621 Assert.IsTrue (Array.IndexOf (restricted_response_request, str) != -1, "restricted " + str);
624 Assert.IsTrue (Array.IndexOf (restricted_response_request, str) == -1, str);
627 Assert.IsTrue (count == restricted_response_request.Length, "length");
631 public void IsRestricted_2_0_RequestResponse ()
634 foreach (string str in request_headers) {
635 if (WebHeaderCollection.IsRestricted (str, true)) {
636 Assert.IsTrue (Array.IndexOf (restricted_request_response, str) != -1, "restricted " + str);
639 Assert.IsTrue (Array.IndexOf (restricted_request_response, str) == -1, str);
642 Assert.IsTrue (count == restricted_request_response.Length, "length");
646 public void IsRestricted_2_0_ResponseResponse ()
649 foreach (string str in response_headers) {
650 if (WebHeaderCollection.IsRestricted (str, true)) {
651 Assert.IsTrue (Array.IndexOf (restricted_response_response, str) != -1, "restricted " + str);
654 Assert.IsTrue (Array.IndexOf (restricted_response_response, str) == -1, str);
657 Assert.IsTrue (count == restricted_response_response.Length, "length");
660 static WebHeaderCollection CreateRestrictedHeaders ()
662 var factory = Activator.CreateInstance (typeof (IWebRequestCreate).Assembly.GetType ("System.Net.HttpRequestCreator"), true) as IWebRequestCreate;
663 return factory.Create (new Uri ("http://localhost")).Headers;