[utils] Fix inet_pton fallback.
[mono.git] / mcs / class / System / Test / System.Net / HttpListenerPrefixCollectionTest.cs
1 //
2 // HttpListenerPrefixCollectionTest.cs
3 //      - Unit tests for System.Net.HttpListenePrefixCollection
4 //
5 // Author:
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29 using System;
30 using System.Net;
31 using NUnit.Framework;
32 using HLPC=System.Net.HttpListenerPrefixCollection;
33
34 namespace MonoTests.System.Net {
35         [TestFixture]
36         public class HttpListenerPrefixCollectionTest {
37                 // NL -> Not listening -> tests when listener.IsListening == false
38                 [Test]
39 #if FEATURE_NO_BSD_SOCKETS
40                 [ExpectedException (typeof (PlatformNotSupportedException))]
41 #endif
42                 public void NL_DefaultProperties ()
43                 {
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");
49                 }
50
51                 [Test]
52 #if FEATURE_NO_BSD_SOCKETS
53                 [ExpectedException (typeof (PlatformNotSupportedException))]
54 #endif
55                 public void DefaultProperties ()
56                 {
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");
63                 }
64
65                 [Test]
66 #if FEATURE_NO_BSD_SOCKETS
67                 [ExpectedException (typeof (PlatformNotSupportedException))]
68 #endif
69                 public void AddOne ()
70                 {
71                         HttpListener listener = new HttpListener ();
72                         HLPC coll = listener.Prefixes;
73                         listener.Start ();
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");
78                         listener.Stop ();
79                 }
80
81                 [Test]
82 #if FEATURE_NO_BSD_SOCKETS
83                 [ExpectedException (typeof (PlatformNotSupportedException))]
84 #endif
85                 public void Duplicate ()
86                 {
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/");
91                         listener.Start ();
92                         Assert.AreEqual (1, coll.Count, "Count");
93                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
94                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
95                         listener.Stop ();
96                 }
97
98                 [Test]
99 #if FEATURE_NO_BSD_SOCKETS
100                 [ExpectedException (typeof (PlatformNotSupportedException))]
101 #endif
102                 public void EndsWithSlash ()
103                 {
104                         HttpListener listener = new HttpListener ();
105                         listener.Prefixes.Add ("http://127.0.0.1:7777/");
106                 }
107
108                 [Test]
109 #if FEATURE_NO_BSD_SOCKETS
110                 [ExpectedException (typeof (PlatformNotSupportedException))]
111 #endif
112                 public void DifferentPath ()
113                 {
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");
118                 }
119
120                 [Test]
121 #if FEATURE_NO_BSD_SOCKETS
122                 [ExpectedException (typeof (PlatformNotSupportedException))]
123 #endif
124                 public void NL_Clear ()
125                 {
126                         HttpListener listener = new HttpListener ();
127                         HLPC coll = listener.Prefixes;
128                         coll.Clear ();
129                 }
130
131                 [Test]
132 #if FEATURE_NO_BSD_SOCKETS
133                 [ExpectedException (typeof (PlatformNotSupportedException))]
134 #endif
135                 public void NL_Remove ()
136                 {
137                         HttpListener listener = new HttpListener ();
138                         HLPC coll = listener.Prefixes;
139                         Assert.IsFalse (coll.Remove ("http://127.0.0.1:8181/"));
140                 }
141
142                 [Test]
143 #if FEATURE_NO_BSD_SOCKETS
144                 [ExpectedException (typeof (PlatformNotSupportedException))]
145 #endif
146                 public void NL_RemoveBadUri ()
147                 {
148                         HttpListener listener = new HttpListener ();
149                         HLPC coll = listener.Prefixes;
150                         Assert.IsFalse (coll.Remove ("httpblah://127.0.0.1:8181/"));
151                 }
152
153                 [Test]
154 #if FEATURE_NO_BSD_SOCKETS
155                 [ExpectedException (typeof (PlatformNotSupportedException))]
156 #else
157                 [ExpectedException (typeof (ArgumentException))]
158 #endif
159                 public void NL_AddBadUri ()
160                 {
161                         HttpListener listener = new HttpListener ();
162                         HLPC coll = listener.Prefixes;
163                         coll.Add ("httpblah://127.0.0.1:8181/");
164                 }
165
166                 [Test]
167 #if FEATURE_NO_BSD_SOCKETS
168                 [ExpectedException (typeof (PlatformNotSupportedException))]
169 #else
170                 [ExpectedException (typeof (ArgumentException))]
171 #endif
172                 public void NoHostInUrl ()
173                 {
174                         HttpListener listener = new HttpListener ();
175                         listener.Prefixes.Add ("http://:7777/hola/");
176                 }
177
178                 [Test]
179 #if FEATURE_NO_BSD_SOCKETS
180                 [ExpectedException (typeof (PlatformNotSupportedException))]
181 #endif
182                 public void MultipleSlashes ()
183                 {
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]);
192                 }
193
194                 [Test]
195 #if FEATURE_NO_BSD_SOCKETS
196                 [ExpectedException (typeof (PlatformNotSupportedException))]
197 #endif
198                 public void PercentSign ()
199                 {
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]);
208                 }
209
210                 [Test]
211 #if FEATURE_NO_BSD_SOCKETS
212                 [ExpectedException (typeof (PlatformNotSupportedException))]
213 #endif
214                 public void Disposed1 ()
215                 {
216                         HttpListener listener = new HttpListener ();
217                         HLPC coll = listener.Prefixes;
218                         listener.Close ();
219                         Assert.AreEqual (0, coll.Count, "Count");
220                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
221                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
222                 }
223
224                 [Test]
225 #if FEATURE_NO_BSD_SOCKETS
226                 [ExpectedException (typeof (PlatformNotSupportedException))]
227 #else
228                 [ExpectedException (typeof (ObjectDisposedException))]
229 #endif
230                 public void Disposed2 ()
231                 {
232                         HttpListener listener = new HttpListener ();
233                         HLPC coll = listener.Prefixes;
234                         listener.Close ();
235                         coll.Add ("http://localhost:7777/hola/");
236                 }
237
238                 [Test]
239 #if FEATURE_NO_BSD_SOCKETS
240                 [ExpectedException (typeof (PlatformNotSupportedException))]
241 #else
242                 [ExpectedException (typeof (ObjectDisposedException))]
243 #endif
244                 public void Disposed3 ()
245                 {
246                         HttpListener listener = new HttpListener ();
247                         HLPC coll = listener.Prefixes;
248                         listener.Close ();
249                         coll.Clear ();
250                 }
251
252                 [Test]
253 #if FEATURE_NO_BSD_SOCKETS
254                 [ExpectedException (typeof (PlatformNotSupportedException))]
255 #else
256                 [ExpectedException (typeof (ObjectDisposedException))]
257 #endif
258                 public void Disposed4 ()
259                 {
260                         HttpListener listener = new HttpListener ();
261                         HLPC coll = listener.Prefixes;
262                         listener.Close ();
263                         coll.Remove ("http://localhost:7777/hola/");
264                 }
265
266                 [Test]
267 #if FEATURE_NO_BSD_SOCKETS
268                 [ExpectedException (typeof (PlatformNotSupportedException))]
269 #else
270                 [ExpectedException (typeof (ObjectDisposedException))]
271 #endif
272                 public void Disposed5 ()
273                 {
274                         HttpListener listener = new HttpListener ();
275                         HLPC coll = listener.Prefixes;
276                         listener.Close ();
277                         string [] strs = new string [0];
278                         coll.CopyTo (strs, 0);
279                 }
280
281                 [Test]
282 #if FEATURE_NO_BSD_SOCKETS
283                 [ExpectedException (typeof (PlatformNotSupportedException))]
284 #endif
285                 public void Disposed6 ()
286                 {
287                         HttpListener listener = new HttpListener ();
288                         HLPC coll = listener.Prefixes;
289                         listener.Close ();
290                         string a = null;
291                         foreach (string s in coll) {
292                                 a = s; // just to make the compiler happy
293                         }
294                         Assert.IsNull (a);
295                 }
296
297                 [Test]
298 #if FEATURE_NO_BSD_SOCKETS
299                 [ExpectedException (typeof (PlatformNotSupportedException))]
300 #endif
301                 public void Disposed7 ()
302                 {
303                         HttpListener listener = new HttpListener ();
304                         HLPC coll = listener.Prefixes;
305                         coll.Add ("http://127.0.0.1/");
306                         listener.Close ();
307                         int items = 0;
308                         foreach (string s in coll) {
309                                 items++;
310                                 Assert.AreEqual (s, "http://127.0.0.1/");
311                         }
312                         Assert.AreEqual (items, 1);
313                 }
314         }
315 }
316