Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / System / Test / System / UriParserTest.cs
1 //
2 // UriParserTest.cs - Unit tests for System.UriParser
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32
33 using System;
34 using System.Text;
35
36 namespace MonoTests.System {
37
38         public class UnitTestUriParser: UriParser {
39
40                 private string scheme_name;
41                 private int default_port;
42                 private bool throw_on_register;
43                 private bool on_new_uri_called;
44
45                 public UnitTestUriParser ()
46                 {
47                 }
48
49                 public UnitTestUriParser (bool throwOnRegister)
50                 {
51                         throw_on_register = throwOnRegister;
52                 }
53
54                 public string SchemeName
55                 {
56                         get
57                         {
58                                 return scheme_name;
59                         }
60                 }
61
62                 public int DefaultPort
63                 {
64                         get
65                         {
66                                 return default_port;
67                         }
68                 }
69
70                 public bool OnNewUriCalled
71                 {
72                         get
73                         {
74                                 return on_new_uri_called;
75                         }
76                 }
77
78                 public string _GetComponents (Uri uri, UriComponents components, UriFormat format)
79                 {
80                         return base.GetComponents (uri, components, format);
81                 }
82
83                 public void _InitializeAndValidate (Uri uri, out UriFormatException parserError)
84                 {
85                         base.InitializeAndValidate (uri, out parserError);
86                 }
87
88                 public bool _IsBaseOf (Uri baseUri, Uri relativeUri)
89                 {
90                         return base.IsBaseOf (baseUri, relativeUri);
91                 }
92
93                 public bool _IsWellFormedOriginalString (Uri uri)
94                 {
95                         return base.IsWellFormedOriginalString (uri);
96                 }
97
98                 public UriParser _OnNewUri ()
99                 {
100                         return base.OnNewUri ();
101                 }
102
103                 public void _OnRegister (string schemeName, int defaultPort)
104                 {
105                         base.OnRegister (schemeName, defaultPort);
106                 }
107
108                 public string _Resolve (Uri baseUri, Uri relativeUri, out UriFormatException parserError)
109                 {
110                         return base.Resolve (baseUri, relativeUri, out parserError);
111                 }
112
113                 protected override UriParser OnNewUri ()
114                 {
115                         on_new_uri_called = true;
116                         return base.OnNewUri ();
117                 }
118
119                 protected override void OnRegister (string schemeName, int defaultPort)
120                 {
121                         if (throw_on_register)
122                                 throw new NotSupportedException ();
123                         scheme_name = schemeName;
124                         default_port = defaultPort;
125                         base.OnRegister (schemeName, defaultPort);
126                 }
127         }
128
129         [TestFixture]
130         public class UriParserTest {
131
132                 private const string full_http = "http://www.mono-project.com/Main_Page#FAQ?Edit";
133
134                 private string prefix;
135                 private Uri http;
136                 private Uri ftp, ftp2;
137
138                 [TestFixtureSetUp]
139                 public void FixtureSetUp ()
140                 {
141                         prefix = "unit.test.";
142                         http = new Uri (full_http);
143                         ftp = new Uri ("ftp://username:password@ftp.go-mono.com:21/with some spaces/mono.tgz");
144
145                         // Uses percent encoding on the username and password
146                         ftp2 = new Uri ("ftp://%75sername%3a%70assword@ftp.go-mono.com:21/with some spaces/mono.tgz");
147                 }
148
149                 public string Prefix
150                 {
151                         get
152                         {
153                                 return prefix;
154                         }
155                         set
156                         {
157                                 prefix = value;
158                         }
159                 }
160
161                 [Test]
162                 public void GetComponents ()
163                 {
164                         UnitTestUriParser p = new UnitTestUriParser ();
165                         Assert.AreEqual ("http", p._GetComponents (http, UriComponents.Scheme, UriFormat.SafeUnescaped), "http.Scheme");
166                         Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.UserInfo, UriFormat.SafeUnescaped), "http.UserInfo");
167                         Assert.AreEqual ("www.mono-project.com", p._GetComponents (http, UriComponents.Host, UriFormat.SafeUnescaped), "http.Host");
168                         Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.Port, UriFormat.SafeUnescaped), "http.Port");
169                         Assert.AreEqual ("Main_Page", p._GetComponents (http, UriComponents.Path, UriFormat.SafeUnescaped), "http.Path");
170                         Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.Query, UriFormat.SafeUnescaped), "http.Query");
171                         Assert.AreEqual ("FAQ?Edit", p._GetComponents (http, UriComponents.Fragment, UriFormat.SafeUnescaped), "http.Fragment");
172                         Assert.AreEqual ("80", p._GetComponents (http, UriComponents.StrongPort, UriFormat.SafeUnescaped), "http.StrongPort");
173                         Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.KeepDelimiter, UriFormat.SafeUnescaped), "http.KeepDelimiter");
174                         Assert.AreEqual ("www.mono-project.com:80", p._GetComponents (http, UriComponents.HostAndPort, UriFormat.SafeUnescaped), "http.HostAndPort");
175                         Assert.AreEqual ("www.mono-project.com:80", p._GetComponents (http, UriComponents.StrongAuthority, UriFormat.SafeUnescaped), "http.StrongAuthority");
176                         Assert.AreEqual (full_http, p._GetComponents (http, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped), "http.AbsoluteUri");
177                         Assert.AreEqual ("/Main_Page", p._GetComponents (http, UriComponents.PathAndQuery, UriFormat.SafeUnescaped), "http.PathAndQuery");
178                         Assert.AreEqual ("http://www.mono-project.com/Main_Page", p._GetComponents (http, UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped), "http.HttpRequestUrl");
179                         Assert.AreEqual ("http://www.mono-project.com", p._GetComponents (http, UriComponents.SchemeAndServer, UriFormat.SafeUnescaped), "http.SchemeAndServer");
180                         Assert.AreEqual (full_http, p._GetComponents (http, UriComponents.SerializationInfoString, UriFormat.SafeUnescaped), "http.SerializationInfoString");
181                         // strange mixup
182                         Assert.AreEqual ("http://", p._GetComponents (http, UriComponents.Scheme | UriComponents.Port, UriFormat.SafeUnescaped), "http.Scheme+Port");
183                         Assert.AreEqual ("www.mono-project.com#FAQ?Edit", p._GetComponents (http, UriComponents.Host | UriComponents.Fragment, UriFormat.SafeUnescaped), "http.Scheme+Port");
184                         Assert.AreEqual ("/Main_Page", p._GetComponents (http, UriComponents.Port | UriComponents.Path, UriFormat.SafeUnescaped), "http.Scheme+Port");
185                         Assert.AreSame (p, p._OnNewUri (), "OnNewUri");
186                 }
187
188                 [Test]
189                 public void GetComponents_Ftp ()
190                 {
191                         UnitTestUriParser p = new UnitTestUriParser ();
192                         Assert.AreEqual ("ftp", p._GetComponents (ftp, UriComponents.Scheme, UriFormat.Unescaped), "ftp.Scheme");
193                         Assert.AreEqual ("username:password", p._GetComponents (ftp, UriComponents.UserInfo, UriFormat.Unescaped), "ftp.UserInfo");
194                         Assert.AreEqual ("ftp.go-mono.com", p._GetComponents (ftp, UriComponents.Host, UriFormat.Unescaped), "ftp.Host");
195                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Port, UriFormat.Unescaped), "ftp.Port");
196                         Assert.AreEqual ("with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.Unescaped), "ftp.Path");
197                         Assert.AreEqual ("with%20some%20spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.UriEscaped), "ftp.Path-UriEscaped");
198                         Assert.AreEqual ("with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.SafeUnescaped), "ftp.Path-SafeUnescaped");
199                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Query, UriFormat.Unescaped), "ftp.Query");
200                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Fragment, UriFormat.Unescaped), "ftp.Fragment");
201                         Assert.AreEqual ("21", p._GetComponents (ftp, UriComponents.StrongPort, UriFormat.Unescaped), "ftp.StrongPort");
202                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.KeepDelimiter, UriFormat.Unescaped), "http.KeepDelimiter");
203                         Assert.AreEqual ("ftp.go-mono.com:21", p._GetComponents (ftp, UriComponents.HostAndPort, UriFormat.Unescaped), "http.HostAndPort");
204                         Assert.AreEqual ("username:password@ftp.go-mono.com:21", p._GetComponents (ftp, UriComponents.StrongAuthority, UriFormat.Unescaped), "http.StrongAuthority");
205                         Assert.AreEqual ("ftp://username:password@ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.AbsoluteUri, UriFormat.Unescaped), "http.AbsoluteUri");
206                         Assert.AreEqual ("/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.PathAndQuery, UriFormat.Unescaped), "http.PathAndQuery");
207                         Assert.AreEqual ("ftp://ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.HttpRequestUrl, UriFormat.Unescaped), "http.HttpRequestUrl");
208                         Assert.AreEqual ("ftp://ftp.go-mono.com", p._GetComponents (ftp, UriComponents.SchemeAndServer, UriFormat.Unescaped), "http.SchemeAndServer");
209                         Assert.AreEqual ("ftp://username:password@ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.SerializationInfoString, UriFormat.Unescaped), "http.SerializationInfoString");
210                         Assert.AreSame (p, p._OnNewUri (), "OnNewUri");
211                         // strange mixup
212                         Assert.AreEqual ("ftp://username:password@", p._GetComponents (ftp, UriComponents.Scheme | UriComponents.UserInfo, UriFormat.Unescaped), "ftp.Scheme+UserInfo");
213                         Assert.AreEqual (":21/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path | UriComponents.StrongPort, UriFormat.Unescaped), "ftp.Path+StrongPort");
214                 }
215
216                 [Test]
217                 public void GetComponents_Ftp2 ()
218                 {
219                         UnitTestUriParser p = new UnitTestUriParser ();
220                         Assert.AreEqual ("ftp", p._GetComponents (ftp, UriComponents.Scheme, UriFormat.Unescaped), "ftp.Scheme");
221                         Assert.AreEqual ("username:password", p._GetComponents (ftp, UriComponents.UserInfo, UriFormat.Unescaped), "ftp.UserInfo");
222                         Assert.AreEqual ("ftp.go-mono.com", p._GetComponents (ftp, UriComponents.Host, UriFormat.Unescaped), "ftp.Host");
223                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Port, UriFormat.Unescaped), "ftp.Port");
224                         Assert.AreEqual ("with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.Unescaped), "ftp.Path");
225                         Assert.AreEqual ("with%20some%20spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.UriEscaped), "ftp.Path-UriEscaped");
226                         Assert.AreEqual ("with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.SafeUnescaped), "ftp.Path-SafeUnescaped");
227                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Query, UriFormat.Unescaped), "ftp.Query");
228                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Fragment, UriFormat.Unescaped), "ftp.Fragment");
229                         Assert.AreEqual ("21", p._GetComponents (ftp, UriComponents.StrongPort, UriFormat.Unescaped), "ftp.StrongPort");
230                         Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.KeepDelimiter, UriFormat.Unescaped), "http.KeepDelimiter");
231                         Assert.AreEqual ("ftp.go-mono.com:21", p._GetComponents (ftp, UriComponents.HostAndPort, UriFormat.Unescaped), "http.HostAndPort");
232                         Assert.AreEqual ("username:password@ftp.go-mono.com:21", p._GetComponents (ftp, UriComponents.StrongAuthority, UriFormat.Unescaped), "http.StrongAuthority");
233                         Assert.AreEqual ("ftp://username:password@ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.AbsoluteUri, UriFormat.Unescaped), "http.AbsoluteUri");
234                         Assert.AreEqual ("/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.PathAndQuery, UriFormat.Unescaped), "http.PathAndQuery");
235                         Assert.AreEqual ("ftp://ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.HttpRequestUrl, UriFormat.Unescaped), "http.HttpRequestUrl");
236                         Assert.AreEqual ("ftp://ftp.go-mono.com", p._GetComponents (ftp, UriComponents.SchemeAndServer, UriFormat.Unescaped), "http.SchemeAndServer");
237                         Assert.AreEqual ("ftp://username:password@ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.SerializationInfoString, UriFormat.Unescaped), "http.SerializationInfoString");
238                         Assert.AreSame (p, p._OnNewUri (), "OnNewUri");
239                         // strange mixup
240                         Assert.AreEqual ("ftp://username:password@", p._GetComponents (ftp, UriComponents.Scheme | UriComponents.UserInfo, UriFormat.Unescaped), "ftp.Scheme+UserInfo");
241                         Assert.AreEqual (":21/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path | UriComponents.StrongPort, UriFormat.Unescaped), "ftp.Path+StrongPort");
242                 }
243
244                 // Test case for Xamarin#17665
245                 [Test]
246                 public void TestParseUserPath ()
247                 {
248                         var u = new Uri("https://a.net/1@1.msg");
249                         var result = u.GetComponents(UriComponents.Scheme | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);
250                         Assert.AreEqual (result, "https://a.net/1@1.msg", "parse@InUrl");
251                 }
252                 
253                 
254                 [Test]
255                 [ExpectedException (typeof (NullReferenceException))]
256                 public void GetComponents_Null ()
257                 {
258                         UnitTestUriParser p = new UnitTestUriParser ();
259                         p._GetComponents (null, UriComponents.Host, UriFormat.SafeUnescaped);
260                 }
261
262                 [Test]
263                 public void GetComponents_BadUriComponents ()
264                 {
265                         UnitTestUriParser p = new UnitTestUriParser ();
266                         Assert.AreEqual (full_http, p._GetComponents (http, (UriComponents) Int32.MinValue, UriFormat.SafeUnescaped), "http");
267                 }
268
269                 [Test]
270                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
271                 public void GetComponents_BadUriFormat ()
272                 {
273                         UnitTestUriParser p = new UnitTestUriParser ();
274                         p._GetComponents (http, UriComponents.Host, (UriFormat) Int32.MinValue);
275                 }
276
277                 [Test]
278                 public void InitializeAndValidate ()
279                 {
280                         UriFormatException error = null;
281                         UnitTestUriParser p = new UnitTestUriParser ();
282                         p._InitializeAndValidate (http, out error);
283                         Assert.IsNotNull (error, "out"); // authority/host couldn't be parsed ?!?!
284                 }
285
286                 [Test]
287                 [ExpectedException (typeof (NullReferenceException))]
288                 // oh man, this is a bad boy.It should be ArgumentNullException.
289                 public void InitializeAndValidate_Null ()
290                 {
291                         UriFormatException error = null;
292                         UnitTestUriParser p = new UnitTestUriParser ();
293                         p._InitializeAndValidate (null, out error);
294                 }
295
296                 [Test]
297                 public void IsBaseOf ()
298                 {
299                         UnitTestUriParser p = new UnitTestUriParser ();
300                         Assert.IsTrue (p._IsBaseOf (http, http), "http-http");
301
302                         Uri u = new Uri ("http://www.mono-project.com/Main_Page#FAQ");
303                         Assert.IsTrue (p._IsBaseOf (u, http), "http-1a");
304                         Assert.IsTrue (p._IsBaseOf (http, u), "http-1b");
305
306                         u = new Uri ("http://www.mono-project.com/Main_Page");
307                         Assert.IsTrue (p._IsBaseOf (u, http), "http-2a");
308                         Assert.IsTrue (p._IsBaseOf (http, u), "http-2b");
309
310                         u = new Uri ("http://www.mono-project.com/");
311                         Assert.IsTrue (p._IsBaseOf (u, http), "http-3a");
312                         Assert.IsTrue (p._IsBaseOf (http, u), "http-3b");
313
314                         u = new Uri ("http://www.mono-project.com/Main_Page/");
315                         Assert.IsFalse (p._IsBaseOf (u, http), "http-4a");
316                         Assert.IsTrue (p._IsBaseOf (http, u), "http-4b");
317
318                         // docs says the UserInfo isn't evaluated, but...
319                         u = new Uri ("http://username:password@www.mono-project.com/Main_Page");
320                         Assert.IsFalse (p._IsBaseOf (u, http), "http-5a");
321                         Assert.IsFalse (p._IsBaseOf (http, u), "http-5b");
322
323                         // scheme case sensitive ? no
324                         u = new Uri ("HTTP://www.mono-project.com/Main_Page");
325                         Assert.IsTrue (p._IsBaseOf (u, http), "http-6a");
326                         Assert.IsTrue (p._IsBaseOf (http, u), "http-6b");
327
328                         // host case sensitive ? no
329                         u = new Uri ("http://www.Mono-Project.com/Main_Page");
330                         Assert.IsTrue (p._IsBaseOf (u, http), "http-7a");
331                         Assert.IsTrue (p._IsBaseOf (http, u), "http-7b");
332
333                         // path case sensitive ? no
334                         u = new Uri ("http://www.Mono-Project.com/MAIN_Page");
335                         Assert.IsTrue (p._IsBaseOf (u, http), "http-8a");
336                         Assert.IsTrue (p._IsBaseOf (http, u), "http-8b");
337
338                         // different scheme
339                         u = new Uri ("ftp://www.mono-project.com/Main_Page");
340                         Assert.IsFalse (p._IsBaseOf (u, http), "http-9a");
341                         Assert.IsFalse (p._IsBaseOf (http, u), "http-9b");
342
343                         // different host
344                         u = new Uri ("http://www.go-mono.com/Main_Page");
345                         Assert.IsFalse (p._IsBaseOf (u, http), "http-10a");
346                         Assert.IsFalse (p._IsBaseOf (http, u), "http-10b");
347
348                         // different port
349                         u = new Uri ("http://www.mono-project.com:8080/");
350                         Assert.IsFalse (p._IsBaseOf (u, http), "http-11a");
351                         Assert.IsFalse (p._IsBaseOf (http, u), "http-11b");
352
353                         // specify default port
354                         u = new Uri ("http://www.mono-project.com:80/");
355                         Assert.IsTrue (p._IsBaseOf (u, http), "http-12a");
356                         Assert.IsTrue (p._IsBaseOf (http, u), "http-12b");
357                 }
358
359                 [Test]
360                 [ExpectedException (typeof (NullReferenceException))]
361                 public void IsBaseOf_UriNull ()
362                 {
363                         UnitTestUriParser p = new UnitTestUriParser ();
364                         p._IsBaseOf (http, null);
365                 }
366
367                 [Test]
368                 [ExpectedException (typeof (NullReferenceException))]
369                 public void IsBaseOf_NullUri ()
370                 {
371                         UnitTestUriParser p = new UnitTestUriParser ();
372                         p._IsBaseOf (null, http);
373                 }
374
375                 [Test]
376                 [Category ("NotWorking")]
377                 public void IsWellFormedOriginalString ()
378                 {
379                         UnitTestUriParser p = new UnitTestUriParser ();
380                         Assert.IsTrue (p._IsWellFormedOriginalString (http), "http");
381                 }
382
383                 [Test]
384                 [ExpectedException (typeof (NullReferenceException))]
385                 [Category ("NotWorking")]
386                 public void IsWellFormedOriginalString_Null ()
387                 {
388                         UnitTestUriParser p = new UnitTestUriParser ();
389                         p._IsWellFormedOriginalString (null);
390                 }
391
392                 [Test]
393                 [Category ("NotWorking")]
394                 public void OnNewUri ()
395                 {
396                         string scheme = prefix + "on.new.uri";
397                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
398
399                         UnitTestUriParser p = new UnitTestUriParser ();
400                         UriParser.Register (p, scheme, 1999);
401
402                         Assert.IsFalse (p.OnNewUriCalled, "!Called");
403                         Uri uri = new Uri (scheme + "://www.mono-project.com");
404                         Assert.IsTrue (p.OnNewUriCalled, "Called");
405                 }
406
407                 [Test]
408                 public void OnRegister ()
409                 {
410                         string scheme = prefix + "onregister";
411                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
412                         UnitTestUriParser p = new UnitTestUriParser ();
413                         try {
414                                 UriParser.Register (p, scheme, 2005);
415                         }
416                         catch (NotSupportedException) {
417                                 // special case / ordering
418                         }
419                         // if true then the registration is done before calling OnRegister
420                         Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
421                 }
422
423                 [Test]
424                 [Category ("NotWorking")]
425                 public void OnRegister2 ()
426                 {
427                         string scheme = prefix + "onregister2";
428                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
429                         UnitTestUriParser p = new UnitTestUriParser ();
430                         try {
431                                 UriParser.Register (p, scheme, 2005);
432                                 Uri uri = new Uri (scheme + "://foobar:2005");
433                                 Assert.AreEqual (scheme, uri.Scheme, "uri-prefix");
434                                 Assert.AreEqual (2005, uri.Port, "uri-port");
435                                 
436                                 Assert.AreEqual ("//foobar:2005", uri.LocalPath, "uri-localpath");
437                         }
438                         catch (NotSupportedException) {
439                                 // special case / ordering
440                         }
441                         // if true then the registration is done before calling OnRegister
442                         Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
443                 }
444
445                 [Test]
446                 [Category ("NotWorking")]
447                 public void Resolve ()
448                 {
449                         UriFormatException error = null;
450                         UnitTestUriParser p = new UnitTestUriParser ();
451                         Assert.AreEqual (full_http, p._Resolve (http, http, out error), "http-http");
452                 }
453
454                 [Test]
455                 [Category ("NotWorking")]
456                 public void Resolve_UriNull ()
457                 {
458                         UriFormatException error = null;
459                         UnitTestUriParser p = new UnitTestUriParser ();
460                         Assert.AreEqual (full_http, p._Resolve (http, null, out error), "http-http");
461                 }
462
463                 [Test]
464                 [ExpectedException (typeof (NullReferenceException))]
465                 [Category ("NotWorking")]
466                 public void Resolve_NullUri ()
467                 {
468                         UriFormatException error = null;
469                         UnitTestUriParser p = new UnitTestUriParser ();
470                         p._Resolve (null, http, out error);
471                         p._Resolve (http, null, out error);
472                 }
473
474                 [Test]
475                 public void IsKnownScheme_WellKnown ()
476                 {
477                         // from Uri.UriScheme* fields
478                         Assert.IsTrue (UriParser.IsKnownScheme ("file"), "file");
479                         Assert.IsTrue (UriParser.IsKnownScheme ("ftp"), "ftp");
480                         Assert.IsTrue (UriParser.IsKnownScheme ("gopher"), "gopher");
481                         Assert.IsTrue (UriParser.IsKnownScheme ("http"), "http");
482                         Assert.IsTrue (UriParser.IsKnownScheme ("https"), "https");
483                         Assert.IsTrue (UriParser.IsKnownScheme ("mailto"), "mailto");
484                         Assert.IsTrue (UriParser.IsKnownScheme ("net.pipe"), "net.pipe");
485                         Assert.IsTrue (UriParser.IsKnownScheme ("net.tcp"), "net.tcp");
486                         Assert.IsTrue (UriParser.IsKnownScheme ("news"), "news");
487                         Assert.IsTrue (UriParser.IsKnownScheme ("nntp"), "nntp");
488                         // inferred from class library
489                         Assert.IsTrue (UriParser.IsKnownScheme ("ldap"), "ldap");
490                         Assert.IsFalse (UriParser.IsKnownScheme ("ldaps"), "ldaps");
491                         // well known for not existing
492                         Assert.IsFalse (UriParser.IsKnownScheme ("unknown"), "unknown");
493
494                         // variations - mixed and upper case
495                         Assert.IsTrue (UriParser.IsKnownScheme ("FiLe"), "FiLe");
496                         Assert.IsTrue (UriParser.IsKnownScheme ("FTP"), "ftp");
497
498                         // see 496783
499                         Assert.IsFalse (UriParser.IsKnownScheme ("tcp"), "tcp");
500                 }
501
502                 [Test]
503                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
504                 [Category ("NotWorking")]
505                 public void IsKnownScheme_ExtraSpace ()
506                 {
507                         // same result for space before, inside or after the scheme
508                         UriParser.IsKnownScheme ("ht tp");
509                         // this is undocumented (and I hate exceptions in a boolean method)
510                 }
511
512                 [Test]
513                 [ExpectedException (typeof (ArgumentNullException))]
514                 public void IsKnownScheme_Null ()
515                 {
516                         UriParser.IsKnownScheme (null);
517                 }
518
519                 [Test]
520                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
521                 public void IsKnownScheme_Empty ()
522                 {
523                         UriParser.IsKnownScheme (String.Empty);
524                 }
525
526                 [Test]
527                 public void Register ()
528                 {
529                         string scheme = prefix + "register.mono";
530                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
531
532                         UnitTestUriParser p = new UnitTestUriParser ();
533                         UriParser.Register (p, scheme, 2005);
534                         Assert.AreEqual (scheme, p.SchemeName, "SchemeName");
535                         Assert.AreEqual (2005, p.DefaultPort, "DefaultPort");
536
537                         Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
538                 }
539
540                 [Test]
541                 [ExpectedException (typeof (ArgumentNullException))]
542                 public void Register_NullParser ()
543                 {
544                         UriParser.Register (null, prefix + "null.parser", 2006);
545                 }
546
547                 [Test]
548                 [ExpectedException (typeof (ArgumentNullException))]
549                 public void Register_NullScheme ()
550                 {
551                         UnitTestUriParser p = new UnitTestUriParser ();
552                         UriParser.Register (p, null, 2006);
553                 }
554
555                 [Test]
556                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
557                 public void Register_NegativePort ()
558                 {
559                         UnitTestUriParser p = new UnitTestUriParser ();
560                         UriParser.Register (p, prefix + "negative.port", -2);
561                 }
562
563                 [Test]
564                 public void Register_Minus1Port ()
565                 {
566                         UnitTestUriParser p = new UnitTestUriParser ();
567                         UriParser.Register (p, prefix + "minus1.port", -1);
568                 }
569
570                 [Test]
571                 public void Register_UInt16PortMinus1 ()
572                 {
573                         UnitTestUriParser p = new UnitTestUriParser ();
574                         UriParser.Register (p, prefix + "uint16.minus.1.port", UInt16.MaxValue - 1);
575                 }
576
577                 [Test]
578                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
579                 public void Register_TooBigPort ()
580                 {
581                         UnitTestUriParser p = new UnitTestUriParser ();
582                         UriParser.Register (p, prefix + "too.big.port", UInt16.MaxValue);
583                 }
584
585                 [Test]
586                 [ExpectedException (typeof (InvalidOperationException))]
587                 public void ReRegister ()
588                 {
589                         string scheme = prefix + "re.register.mono";
590                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
591                         UnitTestUriParser p = new UnitTestUriParser ();
592                         UriParser.Register (p, scheme, 2005);
593                         Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
594                         UriParser.Register (p, scheme, 2006);
595                 }
596         }
597 }
598
599 #endif