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