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