21589dc6c30914ebe227c22b04a99143a720457e
[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                 public void NL_DefaultProperties ()
40                 {
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");
46                 }
47
48                 [Test]
49                 public void DefaultProperties ()
50                 {
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");
57                 }
58
59                 [Test]
60                 [Category ("RequiresBSDSockets")]
61                 public void AddOne ()
62                 {
63                         HttpListener listener = new HttpListener ();
64                         HLPC coll = listener.Prefixes;
65                         listener.Start ();
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");
70                         listener.Stop ();
71                 }
72
73                 [Test]
74                 [Category ("RequiresBSDSockets")]
75                 public void Duplicate ()
76                 {
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/");
81                         listener.Start ();
82                         Assert.AreEqual (1, coll.Count, "Count");
83                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
84                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
85                         listener.Stop ();
86                 }
87
88                 [Test]
89                 public void EndsWithSlash ()
90                 {
91                         HttpListener listener = new HttpListener ();
92                         listener.Prefixes.Add ("http://127.0.0.1:7777/");
93                 }
94
95                 [Test]
96                 public void DifferentPath ()
97                 {
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");
102                 }
103
104                 [Test]
105                 public void NL_Clear ()
106                 {
107                         HttpListener listener = new HttpListener ();
108                         HLPC coll = listener.Prefixes;
109                         coll.Clear ();
110                 }
111
112                 [Test]
113                 public void NL_Remove ()
114                 {
115                         HttpListener listener = new HttpListener ();
116                         HLPC coll = listener.Prefixes;
117                         Assert.IsFalse (coll.Remove ("http://127.0.0.1:8181/"));
118                 }
119
120                 [Test]
121                 public void NL_RemoveBadUri ()
122                 {
123                         HttpListener listener = new HttpListener ();
124                         HLPC coll = listener.Prefixes;
125                         Assert.IsFalse (coll.Remove ("httpblah://127.0.0.1:8181/"));
126                 }
127
128                 [Test]
129                 [ExpectedException (typeof (ArgumentException))]
130                 public void NL_AddBadUri ()
131                 {
132                         HttpListener listener = new HttpListener ();
133                         HLPC coll = listener.Prefixes;
134                         coll.Add ("httpblah://127.0.0.1:8181/");
135                 }
136
137                 [Test]
138                 [ExpectedException (typeof (ArgumentException))]
139                 public void NoHostInUrl ()
140                 {
141                         HttpListener listener = new HttpListener ();
142                         listener.Prefixes.Add ("http://:7777/hola/");
143                 }
144
145                 [Test]
146                 public void MultipleSlashes ()
147                 {
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]);
156                 }
157
158                 [Test]
159                 public void PercentSign ()
160                 {
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]);
169                 }
170
171                 [Test]
172                 public void Disposed1 ()
173                 {
174                         HttpListener listener = new HttpListener ();
175                         HLPC coll = listener.Prefixes;
176                         listener.Close ();
177                         Assert.AreEqual (0, coll.Count, "Count");
178                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
179                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ObjectDisposedException))]
184                 public void Disposed2 ()
185                 {
186                         HttpListener listener = new HttpListener ();
187                         HLPC coll = listener.Prefixes;
188                         listener.Close ();
189                         coll.Add ("http://localhost:7777/hola/");
190                 }
191
192                 [Test]
193                 [ExpectedException (typeof (ObjectDisposedException))]
194                 public void Disposed3 ()
195                 {
196                         HttpListener listener = new HttpListener ();
197                         HLPC coll = listener.Prefixes;
198                         listener.Close ();
199                         coll.Clear ();
200                 }
201
202                 [Test]
203                 [ExpectedException (typeof (ObjectDisposedException))]
204                 public void Disposed4 ()
205                 {
206                         HttpListener listener = new HttpListener ();
207                         HLPC coll = listener.Prefixes;
208                         listener.Close ();
209                         coll.Remove ("http://localhost:7777/hola/");
210                 }
211
212                 [Test]
213                 [ExpectedException (typeof (ObjectDisposedException))]
214                 public void Disposed5 ()
215                 {
216                         HttpListener listener = new HttpListener ();
217                         HLPC coll = listener.Prefixes;
218                         listener.Close ();
219                         string [] strs = new string [0];
220                         coll.CopyTo (strs, 0);
221                 }
222
223                 [Test]
224                 public void Disposed6 ()
225                 {
226                         HttpListener listener = new HttpListener ();
227                         HLPC coll = listener.Prefixes;
228                         listener.Close ();
229                         string a = null;
230                         foreach (string s in coll) {
231                                 a = s; // just to make the compiler happy
232                         }
233                         Assert.IsNull (a);
234                 }
235
236                 [Test]
237                 public void Disposed7 ()
238                 {
239                         HttpListener listener = new HttpListener ();
240                         HLPC coll = listener.Prefixes;
241                         coll.Add ("http://127.0.0.1/");
242                         listener.Close ();
243                         int items = 0;
244                         foreach (string s in coll) {
245                                 items++;
246                                 Assert.AreEqual (s, "http://127.0.0.1/");
247                         }
248                         Assert.AreEqual (items, 1);
249                 }
250         }
251 }
252