2 // UriParserTest.cs - Unit tests for System.UriParser
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
36 namespace MonoTests.System {
38 public class UnitTestUriParser: UriParser {
40 private string scheme_name;
41 private int default_port;
42 private bool throw_on_register;
43 private bool on_new_uri_called;
45 public UnitTestUriParser ()
49 public UnitTestUriParser (bool throwOnRegister)
51 throw_on_register = throwOnRegister;
54 public string SchemeName
62 public int DefaultPort
70 public bool OnNewUriCalled
74 return on_new_uri_called;
78 public string _GetComponents (Uri uri, UriComponents components, UriFormat format)
80 return base.GetComponents (uri, components, format);
83 public void _InitializeAndValidate (Uri uri, out UriFormatException parserError)
85 base.InitializeAndValidate (uri, out parserError);
88 public bool _IsBaseOf (Uri baseUri, Uri relativeUri)
90 return base.IsBaseOf (baseUri, relativeUri);
93 public bool _IsWellFormedOriginalString (Uri uri)
95 return base.IsWellFormedOriginalString (uri);
98 public UriParser _OnNewUri ()
100 return base.OnNewUri ();
103 public void _OnRegister (string schemeName, int defaultPort)
105 base.OnRegister (schemeName, defaultPort);
108 public string _Resolve (Uri baseUri, Uri relativeUri, out UriFormatException parserError)
110 return base.Resolve (baseUri, relativeUri, out parserError);
113 protected override UriParser OnNewUri ()
115 on_new_uri_called = true;
116 return base.OnNewUri ();
119 protected override void OnRegister (string schemeName, int defaultPort)
121 if (throw_on_register)
122 throw new NotSupportedException ();
123 scheme_name = schemeName;
124 default_port = defaultPort;
125 base.OnRegister (schemeName, defaultPort);
130 public class UriParserTest {
132 private const string full_http = "http://www.mono-project.com/Main_Page#FAQ?Edit";
134 private string prefix;
139 public void FixtureSetUp ()
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");
159 public void GetComponents ()
161 UnitTestUriParser p = new UnitTestUriParser ();
162 Assert.AreEqual ("http", p._GetComponents (http, UriComponents.Scheme, UriFormat.SafeUnescaped), "http.Scheme");
163 Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.UserInfo, UriFormat.SafeUnescaped), "http.UserInfo");
164 Assert.AreEqual ("www.mono-project.com", p._GetComponents (http, UriComponents.Host, UriFormat.SafeUnescaped), "http.Host");
165 Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.Port, UriFormat.SafeUnescaped), "http.Port");
166 Assert.AreEqual ("Main_Page", p._GetComponents (http, UriComponents.Path, UriFormat.SafeUnescaped), "http.Path");
167 Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.Query, UriFormat.SafeUnescaped), "http.Query");
168 Assert.AreEqual ("FAQ?Edit", p._GetComponents (http, UriComponents.Fragment, UriFormat.SafeUnescaped), "http.Fragment");
169 Assert.AreEqual ("80", p._GetComponents (http, UriComponents.StrongPort, UriFormat.SafeUnescaped), "http.StrongPort");
170 Assert.AreEqual (String.Empty, p._GetComponents (http, UriComponents.KeepDelimiter, UriFormat.SafeUnescaped), "http.KeepDelimiter");
171 Assert.AreEqual ("www.mono-project.com:80", p._GetComponents (http, UriComponents.HostAndPort, UriFormat.SafeUnescaped), "http.HostAndPort");
172 Assert.AreEqual ("www.mono-project.com:80", p._GetComponents (http, UriComponents.StrongAuthority, UriFormat.SafeUnescaped), "http.StrongAuthority");
173 Assert.AreEqual (full_http, p._GetComponents (http, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped), "http.AbsoluteUri");
174 Assert.AreEqual ("/Main_Page", p._GetComponents (http, UriComponents.PathAndQuery, UriFormat.SafeUnescaped), "http.PathAndQuery");
175 Assert.AreEqual ("http://www.mono-project.com/Main_Page", p._GetComponents (http, UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped), "http.HttpRequestUrl");
176 Assert.AreEqual ("http://www.mono-project.com", p._GetComponents (http, UriComponents.SchemeAndServer, UriFormat.SafeUnescaped), "http.SchemeAndServer");
177 Assert.AreEqual (full_http, p._GetComponents (http, UriComponents.SerializationInfoString, UriFormat.SafeUnescaped), "http.SerializationInfoString");
179 Assert.AreEqual ("http://", p._GetComponents (http, UriComponents.Scheme | UriComponents.Port, UriFormat.SafeUnescaped), "http.Scheme+Port");
180 Assert.AreEqual ("www.mono-project.com#FAQ?Edit", p._GetComponents (http, UriComponents.Host | UriComponents.Fragment, UriFormat.SafeUnescaped), "http.Scheme+Port");
181 Assert.AreEqual ("/Main_Page", p._GetComponents (http, UriComponents.Port | UriComponents.Path, UriFormat.SafeUnescaped), "http.Scheme+Port");
182 Assert.AreSame (p, p._OnNewUri (), "OnNewUri");
186 public void GetComponents_Ftp ()
188 UnitTestUriParser p = new UnitTestUriParser ();
189 Assert.AreEqual ("ftp", p._GetComponents (ftp, UriComponents.Scheme, UriFormat.Unescaped), "ftp.Scheme");
190 Assert.AreEqual ("username:password", p._GetComponents (ftp, UriComponents.UserInfo, UriFormat.Unescaped), "ftp.UserInfo");
191 Assert.AreEqual ("ftp.go-mono.com", p._GetComponents (ftp, UriComponents.Host, UriFormat.Unescaped), "ftp.Host");
192 Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Port, UriFormat.Unescaped), "ftp.Port");
193 Assert.AreEqual ("with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.Unescaped), "ftp.Path");
194 Assert.AreEqual ("with%20some%20spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.UriEscaped), "ftp.Path-UriEscaped");
195 Assert.AreEqual ("with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path, UriFormat.SafeUnescaped), "ftp.Path-SafeUnescaped");
196 Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Query, UriFormat.Unescaped), "ftp.Query");
197 Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.Fragment, UriFormat.Unescaped), "ftp.Fragment");
198 Assert.AreEqual ("21", p._GetComponents (ftp, UriComponents.StrongPort, UriFormat.Unescaped), "ftp.StrongPort");
199 Assert.AreEqual (String.Empty, p._GetComponents (ftp, UriComponents.KeepDelimiter, UriFormat.Unescaped), "http.KeepDelimiter");
200 Assert.AreEqual ("ftp.go-mono.com:21", p._GetComponents (ftp, UriComponents.HostAndPort, UriFormat.Unescaped), "http.HostAndPort");
201 Assert.AreEqual ("username:password@ftp.go-mono.com:21", p._GetComponents (ftp, UriComponents.StrongAuthority, UriFormat.Unescaped), "http.StrongAuthority");
202 Assert.AreEqual ("ftp://username:password@ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.AbsoluteUri, UriFormat.Unescaped), "http.AbsoluteUri");
203 Assert.AreEqual ("/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.PathAndQuery, UriFormat.Unescaped), "http.PathAndQuery");
204 Assert.AreEqual ("ftp://ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.HttpRequestUrl, UriFormat.Unescaped), "http.HttpRequestUrl");
205 Assert.AreEqual ("ftp://ftp.go-mono.com", p._GetComponents (ftp, UriComponents.SchemeAndServer, UriFormat.Unescaped), "http.SchemeAndServer");
206 Assert.AreEqual ("ftp://username:password@ftp.go-mono.com/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.SerializationInfoString, UriFormat.Unescaped), "http.SerializationInfoString");
207 Assert.AreSame (p, p._OnNewUri (), "OnNewUri");
209 Assert.AreEqual ("ftp://username:password@", p._GetComponents (ftp, UriComponents.Scheme | UriComponents.UserInfo, UriFormat.Unescaped), "ftp.Scheme+UserInfo");
210 Assert.AreEqual (":21/with some spaces/mono.tgz", p._GetComponents (ftp, UriComponents.Path | UriComponents.StrongPort, UriFormat.Unescaped), "ftp.Path+StrongPort");
214 [ExpectedException (typeof (NullReferenceException))]
215 public void GetComponents_Null ()
217 UnitTestUriParser p = new UnitTestUriParser ();
218 p._GetComponents (null, UriComponents.Host, UriFormat.SafeUnescaped);
222 public void GetComponents_BadUriComponents ()
224 UnitTestUriParser p = new UnitTestUriParser ();
225 Assert.AreEqual (full_http, p._GetComponents (http, (UriComponents) Int32.MinValue, UriFormat.SafeUnescaped), "http");
229 [ExpectedException (typeof (ArgumentOutOfRangeException))]
230 public void GetComponents_BadUriFormat ()
232 UnitTestUriParser p = new UnitTestUriParser ();
233 p._GetComponents (http, UriComponents.Host, (UriFormat) Int32.MinValue);
237 public void InitializeAndValidate ()
239 UriFormatException error = null;
240 UnitTestUriParser p = new UnitTestUriParser ();
241 p._InitializeAndValidate (http, out error);
242 Assert.IsNotNull (error, "out"); // authority/host couldn't be parsed ?!?!
246 [ExpectedException (typeof (NullReferenceException))]
247 // oh man, this is a bad boy.It should be ArgumentNullException.
248 public void InitializeAndValidate_Null ()
250 UriFormatException error = null;
251 UnitTestUriParser p = new UnitTestUriParser ();
252 p._InitializeAndValidate (null, out error);
256 public void IsBaseOf ()
258 UnitTestUriParser p = new UnitTestUriParser ();
259 Assert.IsTrue (p._IsBaseOf (http, http), "http-http");
261 Uri u = new Uri ("http://www.mono-project.com/Main_Page#FAQ");
262 Assert.IsTrue (p._IsBaseOf (u, http), "http-1a");
263 Assert.IsTrue (p._IsBaseOf (http, u), "http-1b");
265 u = new Uri ("http://www.mono-project.com/Main_Page");
266 Assert.IsTrue (p._IsBaseOf (u, http), "http-2a");
267 Assert.IsTrue (p._IsBaseOf (http, u), "http-2b");
269 u = new Uri ("http://www.mono-project.com/");
270 Assert.IsTrue (p._IsBaseOf (u, http), "http-3a");
271 Assert.IsTrue (p._IsBaseOf (http, u), "http-3b");
273 u = new Uri ("http://www.mono-project.com/Main_Page/");
274 Assert.IsFalse (p._IsBaseOf (u, http), "http-4a");
275 Assert.IsTrue (p._IsBaseOf (http, u), "http-4b");
277 // docs says the UserInfo isn't evaluated, but...
278 u = new Uri ("http://username:password@www.mono-project.com/Main_Page");
279 Assert.IsFalse (p._IsBaseOf (u, http), "http-5a");
280 Assert.IsFalse (p._IsBaseOf (http, u), "http-5b");
282 // scheme case sensitive ? no
283 u = new Uri ("HTTP://www.mono-project.com/Main_Page");
284 Assert.IsTrue (p._IsBaseOf (u, http), "http-6a");
285 Assert.IsTrue (p._IsBaseOf (http, u), "http-6b");
287 // host case sensitive ? no
288 u = new Uri ("http://www.Mono-Project.com/Main_Page");
289 Assert.IsTrue (p._IsBaseOf (u, http), "http-7a");
290 Assert.IsTrue (p._IsBaseOf (http, u), "http-7b");
292 // path case sensitive ? no
293 u = new Uri ("http://www.Mono-Project.com/MAIN_Page");
294 Assert.IsTrue (p._IsBaseOf (u, http), "http-8a");
295 Assert.IsTrue (p._IsBaseOf (http, u), "http-8b");
298 u = new Uri ("ftp://www.mono-project.com/Main_Page");
299 Assert.IsFalse (p._IsBaseOf (u, http), "http-9a");
300 Assert.IsFalse (p._IsBaseOf (http, u), "http-9b");
303 u = new Uri ("http://www.go-mono.com/Main_Page");
304 Assert.IsFalse (p._IsBaseOf (u, http), "http-10a");
305 Assert.IsFalse (p._IsBaseOf (http, u), "http-10b");
308 u = new Uri ("http://www.mono-project.com:8080/");
309 Assert.IsFalse (p._IsBaseOf (u, http), "http-11a");
310 Assert.IsFalse (p._IsBaseOf (http, u), "http-11b");
312 // specify default port
313 u = new Uri ("http://www.mono-project.com:80/");
314 Assert.IsTrue (p._IsBaseOf (u, http), "http-12a");
315 Assert.IsTrue (p._IsBaseOf (http, u), "http-12b");
319 [ExpectedException (typeof (NullReferenceException))]
320 public void IsBaseOf_UriNull ()
322 UnitTestUriParser p = new UnitTestUriParser ();
323 p._IsBaseOf (http, null);
327 [ExpectedException (typeof (NullReferenceException))]
328 public void IsBaseOf_NullUri ()
330 UnitTestUriParser p = new UnitTestUriParser ();
331 p._IsBaseOf (null, http);
335 [Category ("NotWorking")]
336 public void IsWellFormedOriginalString ()
338 UnitTestUriParser p = new UnitTestUriParser ();
339 Assert.IsTrue (p._IsWellFormedOriginalString (http), "http");
343 [ExpectedException (typeof (NullReferenceException))]
344 [Category ("NotWorking")]
345 public void IsWellFormedOriginalString_Null ()
347 UnitTestUriParser p = new UnitTestUriParser ();
348 p._IsWellFormedOriginalString (null);
352 [Category ("NotWorking")]
353 public void OnNewUri ()
355 string scheme = prefix + "on.new.uri";
356 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
358 UnitTestUriParser p = new UnitTestUriParser ();
359 UriParser.Register (p, scheme, 1999);
361 Assert.IsFalse (p.OnNewUriCalled, "!Called");
362 Uri uri = new Uri (scheme + "://www.mono-project.com");
363 Assert.IsTrue (p.OnNewUriCalled, "Called");
367 public void OnRegister ()
369 string scheme = prefix + "onregister";
370 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
371 UnitTestUriParser p = new UnitTestUriParser ();
373 UriParser.Register (p, scheme, 2005);
375 catch (NotSupportedException) {
376 // special case / ordering
378 // if true then the registration is done before calling OnRegister
379 Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
383 [Category ("NotWorking")]
384 public void OnRegister2 ()
386 string scheme = prefix + "onregister2";
387 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
388 UnitTestUriParser p = new UnitTestUriParser ();
390 UriParser.Register (p, scheme, 2005);
391 Uri uri = new Uri (scheme + "://foobar:2005");
392 Assert.AreEqual (scheme, uri.Scheme, "uri-prefix");
393 Assert.AreEqual (2005, uri.Port, "uri-port");
395 Assert.AreEqual ("//foobar:2005", uri.LocalPath, "uri-localpath");
397 catch (NotSupportedException) {
398 // special case / ordering
400 // if true then the registration is done before calling OnRegister
401 Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
405 [Category ("NotWorking")]
406 public void Resolve ()
408 UriFormatException error = null;
409 UnitTestUriParser p = new UnitTestUriParser ();
410 Assert.AreEqual (full_http, p._Resolve (http, http, out error), "http-http");
414 [Category ("NotWorking")]
415 public void Resolve_UriNull ()
417 UriFormatException error = null;
418 UnitTestUriParser p = new UnitTestUriParser ();
419 Assert.AreEqual (full_http, p._Resolve (http, null, out error), "http-http");
423 [ExpectedException (typeof (NullReferenceException))]
424 [Category ("NotWorking")]
425 public void Resolve_NullUri ()
427 UriFormatException error = null;
428 UnitTestUriParser p = new UnitTestUriParser ();
429 p._Resolve (null, http, out error);
430 p._Resolve (http, null, out error);
434 public void IsKnownScheme_WellKnown ()
436 // from Uri.UriScheme* fields
437 Assert.IsTrue (UriParser.IsKnownScheme ("file"), "file");
438 Assert.IsTrue (UriParser.IsKnownScheme ("ftp"), "ftp");
439 Assert.IsTrue (UriParser.IsKnownScheme ("gopher"), "gopher");
440 Assert.IsTrue (UriParser.IsKnownScheme ("http"), "http");
441 Assert.IsTrue (UriParser.IsKnownScheme ("https"), "https");
442 Assert.IsTrue (UriParser.IsKnownScheme ("mailto"), "mailto");
443 Assert.IsTrue (UriParser.IsKnownScheme ("net.pipe"), "net.pipe");
444 Assert.IsTrue (UriParser.IsKnownScheme ("net.tcp"), "net.tcp");
445 Assert.IsTrue (UriParser.IsKnownScheme ("news"), "news");
446 Assert.IsTrue (UriParser.IsKnownScheme ("nntp"), "nntp");
447 // inferred from class library
448 Assert.IsTrue (UriParser.IsKnownScheme ("ldap"), "ldap");
449 Assert.IsFalse (UriParser.IsKnownScheme ("ldaps"), "ldaps");
450 // well known for not existing
451 Assert.IsFalse (UriParser.IsKnownScheme ("unknown"), "unknown");
453 // variations - mixed and upper case
454 Assert.IsTrue (UriParser.IsKnownScheme ("FiLe"), "FiLe");
455 Assert.IsTrue (UriParser.IsKnownScheme ("FTP"), "ftp");
458 Assert.IsFalse (UriParser.IsKnownScheme ("tcp"), "tcp");
462 [ExpectedException (typeof (ArgumentOutOfRangeException))]
463 [Category ("NotWorking")]
464 public void IsKnownScheme_ExtraSpace ()
466 // same result for space before, inside or after the scheme
467 UriParser.IsKnownScheme ("ht tp");
468 // this is undocumented (and I hate exceptions in a boolean method)
472 [ExpectedException (typeof (ArgumentNullException))]
473 public void IsKnownScheme_Null ()
475 UriParser.IsKnownScheme (null);
479 [ExpectedException (typeof (ArgumentOutOfRangeException))]
480 public void IsKnownScheme_Empty ()
482 UriParser.IsKnownScheme (String.Empty);
486 public void Register ()
488 string scheme = prefix + "register.mono";
489 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
491 UnitTestUriParser p = new UnitTestUriParser ();
492 UriParser.Register (p, scheme, 2005);
493 Assert.AreEqual (scheme, p.SchemeName, "SchemeName");
494 Assert.AreEqual (2005, p.DefaultPort, "DefaultPort");
496 Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
500 [ExpectedException (typeof (ArgumentNullException))]
501 public void Register_NullParser ()
503 UriParser.Register (null, prefix + "null.parser", 2006);
507 [ExpectedException (typeof (ArgumentNullException))]
508 public void Register_NullScheme ()
510 UnitTestUriParser p = new UnitTestUriParser ();
511 UriParser.Register (p, null, 2006);
515 [ExpectedException (typeof (ArgumentOutOfRangeException))]
516 public void Register_NegativePort ()
518 UnitTestUriParser p = new UnitTestUriParser ();
519 UriParser.Register (p, prefix + "negative.port", -2);
523 public void Register_Minus1Port ()
525 UnitTestUriParser p = new UnitTestUriParser ();
526 UriParser.Register (p, prefix + "minus1.port", -1);
530 public void Register_UInt16PortMinus1 ()
532 UnitTestUriParser p = new UnitTestUriParser ();
533 UriParser.Register (p, prefix + "uint16.minus.1.port", UInt16.MaxValue - 1);
537 [ExpectedException (typeof (ArgumentOutOfRangeException))]
538 public void Register_TooBigPort ()
540 UnitTestUriParser p = new UnitTestUriParser ();
541 UriParser.Register (p, prefix + "too.big.port", UInt16.MaxValue);
545 [ExpectedException (typeof (InvalidOperationException))]
546 public void ReRegister ()
548 string scheme = prefix + "re.register.mono";
549 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
550 UnitTestUriParser p = new UnitTestUriParser ();
551 UriParser.Register (p, scheme, 2005);
552 Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
553 UriParser.Register (p, scheme, 2006);