File: WebHeaderCollectionTest.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (218 lines) | stat: -rw-r--r-- 5,864 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// WebHeaderCollectionTest.cs - NUnit Test Cases for System.Net.WebHeaderCollection
//
// Authors:
//   Lawrence Pit (loz@cable.a2000.nl)
//   Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) 2003 Martin Willemoes Hansen
//

using NUnit.Framework;
using System;
using System.Net;
using System.Collections;
using System.Collections.Specialized;

namespace MonoTests.System.Net
{

[TestFixture]
public class WebHeaderCollectionTest
{
	WebHeaderCollection col;
	
	[SetUp]
        public void GetReady () 
        {
		col = new WebHeaderCollection ();	
		col.Add ("Name1: Value1");
		col.Add ("Name2: Value2");
	}

        [Test]
        public void Add ()
        {
		try {
			col.Add (null);
			Assertion.Fail ("#1");
		} catch (ArgumentNullException) {}
		try {
			col.Add ("");
			Assertion.Fail ("#2");
		} catch (ArgumentException) {}
		try {
			col.Add ("  ");
			Assertion.Fail ("#3");
		} catch (ArgumentException) {}
		try {
			col.Add (":");
			Assertion.Fail ("#4");
		} catch (ArgumentException) {}
		try {
			col.Add (" : ");
			Assertion.Fail ("#5");
		} catch (ArgumentException) {}

		try {
			col.Add ("XHost: foo");
		} catch (ArgumentException) {
			Assertion.Fail ("#7");			
		}

		// invalid values
		try {
			col.Add ("XHost" + ((char) 0xa9) + ": foo");
			Assertion.Fail ("#8");
		} catch (ArgumentException) {}
		try {
			col.Add ("XHost: foo" + (char) 0xa9);
		} catch (ArgumentException) {
			Assertion.Fail ("#9");			
		}
		try {
			col.Add ("XHost: foo" + (char) 0x7f);
			Assertion.Fail ("#10");
		} catch (ArgumentException) {
			
		}

		try {
			col.Add ("XHost", null);
		} catch (ArgumentException) {
			Assertion.Fail ("#11");
		}		
		try {
			col.Add ("XHost:");			
		} catch (ArgumentException) {
			Assertion.Fail ("#12");			
		}				
		
		// restricted
		/*
		// this can only be tested in namespace System.Net
		try {
			WebHeaderCollection col2 = new WebHeaderCollection (true);
			col2.Add ("Host: foo");
			Assertion.Fail ("#13: should fail according to spec");
		} catch (ArgumentException) {}		
		*/
	}
	
        [Test]
	[Category ("NotWorking")]
	public void GetValues ()
	{			
		WebHeaderCollection w = new WebHeaderCollection ();
		w.Add ("Hello", "H1");
		w.Add ("Hello", "H2");
		w.Add ("Hello", "H3,H4");
		
		string [] sa = w.GetValues ("Hello");
		Assertion.AssertEquals ("#1", 3, sa.Length);
		Assertion.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");		
		Assertion.AssertEquals ("#3a", 3, w.GetValues (0).Length);
		Assertion.AssertEquals ("#3b", 4, w.GetValues ("Accept").Length);
		Assertion.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");		
		Assertion.AssertEquals ("#5", 4, sa.Length);
		Assertion.AssertEquals ("#6", "H1,H2,H3,H4", w.Get ("Allow"));

		w = new WebHeaderCollection ();
		w.Add ("AUTHorization", "H1, H2, H3");
		sa = w.GetValues ("authorization");		
		Assertion.AssertEquals ("#9", 3, sa.Length);

		w = new WebHeaderCollection ();
		w.Add ("proxy-authenticate", "H1, H2, H3");
		sa = w.GetValues ("Proxy-Authenticate");		
		Assertion.AssertEquals ("#9", 3, sa.Length);

		w = new WebHeaderCollection ();
		w.Add ("expect", "H1,\tH2,   H3  ");
		sa = w.GetValues ("EXPECT");		
		Assertion.AssertEquals ("#10", 3, sa.Length);
		Assertion.AssertEquals ("#11", "H2", sa [1]);
		Assertion.AssertEquals ("#12", "H3", sa [2]);
		
		try {
			w.GetValues (null);
			Assertion.Fail ("#13");
		} catch (ArgumentNullException) {}
		Assertion.AssertEquals ("#14", null, w.GetValues (""));
		Assertion.AssertEquals ("#15", null, w.GetValues ("NotExistent"));
	}

        [Test]
	public void Indexers ()
	{
#if NET_2_0
		Assertion.AssertEquals ("#1.1", "Value1", ((NameValueCollection)col)[0]);
		//FIXME: test also HttpRequestHeader and HttpResponseHeader
#else
		Assertion.AssertEquals ("#1", "Value1", col [0]);
#endif
		Assertion.AssertEquals ("#2", "Value1", col ["Name1"]);
		Assertion.AssertEquals ("#3", "Value1", col ["NAME1"]);
	}

	[Test]
	public void Remove ()
	{
		col.Remove ("Name1");
		col.Remove ("NameNotExist");
		Assertion.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");
			Assertion.Fail ("#2: should fail according to spec");
		} catch (ArgumentException) {}
		*/
	}

        [Test]	
	public void Set ()
	{
		col.Add ("Name1", "Value1b");
		col.Set ("Name1", "\t  X  \t");
		Assertion.AssertEquals ("#1", "X", col.Get ("Name1"));
	}
	
        [Test]
	public void IsRestricted ()
	{
		Assertion.Assert ("#1", !WebHeaderCollection.IsRestricted ("Xhost"));
		Assertion.Assert ("#2", WebHeaderCollection.IsRestricted ("Host"));
		Assertion.Assert ("#3", WebHeaderCollection.IsRestricted ("HOST"));
		Assertion.Assert ("#4", WebHeaderCollection.IsRestricted ("Transfer-Encoding"));
		Assertion.Assert ("#5", WebHeaderCollection.IsRestricted ("user-agent"));
		Assertion.Assert ("#6", WebHeaderCollection.IsRestricted ("accept"));
		Assertion.Assert ("#7", !WebHeaderCollection.IsRestricted ("accept-charset"));
	}

	[Test]
	public void ToStringTest ()
	{
		col.Add ("Name1", "Value1b");
		col.Add ("Name3", "Value3a\r\n Value3b");
		col.Add ("Name4", "   Value4   ");
		Assertion.AssertEquals ("#1", "Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString ());
	}
}

}