| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // WebHeaderCollectionTest.cs - NUnit Test Cases for System.Net.WebHeaderCollection
- //
- // Author:
- // Lawrence Pit ([email protected])
- //
- using NUnit.Framework;
- using System;
- using System.Net;
- using System.Collections;
- namespace MonoTests.System.Net
- {
- public class WebHeaderCollectionTest : TestCase
- {
- WebHeaderCollection col;
-
- public WebHeaderCollectionTest () :
- base ("[MonoTests.System.Net.WebHeaderCollectionTest]") {}
- public WebHeaderCollectionTest (string name) : base (name) {}
- protected override void SetUp ()
- {
- col = new WebHeaderCollection ();
- col.Add ("Name1: Value1");
- col.Add ("Name2: Value2");
- }
- protected override void TearDown () {}
- public static ITest Suite
- {
- get {
- return new TestSuite (typeof (WebHeaderCollectionTest));
- }
- }
-
- public void TestAdd ()
- {
- try {
- col.Add (null);
- Fail ("#1");
- } catch (ArgumentNullException) {}
- try {
- col.Add ("");
- Fail ("#2");
- } catch (ArgumentException) {}
- try {
- col.Add (" ");
- Fail ("#3");
- } catch (ArgumentException) {}
- try {
- col.Add (":");
- Fail ("#4");
- } catch (ArgumentException) {}
- try {
- col.Add (" : ");
- Fail ("#5");
- } catch (ArgumentException) {}
- try {
- col.Add ("XHost: foo");
- } catch (ArgumentException) {
- Fail ("#7");
- }
- // invalid values
- try {
- col.Add ("XHost" + ((char) 0xa9) + ": foo");
- Fail ("#8");
- } catch (ArgumentException) {}
- try {
- col.Add ("XHost: foo" + (char) 0xa9);
- } catch (ArgumentException) {
- Fail ("#9");
- }
- try {
- col.Add ("XHost: foo" + (char) 0x7f);
- Fail ("#10");
- } catch (ArgumentException) {
-
- }
- try {
- col.Add ("XHost", null);
- } catch (ArgumentException) {
- Fail ("#11");
- }
- try {
- col.Add ("XHost:");
- } catch (ArgumentException) {
- Fail ("#12");
- }
-
- // restricted
- /*
- // this can only be tested in namespace System.Net
- try {
- WebHeaderCollection col2 = new WebHeaderCollection (true);
- col2.Add ("Host: foo");
- Fail ("#13: should fail according to spec");
- } catch (ArgumentException) {}
- */
- }
-
- public void TestGetValues ()
- {
- WebHeaderCollection w = new WebHeaderCollection ();
- w.Add ("Hello", "H1");
- w.Add ("Hello", "H2");
- w.Add ("Hello", "H3,H4");
-
- string [] sa = w.GetValues ("Hello");
- AssertEquals ("#1", 3, sa.Length);
- AssertEquals ("#2", "H1,H2,H3,H4", w.Get ("Hello"));
- w = new WebHeaderCollection ();
- w.Add ("Accept", "H1");
- w.Add ("Accept", "H2");
- w.Add ("Accept", "H3,H4");
- AssertEquals ("#3a", 3, w.GetValues (0).Length);
- AssertEquals ("#3b", 4, w.GetValues ("Accept").Length);
- AssertEquals ("#4", "H1,H2,H3,H4", w.Get ("Accept"));
- w = new WebHeaderCollection ();
- w.Add ("Allow", "H1");
- w.Add ("Allow", "H2");
- w.Add ("Allow", "H3,H4");
- sa = w.GetValues ("Allow");
- AssertEquals ("#5", 4, sa.Length);
- AssertEquals ("#6", "H1,H2,H3,H4", w.Get ("Allow"));
- w = new WebHeaderCollection ();
- w.Add ("AUTHorization", "H1, H2, H3");
- sa = w.GetValues ("authorization");
- AssertEquals ("#9", 3, sa.Length);
- w = new WebHeaderCollection ();
- w.Add ("proxy-authenticate", "H1, H2, H3");
- sa = w.GetValues ("Proxy-Authenticate");
- AssertEquals ("#9", 3, sa.Length);
- w = new WebHeaderCollection ();
- w.Add ("expect", "H1,\tH2, H3 ");
- sa = w.GetValues ("EXPECT");
- AssertEquals ("#10", 3, sa.Length);
- AssertEquals ("#11", "H2", sa [1]);
- AssertEquals ("#12", "H3", sa [2]);
-
- try {
- w.GetValues (null);
- Fail ("#13");
- } catch (ArgumentNullException) {}
- AssertEquals ("#14", null, w.GetValues (""));
- AssertEquals ("#15", null, w.GetValues ("NotExistent"));
- }
-
- public void TestIndexers ()
- {
- AssertEquals ("#1", "Value1", col [0]);
- AssertEquals ("#2", "Value1", col ["Name1"]);
- AssertEquals ("#3", "Value1", col ["NAME1"]);
- }
-
- public void TestRemove ()
- {
- col.Remove ("Name1");
- col.Remove ("NameNotExist");
- AssertEquals ("#1", 1, col.Count);
-
- /*
- // this can only be tested in namespace System.Net
- try {
- WebHeaderCollection col2 = new WebHeaderCollection (true);
- col2.Add ("Host", "foo");
- col2.Remove ("Host");
- Fail ("#2: should fail according to spec");
- } catch (ArgumentException) {}
- */
- }
-
- public void TestSet ()
- {
- col.Add ("Name1", "Value1b");
- col.Set ("Name1", "\t X \t");
- AssertEquals ("#1", "X", col.Get ("Name1"));
- }
-
- public void TestIsRestricted ()
- {
- Assert ("#1", !WebHeaderCollection.IsRestricted ("Xhost"));
- Assert ("#2", WebHeaderCollection.IsRestricted ("Host"));
- Assert ("#3", WebHeaderCollection.IsRestricted ("HOST"));
- Assert ("#4", WebHeaderCollection.IsRestricted ("Transfer-Encoding"));
- Assert ("#5", WebHeaderCollection.IsRestricted ("user-agent"));
- Assert ("#6", WebHeaderCollection.IsRestricted ("accept"));
- Assert ("#7", !WebHeaderCollection.IsRestricted ("accept-charset"));
- }
-
- public void TestToString ()
- {
- col.Add ("Name1", "Value1b");
- col.Add ("Name3", "Value3a\r\n Value3b");
- col.Add ("Name4", " Value4 ");
- AssertEquals ("#1", "Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString ());
- }
- }
- }
|