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 {
55 get { return scheme_name; }
58 public int DefaultPort {
59 get { return default_port; }
62 public bool OnNewUriCalled {
63 get { return on_new_uri_called; }
66 public string _GetComponents (Uri uri, UriComponents components, UriFormat format)
68 return base.GetComponents (uri, components, format);
71 public void _InitializeAndValidate (Uri uri, out UriFormatException parserError)
73 base.InitializeAndValidate (uri, out parserError);
76 public bool _IsBaseOf (Uri baseUri, Uri relativeUri)
78 return base.IsBaseOf (baseUri, relativeUri);
81 public bool _IsWellFormedOriginalString (Uri uri)
83 return base.IsWellFormedOriginalString (uri);
86 public UriParser _OnNewUri ()
88 return base.OnNewUri ();
91 public void _OnRegister (string schemeName, int defaultPort)
93 base.OnRegister (schemeName, defaultPort);
96 public string _Resolve (Uri baseUri, Uri relativeUri, out UriFormatException parserError)
98 return base.Resolve (baseUri, relativeUri, out parserError);
101 protected override UriParser OnNewUri ()
103 on_new_uri_called = true;
104 return base.OnNewUri ();
107 protected override void OnRegister (string schemeName, int defaultPort)
109 if (throw_on_register)
110 throw new NotSupportedException ();
111 scheme_name = schemeName;
112 default_port = defaultPort;
113 base.OnRegister (schemeName, defaultPort);
118 public class UriParserTest {
120 private string prefix;
124 public void FixtureSetUp ()
126 prefix = "unit.test.";
127 http = new Uri ("http://www.mono-project.com");
130 public string Prefix {
131 get { return prefix; }
132 set { prefix = value; }
136 [Category ("NotWorking")]
137 public void GetComponents ()
139 UnitTestUriParser p = new UnitTestUriParser ();
140 Assert.AreEqual ("www.mono-project.com", p._GetComponents (http, UriComponents.Host, UriFormat.SafeUnescaped), "http");
141 Assert.AreSame (p, p._OnNewUri (), "OnNewUri");
145 [ExpectedException (typeof (NullReferenceException))]
146 [Category ("NotWorking")]
147 public void GetComponents_Null ()
149 UnitTestUriParser p = new UnitTestUriParser ();
150 p._GetComponents (null, UriComponents.Host, UriFormat.SafeUnescaped);
154 [Category ("NotWorking")]
155 public void GetComponents_BadUriComponents ()
157 UnitTestUriParser p = new UnitTestUriParser ();
158 Assert.AreEqual ("http://www.mono-project.com/", p._GetComponents (http, (UriComponents) Int32.MinValue, UriFormat.SafeUnescaped), "http");
162 [ExpectedException (typeof (ArgumentOutOfRangeException))]
163 public void GetComponents_BadUriFormat ()
165 UnitTestUriParser p = new UnitTestUriParser ();
166 p._GetComponents (http, UriComponents.Host, (UriFormat)Int32.MinValue);
170 [Category ("NotWorking")]
171 public void InitializeAndValidate ()
173 UriFormatException error = null;
174 UnitTestUriParser p = new UnitTestUriParser ();
175 p._InitializeAndValidate (http, out error);
176 Assert.IsNotNull (error, "out"); // authority/host couldn't be parsed ?!?!
180 [ExpectedException (typeof (NullReferenceException))]
181 [Category ("NotWorking")]
182 public void InitializeAndValidate_Null ()
184 UriFormatException error = null;
185 UnitTestUriParser p = new UnitTestUriParser ();
186 p._InitializeAndValidate (null, out error);
190 [Category ("NotWorking")]
191 public void IsBaseOf ()
193 UnitTestUriParser p = new UnitTestUriParser ();
194 Assert.IsTrue (p._IsBaseOf (http, http), "http-http");
198 [ExpectedException (typeof (NullReferenceException))]
199 [Category ("NotWorking")]
200 public void IsBaseOf_UriNull ()
202 UnitTestUriParser p = new UnitTestUriParser ();
203 p._IsBaseOf (http, null);
207 [ExpectedException (typeof (NullReferenceException))]
208 [Category ("NotWorking")]
209 public void IsBaseOf_NullUri ()
211 UnitTestUriParser p = new UnitTestUriParser ();
212 p._IsBaseOf (null, http);
216 [Category ("NotWorking")]
217 public void IsWellFormedOriginalString ()
219 UnitTestUriParser p = new UnitTestUriParser ();
220 Assert.IsTrue (p._IsWellFormedOriginalString (http), "http");
224 [ExpectedException (typeof (NullReferenceException))]
225 [Category ("NotWorking")]
226 public void IsWellFormedOriginalString_Null ()
228 UnitTestUriParser p = new UnitTestUriParser ();
229 p._IsWellFormedOriginalString (null);
233 [Category ("NotWorking")]
234 public void OnNewUri ()
236 string scheme = prefix + "on.new.uri";
237 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
239 UnitTestUriParser p = new UnitTestUriParser ();
240 UriParser.Register (p, scheme, 1999);
242 Assert.IsFalse (p.OnNewUriCalled, "!Called");
243 Uri uri = new Uri (scheme + "://www.mono-project.com");
244 Assert.IsTrue (p.OnNewUriCalled, "Called");
248 public void OnRegister ()
250 string scheme = prefix + "onregister";
251 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
252 UnitTestUriParser p = new UnitTestUriParser ();
254 UriParser.Register (p, scheme, 2005);
256 catch (NotSupportedException) {
257 // special case / ordering
259 // if true then the registration is done before calling OnRegister
260 Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
264 [Category ("NotWorking")]
265 public void Resolve ()
267 UriFormatException error = null;
268 UnitTestUriParser p = new UnitTestUriParser ();
269 Assert.AreEqual ("http://www.mono-project.com", p._Resolve (http, http, out error), "http-http");
273 [Category ("NotWorking")]
274 public void Resolve_UriNull ()
276 UriFormatException error = null;
277 UnitTestUriParser p = new UnitTestUriParser ();
278 Assert.AreEqual ("http://www.mono-project.com", p._Resolve (http, null, out error), "http-http");
282 [ExpectedException (typeof (NullReferenceException))]
283 [Category ("NotWorking")]
284 public void Resolve_NullUri ()
286 UriFormatException error = null;
287 UnitTestUriParser p = new UnitTestUriParser ();
288 p._Resolve (null, http, out error);
289 p._Resolve (http, null, out error);
293 public void IsKnownScheme_WellKnown ()
295 // from Uri.UriScheme* fields
296 Assert.IsTrue (UriParser.IsKnownScheme ("file"), "file");
297 Assert.IsTrue (UriParser.IsKnownScheme ("ftp"), "ftp");
298 Assert.IsTrue (UriParser.IsKnownScheme ("gopher"), "gopher");
299 Assert.IsTrue (UriParser.IsKnownScheme ("http"), "http");
300 Assert.IsTrue (UriParser.IsKnownScheme ("https"), "https");
301 Assert.IsTrue (UriParser.IsKnownScheme ("mailto"), "mailto");
302 Assert.IsTrue (UriParser.IsKnownScheme ("net.pipe"), "net.pipe");
303 Assert.IsTrue (UriParser.IsKnownScheme ("net.tcp"), "net.tcp");
304 Assert.IsTrue (UriParser.IsKnownScheme ("news"), "news");
305 Assert.IsTrue (UriParser.IsKnownScheme ("nntp"), "nntp");
306 // infered from class library
307 Assert.IsTrue (UriParser.IsKnownScheme ("ldap"), "ldap");
308 Assert.IsFalse (UriParser.IsKnownScheme ("ldaps"), "ldaps");
309 // well known for not existing
310 Assert.IsFalse (UriParser.IsKnownScheme ("unknown"), "unknown");
312 // variations - mixed and upper case
313 Assert.IsTrue (UriParser.IsKnownScheme ("FiLe"), "FiLe");
314 Assert.IsTrue (UriParser.IsKnownScheme ("FTP"), "ftp");
318 [ExpectedException (typeof (ArgumentOutOfRangeException))]
319 [Category ("NotWorking")]
320 public void IsKnownScheme_ExtraSpace ()
322 // same result for space before, inside or after the scheme
323 UriParser.IsKnownScheme ("ht tp");
324 // this is undocumented (and I hate exceptions in a boolean method)
328 [ExpectedException (typeof (ArgumentNullException))]
329 public void IsKnownScheme_Null ()
331 UriParser.IsKnownScheme (null);
335 [ExpectedException (typeof (ArgumentOutOfRangeException))]
336 public void IsKnownScheme_Empty ()
338 UriParser.IsKnownScheme (String.Empty);
342 public void Register ()
344 string scheme = prefix + "register.mono";
345 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
347 UnitTestUriParser p = new UnitTestUriParser ();
348 UriParser.Register (p, scheme, 2005);
349 Assert.AreEqual (scheme, p.SchemeName, "SchemeName");
350 Assert.AreEqual (2005, p.DefaultPort, "DefaultPort");
352 Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
356 [ExpectedException (typeof (ArgumentNullException))]
357 public void Register_NullParser ()
359 UriParser.Register (null, prefix + "null.parser", 2006);
363 [ExpectedException (typeof (ArgumentNullException))]
364 public void Register_NullScheme ()
366 UnitTestUriParser p = new UnitTestUriParser ();
367 UriParser.Register (p, null, 2006);
371 [ExpectedException (typeof (ArgumentOutOfRangeException))]
372 public void Register_NegativePort ()
374 UnitTestUriParser p = new UnitTestUriParser ();
375 UriParser.Register (p, prefix + "negative.port", -2);
379 public void Register_Minus1Port ()
381 UnitTestUriParser p = new UnitTestUriParser ();
382 UriParser.Register (p, prefix + "minus1.port", -1);
386 public void Register_UInt16PortMinus1 ()
388 UnitTestUriParser p = new UnitTestUriParser ();
389 UriParser.Register (p, prefix + "uint16.minus.1.port", UInt16.MaxValue - 1);
393 [ExpectedException (typeof (ArgumentOutOfRangeException))]
394 public void Register_TooBigPort ()
396 UnitTestUriParser p = new UnitTestUriParser ();
397 UriParser.Register (p, prefix + "too.big.port", UInt16.MaxValue);
401 [ExpectedException (typeof (InvalidOperationException))]
402 public void ReRegister ()
404 string scheme = prefix + "re.register.mono";
405 Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
406 UnitTestUriParser p = new UnitTestUriParser ();
407 UriParser.Register (p, scheme, 2005);
408 Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
409 UriParser.Register (p, scheme, 2006);