2 // HttpListenerPrefixCollectionTest.cs
3 // - Unit tests for System.Net.HttpListenePrefixCollection
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
32 using HLPC=System.Net.HttpListenerPrefixCollection;
34 namespace MonoTests.System.Net {
36 public class HttpListenerPrefixCollectionTest {
37 // NL -> Not listening -> tests when listener.IsListening == false
39 #if FEATURE_NO_BSD_SOCKETS
40 [ExpectedException (typeof (PlatformNotSupportedException))]
42 public void NL_DefaultProperties ()
44 HttpListener listener = new HttpListener ();
45 HLPC coll = listener.Prefixes;
46 Assert.AreEqual (0, coll.Count, "Count");
47 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
48 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
52 #if FEATURE_NO_BSD_SOCKETS
53 [ExpectedException (typeof (PlatformNotSupportedException))]
55 public void DefaultProperties ()
57 HttpListener listener = new HttpListener ();
58 HLPC coll = listener.Prefixes;
59 coll.Add ("http://127.0.0.1:8181/");
60 Assert.AreEqual (1, coll.Count, "Count");
61 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
62 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
66 #if FEATURE_NO_BSD_SOCKETS
67 [ExpectedException (typeof (PlatformNotSupportedException))]
71 HttpListener listener = new HttpListener ();
72 HLPC coll = listener.Prefixes;
74 coll.Add ("http://127.0.0.1:8181/");
75 Assert.AreEqual (1, coll.Count, "Count");
76 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
77 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
82 #if FEATURE_NO_BSD_SOCKETS
83 [ExpectedException (typeof (PlatformNotSupportedException))]
85 public void Duplicate ()
87 HttpListener listener = new HttpListener ();
88 HLPC coll = listener.Prefixes;
89 coll.Add ("http://127.0.0.1:8181/");
90 coll.Add ("http://127.0.0.1:8181/");
92 Assert.AreEqual (1, coll.Count, "Count");
93 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
94 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
99 #if FEATURE_NO_BSD_SOCKETS
100 [ExpectedException (typeof (PlatformNotSupportedException))]
102 public void EndsWithSlash ()
104 HttpListener listener = new HttpListener ();
105 listener.Prefixes.Add ("http://127.0.0.1:7777/");
109 #if FEATURE_NO_BSD_SOCKETS
110 [ExpectedException (typeof (PlatformNotSupportedException))]
112 public void DifferentPath ()
114 HttpListener listener = new HttpListener ();
115 listener.Prefixes.Add ("http://127.0.0.1:7777/");
116 listener.Prefixes.Add ("http://127.0.0.1:7777/hola/");
117 Assert.AreEqual (2, listener.Prefixes.Count, "#01");
121 #if FEATURE_NO_BSD_SOCKETS
122 [ExpectedException (typeof (PlatformNotSupportedException))]
124 public void NL_Clear ()
126 HttpListener listener = new HttpListener ();
127 HLPC coll = listener.Prefixes;
132 #if FEATURE_NO_BSD_SOCKETS
133 [ExpectedException (typeof (PlatformNotSupportedException))]
135 public void NL_Remove ()
137 HttpListener listener = new HttpListener ();
138 HLPC coll = listener.Prefixes;
139 Assert.IsFalse (coll.Remove ("http://127.0.0.1:8181/"));
143 #if FEATURE_NO_BSD_SOCKETS
144 [ExpectedException (typeof (PlatformNotSupportedException))]
146 public void NL_RemoveBadUri ()
148 HttpListener listener = new HttpListener ();
149 HLPC coll = listener.Prefixes;
150 Assert.IsFalse (coll.Remove ("httpblah://127.0.0.1:8181/"));
154 #if FEATURE_NO_BSD_SOCKETS
155 [ExpectedException (typeof (PlatformNotSupportedException))]
157 [ExpectedException (typeof (ArgumentException))]
159 public void NL_AddBadUri ()
161 HttpListener listener = new HttpListener ();
162 HLPC coll = listener.Prefixes;
163 coll.Add ("httpblah://127.0.0.1:8181/");
167 #if FEATURE_NO_BSD_SOCKETS
168 [ExpectedException (typeof (PlatformNotSupportedException))]
170 [ExpectedException (typeof (ArgumentException))]
172 public void NoHostInUrl ()
174 HttpListener listener = new HttpListener ();
175 listener.Prefixes.Add ("http://:7777/hola/");
179 #if FEATURE_NO_BSD_SOCKETS
180 [ExpectedException (typeof (PlatformNotSupportedException))]
182 public void MultipleSlashes ()
184 // this one throws on Start(), not when adding it.
185 // See same test name in HttpListenerTest.
186 HttpListener listener = new HttpListener ();
187 HLPC coll = listener.Prefixes;
188 coll.Add ("http://localhost:7777/hola////");
189 string [] strs = new string [1];
190 coll.CopyTo (strs, 0);
191 Assert.AreEqual ("http://localhost:7777/hola////", strs [0]);
195 #if FEATURE_NO_BSD_SOCKETS
196 [ExpectedException (typeof (PlatformNotSupportedException))]
198 public void PercentSign ()
200 HttpListener listener = new HttpListener ();
201 HLPC coll = listener.Prefixes;
202 // this one throws on Start(), not when adding it.
203 // See same test name in HttpListenerTest.
204 coll.Add ("http://localhost:7777/hola%3E/");
205 string [] strs = new string [1];
206 coll.CopyTo (strs, 0);
207 Assert.AreEqual ("http://localhost:7777/hola%3E/", strs [0]);
211 #if FEATURE_NO_BSD_SOCKETS
212 [ExpectedException (typeof (PlatformNotSupportedException))]
214 public void Disposed1 ()
216 HttpListener listener = new HttpListener ();
217 HLPC coll = listener.Prefixes;
219 Assert.AreEqual (0, coll.Count, "Count");
220 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
221 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
225 #if FEATURE_NO_BSD_SOCKETS
226 [ExpectedException (typeof (PlatformNotSupportedException))]
228 [ExpectedException (typeof (ObjectDisposedException))]
230 public void Disposed2 ()
232 HttpListener listener = new HttpListener ();
233 HLPC coll = listener.Prefixes;
235 coll.Add ("http://localhost:7777/hola/");
239 #if FEATURE_NO_BSD_SOCKETS
240 [ExpectedException (typeof (PlatformNotSupportedException))]
242 [ExpectedException (typeof (ObjectDisposedException))]
244 public void Disposed3 ()
246 HttpListener listener = new HttpListener ();
247 HLPC coll = listener.Prefixes;
253 #if FEATURE_NO_BSD_SOCKETS
254 [ExpectedException (typeof (PlatformNotSupportedException))]
256 [ExpectedException (typeof (ObjectDisposedException))]
258 public void Disposed4 ()
260 HttpListener listener = new HttpListener ();
261 HLPC coll = listener.Prefixes;
263 coll.Remove ("http://localhost:7777/hola/");
267 #if FEATURE_NO_BSD_SOCKETS
268 [ExpectedException (typeof (PlatformNotSupportedException))]
270 [ExpectedException (typeof (ObjectDisposedException))]
272 public void Disposed5 ()
274 HttpListener listener = new HttpListener ();
275 HLPC coll = listener.Prefixes;
277 string [] strs = new string [0];
278 coll.CopyTo (strs, 0);
282 #if FEATURE_NO_BSD_SOCKETS
283 [ExpectedException (typeof (PlatformNotSupportedException))]
285 public void Disposed6 ()
287 HttpListener listener = new HttpListener ();
288 HLPC coll = listener.Prefixes;
291 foreach (string s in coll) {
292 a = s; // just to make the compiler happy
298 #if FEATURE_NO_BSD_SOCKETS
299 [ExpectedException (typeof (PlatformNotSupportedException))]
301 public void Disposed7 ()
303 HttpListener listener = new HttpListener ();
304 HLPC coll = listener.Prefixes;
305 coll.Add ("http://127.0.0.1/");
308 foreach (string s in coll) {
310 Assert.AreEqual (s, "http://127.0.0.1/");
312 Assert.AreEqual (items, 1);