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.
32 using NUnit.Framework;
33 using HLPC=System.Net.HttpListenerPrefixCollection;
35 namespace MonoTests.System.Net {
37 public class HttpListenerPrefixCollectionTest {
38 // NL -> Not listening -> tests when listener.IsListening == false
40 public void NL_DefaultProperties ()
42 HttpListener listener = new HttpListener ();
43 HLPC coll = listener.Prefixes;
44 Assert.AreEqual (0, coll.Count, "Count");
45 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
46 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
50 public void DefaultProperties ()
52 HttpListener listener = new HttpListener ();
53 HLPC coll = listener.Prefixes;
54 coll.Add ("http://127.0.0.1:8181/");
55 Assert.AreEqual (1, coll.Count, "Count");
56 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
57 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
63 HttpListener listener = new HttpListener ();
64 HLPC coll = listener.Prefixes;
66 coll.Add ("http://127.0.0.1:8181/");
67 Assert.AreEqual (1, coll.Count, "Count");
68 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
69 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
74 public void Duplicate ()
76 HttpListener listener = new HttpListener ();
77 HLPC coll = listener.Prefixes;
78 coll.Add ("http://127.0.0.1:8181/");
79 coll.Add ("http://127.0.0.1:8181/");
81 Assert.AreEqual (1, coll.Count, "Count");
82 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
83 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
88 public void EndsWithSlash ()
90 HttpListener listener = new HttpListener ();
91 listener.Prefixes.Add ("http://127.0.0.1:7777/");
95 public void DifferentPath ()
97 HttpListener listener = new HttpListener ();
98 listener.Prefixes.Add ("http://127.0.0.1:7777/");
99 listener.Prefixes.Add ("http://127.0.0.1:7777/hola/");
100 Assert.AreEqual (2, listener.Prefixes.Count, "#01");
104 public void NL_Clear ()
106 HttpListener listener = new HttpListener ();
107 HLPC coll = listener.Prefixes;
112 public void NL_Remove ()
114 HttpListener listener = new HttpListener ();
115 HLPC coll = listener.Prefixes;
116 Assert.IsFalse (coll.Remove ("http://127.0.0.1:8181/"));
120 public void NL_RemoveBadUri ()
122 HttpListener listener = new HttpListener ();
123 HLPC coll = listener.Prefixes;
124 Assert.IsFalse (coll.Remove ("httpblah://127.0.0.1:8181/"));
128 [ExpectedException (typeof (ArgumentException))]
129 public void NL_AddBadUri ()
131 HttpListener listener = new HttpListener ();
132 HLPC coll = listener.Prefixes;
133 coll.Add ("httpblah://127.0.0.1:8181/");
137 [ExpectedException (typeof (ArgumentException))]
138 public void NoHostInUrl ()
140 HttpListener listener = new HttpListener ();
141 listener.Prefixes.Add ("http://:7777/hola/");
145 public void MultipleSlashes ()
147 // this one throws on Start(), not when adding it.
148 // See same test name in HttpListenerTest.
149 HttpListener listener = new HttpListener ();
150 HLPC coll = listener.Prefixes;
151 coll.Add ("http://localhost:7777/hola////");
152 string [] strs = new string [1];
153 coll.CopyTo (strs, 0);
154 Assert.AreEqual ("http://localhost:7777/hola////", strs [0]);
158 public void PercentSign ()
160 HttpListener listener = new HttpListener ();
161 HLPC coll = listener.Prefixes;
162 // this one throws on Start(), not when adding it.
163 // See same test name in HttpListenerTest.
164 coll.Add ("http://localhost:7777/hola%3E/");
165 string [] strs = new string [1];
166 coll.CopyTo (strs, 0);
167 Assert.AreEqual ("http://localhost:7777/hola%3E/", strs [0]);
171 public void Disposed1 ()
173 HttpListener listener = new HttpListener ();
174 HLPC coll = listener.Prefixes;
176 Assert.AreEqual (0, coll.Count, "Count");
177 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
178 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
182 [ExpectedException (typeof (ObjectDisposedException))]
183 public void Disposed2 ()
185 HttpListener listener = new HttpListener ();
186 HLPC coll = listener.Prefixes;
188 coll.Add ("http://localhost:7777/hola/");
192 [ExpectedException (typeof (ObjectDisposedException))]
193 public void Disposed3 ()
195 HttpListener listener = new HttpListener ();
196 HLPC coll = listener.Prefixes;
202 [ExpectedException (typeof (ObjectDisposedException))]
203 public void Disposed4 ()
205 HttpListener listener = new HttpListener ();
206 HLPC coll = listener.Prefixes;
208 coll.Remove ("http://localhost:7777/hola/");
212 [ExpectedException (typeof (ObjectDisposedException))]
213 public void Disposed5 ()
215 HttpListener listener = new HttpListener ();
216 HLPC coll = listener.Prefixes;
218 string [] strs = new string [0];
219 coll.CopyTo (strs, 0);
223 public void Disposed6 ()
225 HttpListener listener = new HttpListener ();
226 HLPC coll = listener.Prefixes;
229 foreach (string s in coll) {
230 a = s; // just to make the compiler happy
236 public void Disposed7 ()
238 HttpListener listener = new HttpListener ();
239 HLPC coll = listener.Prefixes;
240 coll.Add ("http://127.0.0.1/");
243 foreach (string s in coll) {
245 Assert.AreEqual (s, "http://127.0.0.1/");
247 Assert.AreEqual (items, 1);