b30b7cc6bea8856ea812315469894b58c530ebc5
[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 #if NET_2_0
30 using System;
31 using System.Net;
32 using NUnit.Framework;
33 using HLPC=System.Net.HttpListenerPrefixCollection;
34
35 namespace MonoTests.System.Net {
36         [TestFixture]
37 #if TARGET_JVM
38         [Ignore ("The class System.Net.HttpListenerPrefixCollection - is not supported")]
39 #endif
40         public class HttpListenerPrefixCollectionTest {
41 #if !TARGET_JVM
42                 // NL -> Not listening -> tests when listener.IsListening == false
43                 [Test]
44                 public void NL_DefaultProperties ()
45                 {
46                         HttpListener listener = new HttpListener ();
47                         HLPC coll = listener.Prefixes;
48                         Assert.AreEqual (0, coll.Count, "Count");
49                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
50                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
51                 }
52
53                 [Test]
54                 public void DefaultProperties ()
55                 {
56                         HttpListener listener = new HttpListener ();
57                         HLPC coll = listener.Prefixes;
58                         coll.Add ("http://127.0.0.1:8181/");
59                         Assert.AreEqual (1, coll.Count, "Count");
60                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
61                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
62                 }
63
64                 [Test]
65                 public void AddOne ()
66                 {
67                         HttpListener listener = new HttpListener ();
68                         HLPC coll = listener.Prefixes;
69                         listener.Start ();
70                         coll.Add ("http://127.0.0.1:8181/");
71                         Assert.AreEqual (1, coll.Count, "Count");
72                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
73                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
74                         listener.Stop ();
75                 }
76
77                 [Test]
78                 public void Duplicate ()
79                 {
80                         HttpListener listener = new HttpListener ();
81                         HLPC coll = listener.Prefixes;
82                         coll.Add ("http://127.0.0.1:8181/");
83                         coll.Add ("http://127.0.0.1:8181/");
84                         listener.Start ();
85                         Assert.AreEqual (1, coll.Count, "Count");
86                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
87                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
88                         listener.Stop ();
89                 }
90
91                 [Test]
92                 public void EndsWithSlash ()
93                 {
94                         HttpListener listener = new HttpListener ();
95                         listener.Prefixes.Add ("http://127.0.0.1:7777/");
96                 }
97
98                 [Test]
99                 public void DifferentPath ()
100                 {
101                         HttpListener listener = new HttpListener ();
102                         listener.Prefixes.Add ("http://127.0.0.1:7777/");
103                         listener.Prefixes.Add ("http://127.0.0.1:7777/hola/");
104                         Assert.AreEqual (2, listener.Prefixes.Count, "#01");
105                 }
106
107                 [Test]
108                 public void NL_Clear ()
109                 {
110                         HttpListener listener = new HttpListener ();
111                         HLPC coll = listener.Prefixes;
112                         coll.Clear ();
113                 }
114
115                 [Test]
116                 public void NL_Remove ()
117                 {
118                         HttpListener listener = new HttpListener ();
119                         HLPC coll = listener.Prefixes;
120                         Assert.IsFalse (coll.Remove ("http://127.0.0.1:8181/"));
121                 }
122
123                 [Test]
124                 public void NL_RemoveBadUri ()
125                 {
126                         HttpListener listener = new HttpListener ();
127                         HLPC coll = listener.Prefixes;
128                         Assert.IsFalse (coll.Remove ("httpblah://127.0.0.1:8181/"));
129                 }
130
131                 [Test]
132                 [ExpectedException (typeof (ArgumentException))]
133                 public void NL_AddBadUri ()
134                 {
135                         HttpListener listener = new HttpListener ();
136                         HLPC coll = listener.Prefixes;
137                         coll.Add ("httpblah://127.0.0.1:8181/");
138                 }
139
140                 [Test]
141                 [ExpectedException (typeof (ArgumentException))]
142                 public void NoHostInUrl ()
143                 {
144                         HttpListener listener = new HttpListener ();
145                         listener.Prefixes.Add ("http://:7777/hola/");
146                 }
147
148                 [Test]
149                 public void MultipleSlashes ()
150                 {
151                         // this one throws on Start(), not when adding it.
152                         // See same test name in HttpListenerTest.
153                         HttpListener listener = new HttpListener ();
154                         HLPC coll = listener.Prefixes;
155                         coll.Add ("http://localhost:7777/hola////");
156                         string [] strs = new string [1];
157                         coll.CopyTo (strs, 0);
158                         Assert.AreEqual ("http://localhost:7777/hola////", strs [0]);
159                 }
160
161                 [Test]
162                 public void PercentSign ()
163                 {
164                         HttpListener listener = new HttpListener ();
165                         HLPC coll = listener.Prefixes;
166                         // this one throws on Start(), not when adding it.
167                         // See same test name in HttpListenerTest.
168                         coll.Add ("http://localhost:7777/hola%3E/");
169                         string [] strs = new string [1];
170                         coll.CopyTo (strs, 0);
171                         Assert.AreEqual ("http://localhost:7777/hola%3E/", strs [0]);
172                 }
173
174                 [Test]
175                 public void Disposed1 ()
176                 {
177                         HttpListener listener = new HttpListener ();
178                         HLPC coll = listener.Prefixes;
179                         listener.Close ();
180                         Assert.AreEqual (0, coll.Count, "Count");
181                         Assert.IsFalse (coll.IsReadOnly, "IsReadOnly");
182                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (ObjectDisposedException))]
187                 public void Disposed2 ()
188                 {
189                         HttpListener listener = new HttpListener ();
190                         HLPC coll = listener.Prefixes;
191                         listener.Close ();
192                         coll.Add ("http://localhost:7777/hola/");
193                 }
194
195                 [Test]
196                 [ExpectedException (typeof (ObjectDisposedException))]
197                 public void Disposed3 ()
198                 {
199                         HttpListener listener = new HttpListener ();
200                         HLPC coll = listener.Prefixes;
201                         listener.Close ();
202                         coll.Clear ();
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof (ObjectDisposedException))]
207                 public void Disposed4 ()
208                 {
209                         HttpListener listener = new HttpListener ();
210                         HLPC coll = listener.Prefixes;
211                         listener.Close ();
212                         coll.Remove ("http://localhost:7777/hola/");
213                 }
214
215                 [Test]
216                 [ExpectedException (typeof (ObjectDisposedException))]
217                 public void Disposed5 ()
218                 {
219                         HttpListener listener = new HttpListener ();
220                         HLPC coll = listener.Prefixes;
221                         listener.Close ();
222                         string [] strs = new string [0];
223                         coll.CopyTo (strs, 0);
224                 }
225
226                 [Test]
227                 public void Disposed6 ()
228                 {
229                         HttpListener listener = new HttpListener ();
230                         HLPC coll = listener.Prefixes;
231                         listener.Close ();
232                         string a = null;
233                         foreach (string s in coll) {
234                                 a = s; // just to make the compiler happy
235                         }
236                         Assert.IsNull (a);
237                 }
238
239                 [Test]
240                 public void Disposed7 ()
241                 {
242                         HttpListener listener = new HttpListener ();
243                         HLPC coll = listener.Prefixes;
244                         coll.Add ("http://127.0.0.1/");
245                         listener.Close ();
246                         int items = 0;
247                         foreach (string s in coll) {
248                                 items++;
249                                 Assert.AreEqual (s, "http://127.0.0.1/");
250                         }
251                         Assert.AreEqual (items, 1);
252                 }
253 #endif
254         }
255 }
256 #endif
257