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 public void NL_DefaultProperties ()
41 HttpListener listener = new HttpListener ();
42 HLPC coll = listener.Prefixes;
43 Assert.AreEqual (0, coll.Count, "Count");
44 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
45 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
49 public void DefaultProperties ()
51 HttpListener listener = new HttpListener ();
52 HLPC coll = listener.Prefixes;
53 coll.Add ("http://127.0.0.1:8181/");
54 Assert.AreEqual (1, coll.Count, "Count");
55 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
56 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
62 HttpListener listener = new HttpListener ();
63 HLPC coll = listener.Prefixes;
65 coll.Add ("http://127.0.0.1:8181/");
66 Assert.AreEqual (1, coll.Count, "Count");
67 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
68 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
73 public void Duplicate ()
75 HttpListener listener = new HttpListener ();
76 HLPC coll = listener.Prefixes;
77 coll.Add ("http://127.0.0.1:8181/");
78 coll.Add ("http://127.0.0.1:8181/");
80 Assert.AreEqual (1, coll.Count, "Count");
81 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
82 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
87 public void EndsWithSlash ()
89 HttpListener listener = new HttpListener ();
90 listener.Prefixes.Add ("http://127.0.0.1:7777/");
94 public void DifferentPath ()
96 HttpListener listener = new HttpListener ();
97 listener.Prefixes.Add ("http://127.0.0.1:7777/");
98 listener.Prefixes.Add ("http://127.0.0.1:7777/hola/");
99 Assert.AreEqual (2, listener.Prefixes.Count, "#01");
103 public void NL_Clear ()
105 HttpListener listener = new HttpListener ();
106 HLPC coll = listener.Prefixes;
111 public void NL_Remove ()
113 HttpListener listener = new HttpListener ();
114 HLPC coll = listener.Prefixes;
115 Assert.IsFalse (coll.Remove ("http://127.0.0.1:8181/"));
119 public void NL_RemoveBadUri ()
121 HttpListener listener = new HttpListener ();
122 HLPC coll = listener.Prefixes;
123 Assert.IsFalse (coll.Remove ("httpblah://127.0.0.1:8181/"));
127 [ExpectedException (typeof (ArgumentException))]
128 public void NL_AddBadUri ()
130 HttpListener listener = new HttpListener ();
131 HLPC coll = listener.Prefixes;
132 coll.Add ("httpblah://127.0.0.1:8181/");
136 [ExpectedException (typeof (ArgumentException))]
137 public void NoHostInUrl ()
139 HttpListener listener = new HttpListener ();
140 listener.Prefixes.Add ("http://:7777/hola/");
144 public void MultipleSlashes ()
146 // this one throws on Start(), not when adding it.
147 // See same test name in HttpListenerTest.
148 HttpListener listener = new HttpListener ();
149 HLPC coll = listener.Prefixes;
150 coll.Add ("http://localhost:7777/hola////");
151 string [] strs = new string [1];
152 coll.CopyTo (strs, 0);
153 Assert.AreEqual ("http://localhost:7777/hola////", strs [0]);
157 public void PercentSign ()
159 HttpListener listener = new HttpListener ();
160 HLPC coll = listener.Prefixes;
161 // this one throws on Start(), not when adding it.
162 // See same test name in HttpListenerTest.
163 coll.Add ("http://localhost:7777/hola%3E/");
164 string [] strs = new string [1];
165 coll.CopyTo (strs, 0);
166 Assert.AreEqual ("http://localhost:7777/hola%3E/", strs [0]);
170 public void Disposed1 ()
172 HttpListener listener = new HttpListener ();
173 HLPC coll = listener.Prefixes;
175 Assert.AreEqual (0, coll.Count, "Count");
176 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
177 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
181 [ExpectedException (typeof (ObjectDisposedException))]
182 public void Disposed2 ()
184 HttpListener listener = new HttpListener ();
185 HLPC coll = listener.Prefixes;
187 coll.Add ("http://localhost:7777/hola/");
191 [ExpectedException (typeof (ObjectDisposedException))]
192 public void Disposed3 ()
194 HttpListener listener = new HttpListener ();
195 HLPC coll = listener.Prefixes;
201 [ExpectedException (typeof (ObjectDisposedException))]
202 public void Disposed4 ()
204 HttpListener listener = new HttpListener ();
205 HLPC coll = listener.Prefixes;
207 coll.Remove ("http://localhost:7777/hola/");
211 [ExpectedException (typeof (ObjectDisposedException))]
212 public void Disposed5 ()
214 HttpListener listener = new HttpListener ();
215 HLPC coll = listener.Prefixes;
217 string [] strs = new string [0];
218 coll.CopyTo (strs, 0);
222 public void Disposed6 ()
224 HttpListener listener = new HttpListener ();
225 HLPC coll = listener.Prefixes;
228 foreach (string s in coll) {
229 a = s; // just to make the compiler happy
235 public void Disposed7 ()
237 HttpListener listener = new HttpListener ();
238 HLPC coll = listener.Prefixes;
239 coll.Add ("http://127.0.0.1/");
242 foreach (string s in coll) {
244 Assert.AreEqual (s, "http://127.0.0.1/");
246 Assert.AreEqual (items, 1);