**** Merged from MCS ****
[mono-project.git] / mcs / class / System / Test / System.Net / WebHeaderCollectionTest.cs
blobd3e86549a231c5830d73cb4c4b0685ea89bbad5d
1 //
2 // WebHeaderCollectionTest.cs - NUnit Test Cases for System.Net.WebHeaderCollection
3 //
4 // Authors:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 //
11 using NUnit.Framework;
12 using System;
13 using System.Net;
14 using System.Collections;
16 namespace MonoTests.System.Net
19 [TestFixture]
20 public class WebHeaderCollectionTest
22 WebHeaderCollection col;
24 [SetUp]
25 public void GetReady ()
27 col = new WebHeaderCollection ();
28 col.Add ("Name1: Value1");
29 col.Add ("Name2: Value2");
32 [Test]
33 public void Add ()
35 try {
36 col.Add (null);
37 Assertion.Fail ("#1");
38 } catch (ArgumentNullException) {}
39 try {
40 col.Add ("");
41 Assertion.Fail ("#2");
42 } catch (ArgumentException) {}
43 try {
44 col.Add (" ");
45 Assertion.Fail ("#3");
46 } catch (ArgumentException) {}
47 try {
48 col.Add (":");
49 Assertion.Fail ("#4");
50 } catch (ArgumentException) {}
51 try {
52 col.Add (" : ");
53 Assertion.Fail ("#5");
54 } catch (ArgumentException) {}
56 try {
57 col.Add ("XHost: foo");
58 } catch (ArgumentException) {
59 Assertion.Fail ("#7");
62 // invalid values
63 try {
64 col.Add ("XHost" + ((char) 0xa9) + ": foo");
65 Assertion.Fail ("#8");
66 } catch (ArgumentException) {}
67 try {
68 col.Add ("XHost: foo" + (char) 0xa9);
69 } catch (ArgumentException) {
70 Assertion.Fail ("#9");
72 try {
73 col.Add ("XHost: foo" + (char) 0x7f);
74 Assertion.Fail ("#10");
75 } catch (ArgumentException) {
79 try {
80 col.Add ("XHost", null);
81 } catch (ArgumentException) {
82 Assertion.Fail ("#11");
84 try {
85 col.Add ("XHost:");
86 } catch (ArgumentException) {
87 Assertion.Fail ("#12");
90 // restricted
92 // this can only be tested in namespace System.Net
93 try {
94 WebHeaderCollection col2 = new WebHeaderCollection (true);
95 col2.Add ("Host: foo");
96 Assertion.Fail ("#13: should fail according to spec");
97 } catch (ArgumentException) {}
101 [Test]
102 public void GetValues ()
104 WebHeaderCollection w = new WebHeaderCollection ();
105 w.Add ("Hello", "H1");
106 w.Add ("Hello", "H2");
107 w.Add ("Hello", "H3,H4");
109 string [] sa = w.GetValues ("Hello");
110 Assertion.AssertEquals ("#1", 3, sa.Length);
111 Assertion.AssertEquals ("#2", "H1,H2,H3,H4", w.Get ("Hello"));
113 w = new WebHeaderCollection ();
114 w.Add ("Accept", "H1");
115 w.Add ("Accept", "H2");
116 w.Add ("Accept", "H3,H4");
117 Assertion.AssertEquals ("#3a", 3, w.GetValues (0).Length);
118 Assertion.AssertEquals ("#3b", 4, w.GetValues ("Accept").Length);
119 Assertion.AssertEquals ("#4", "H1,H2,H3,H4", w.Get ("Accept"));
121 w = new WebHeaderCollection ();
122 w.Add ("Allow", "H1");
123 w.Add ("Allow", "H2");
124 w.Add ("Allow", "H3,H4");
125 sa = w.GetValues ("Allow");
126 Assertion.AssertEquals ("#5", 4, sa.Length);
127 Assertion.AssertEquals ("#6", "H1,H2,H3,H4", w.Get ("Allow"));
129 w = new WebHeaderCollection ();
130 w.Add ("AUTHorization", "H1, H2, H3");
131 sa = w.GetValues ("authorization");
132 Assertion.AssertEquals ("#9", 3, sa.Length);
134 w = new WebHeaderCollection ();
135 w.Add ("proxy-authenticate", "H1, H2, H3");
136 sa = w.GetValues ("Proxy-Authenticate");
137 Assertion.AssertEquals ("#9", 3, sa.Length);
139 w = new WebHeaderCollection ();
140 w.Add ("expect", "H1,\tH2, H3 ");
141 sa = w.GetValues ("EXPECT");
142 Assertion.AssertEquals ("#10", 3, sa.Length);
143 Assertion.AssertEquals ("#11", "H2", sa [1]);
144 Assertion.AssertEquals ("#12", "H3", sa [2]);
146 try {
147 w.GetValues (null);
148 Assertion.Fail ("#13");
149 } catch (ArgumentNullException) {}
150 Assertion.AssertEquals ("#14", null, w.GetValues (""));
151 Assertion.AssertEquals ("#15", null, w.GetValues ("NotExistent"));
154 [Test]
155 public void Indexers ()
157 Assertion.AssertEquals ("#1", "Value1", col [0]);
158 Assertion.AssertEquals ("#2", "Value1", col ["Name1"]);
159 Assertion.AssertEquals ("#3", "Value1", col ["NAME1"]);
162 [Test]
163 public void Remove ()
165 col.Remove ("Name1");
166 col.Remove ("NameNotExist");
167 Assertion.AssertEquals ("#1", 1, col.Count);
170 // this can only be tested in namespace System.Net
171 try {
172 WebHeaderCollection col2 = new WebHeaderCollection (true);
173 col2.Add ("Host", "foo");
174 col2.Remove ("Host");
175 Assertion.Fail ("#2: should fail according to spec");
176 } catch (ArgumentException) {}
180 [Test]
181 public void Set ()
183 col.Add ("Name1", "Value1b");
184 col.Set ("Name1", "\t X \t");
185 Assertion.AssertEquals ("#1", "X", col.Get ("Name1"));
188 [Test]
189 public void IsRestricted ()
191 Assertion.Assert ("#1", !WebHeaderCollection.IsRestricted ("Xhost"));
192 Assertion.Assert ("#2", WebHeaderCollection.IsRestricted ("Host"));
193 Assertion.Assert ("#3", WebHeaderCollection.IsRestricted ("HOST"));
194 Assertion.Assert ("#4", WebHeaderCollection.IsRestricted ("Transfer-Encoding"));
195 Assertion.Assert ("#5", WebHeaderCollection.IsRestricted ("user-agent"));
196 Assertion.Assert ("#6", WebHeaderCollection.IsRestricted ("accept"));
197 Assertion.Assert ("#7", !WebHeaderCollection.IsRestricted ("accept-charset"));
200 [Test]
201 public void ToStringTest ()
203 col.Add ("Name1", "Value1b");
204 col.Add ("Name3", "Value3a\r\n Value3b");
205 col.Add ("Name4", " Value4 ");
206 Assertion.AssertEquals ("#1", "Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString ());