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");
60 [Category ("RequiresBSDSockets")]
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 [Category ("RequiresBSDSockets")]
75 public void Duplicate ()
77 HttpListener listener = new HttpListener ();
78 HLPC coll = listener.Prefixes;
79 coll.Add ("http://127.0.0.1:8181/");
80 coll.Add ("http://127.0.0.1:8181/");
82 Assert.AreEqual (1, coll.Count, "Count");
83 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
84 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
89 public void EndsWithSlash ()
91 HttpListener listener = new HttpListener ();
92 listener.Prefixes.Add ("http://127.0.0.1:7777/");
96 public void DifferentPath ()
98 HttpListener listener = new HttpListener ();
99 listener.Prefixes.Add ("http://127.0.0.1:7777/");
100 listener.Prefixes.Add ("http://127.0.0.1:7777/hola/");
101 Assert.AreEqual (2, listener.Prefixes.Count, "#01");
105 public void NL_Clear ()
107 HttpListener listener = new HttpListener ();
108 HLPC coll = listener.Prefixes;
113 public void NL_Remove ()
115 HttpListener listener = new HttpListener ();
116 HLPC coll = listener.Prefixes;
117 Assert.IsFalse (coll.Remove ("http://127.0.0.1:8181/"));
121 public void NL_RemoveBadUri ()
123 HttpListener listener = new HttpListener ();
124 HLPC coll = listener.Prefixes;
125 Assert.IsFalse (coll.Remove ("httpblah://127.0.0.1:8181/"));
129 [ExpectedException (typeof (ArgumentException))]
130 public void NL_AddBadUri ()
132 HttpListener listener = new HttpListener ();
133 HLPC coll = listener.Prefixes;
134 coll.Add ("httpblah://127.0.0.1:8181/");
138 [ExpectedException (typeof (ArgumentException))]
139 public void NoHostInUrl ()
141 HttpListener listener = new HttpListener ();
142 listener.Prefixes.Add ("http://:7777/hola/");
146 public void MultipleSlashes ()
148 // this one throws on Start(), not when adding it.
149 // See same test name in HttpListenerTest.
150 HttpListener listener = new HttpListener ();
151 HLPC coll = listener.Prefixes;
152 coll.Add ("http://localhost:7777/hola////");
153 string [] strs = new string [1];
154 coll.CopyTo (strs, 0);
155 Assert.AreEqual ("http://localhost:7777/hola////", strs [0]);
159 public void PercentSign ()
161 HttpListener listener = new HttpListener ();
162 HLPC coll = listener.Prefixes;
163 // this one throws on Start(), not when adding it.
164 // See same test name in HttpListenerTest.
165 coll.Add ("http://localhost:7777/hola%3E/");
166 string [] strs = new string [1];
167 coll.CopyTo (strs, 0);
168 Assert.AreEqual ("http://localhost:7777/hola%3E/", strs [0]);
172 public void Disposed1 ()
174 HttpListener listener = new HttpListener ();
175 HLPC coll = listener.Prefixes;
177 Assert.AreEqual (0, coll.Count, "Count");
178 Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
179 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
183 [ExpectedException (typeof (ObjectDisposedException))]
184 public void Disposed2 ()
186 HttpListener listener = new HttpListener ();
187 HLPC coll = listener.Prefixes;
189 coll.Add ("http://localhost:7777/hola/");
193 [ExpectedException (typeof (ObjectDisposedException))]
194 public void Disposed3 ()
196 HttpListener listener = new HttpListener ();
197 HLPC coll = listener.Prefixes;
203 [ExpectedException (typeof (ObjectDisposedException))]
204 public void Disposed4 ()
206 HttpListener listener = new HttpListener ();
207 HLPC coll = listener.Prefixes;
209 coll.Remove ("http://localhost:7777/hola/");
213 [ExpectedException (typeof (ObjectDisposedException))]
214 public void Disposed5 ()
216 HttpListener listener = new HttpListener ();
217 HLPC coll = listener.Prefixes;
219 string [] strs = new string [0];
220 coll.CopyTo (strs, 0);
224 public void Disposed6 ()
226 HttpListener listener = new HttpListener ();
227 HLPC coll = listener.Prefixes;
230 foreach (string s in coll) {
231 a = s; // just to make the compiler happy
237 public void Disposed7 ()
239 HttpListener listener = new HttpListener ();
240 HLPC coll = listener.Prefixes;
241 coll.Add ("http://127.0.0.1/");
244 foreach (string s in coll) {
246 Assert.AreEqual (s, "http://127.0.0.1/");
248 Assert.AreEqual (items, 1);