2 // UriTest3.cs - Even more (2.0 specific) unit tests for System.Uri
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.
29 using NUnit
.Framework
;
33 namespace MonoTests
.System
38 private const string absolute
= "http://www.example.com/CAS";
39 private const string relative
= "server.com/directory/";
41 [Test
] // .ctor (String, UriKind)
42 public void Constructor4_UriKind_Invalid ()
45 new Uri ("http://www.contoso.com", (UriKind
) 666);
47 } catch (ArgumentException ex
) {
48 // The value '666' passed for the UriKind parameter
50 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
51 Assert
.IsNull (ex
.InnerException
, "#3");
52 Assert
.IsNotNull (ex
.Message
, "#4");
53 Assert
.IsNotNull (ex
.Message
.IndexOf ("'666'") != -1, "#5:" + ex
.Message
);
54 Assert
.IsNotNull (ex
.Message
.IndexOf ("UriKind") != -1, "#6:" + ex
.Message
);
55 Assert
.IsNull (ex
.ParamName
, "#7");
59 [Test
] // .ctor (String, UriKind)
60 public void Constructor4_UriString_Null ()
63 new Uri ((string) null, (UriKind
) 666);
65 } catch (ArgumentNullException ex
) {
66 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
67 Assert
.IsNull (ex
.InnerException
, "#3");
68 Assert
.IsNotNull (ex
.Message
, "#4");
69 Assert
.AreEqual ("uriString", ex
.ParamName
, "#5");
74 public void AbsoluteUri_RelativeUri ()
76 Uri uri1
= new Uri ("http://www.contoso.com/index.htm?x=2");
77 Uri uri2
= new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
78 Uri relativeUri
= uri1
.MakeRelativeUri (uri2
);
81 string result
= relativeUri
.AbsoluteUri
;
82 Assert
.Fail ("#1: " + result
);
83 } catch (InvalidOperationException ex
) {
84 // This operation is not supported for a relative URI
85 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
86 Assert
.IsNull (ex
.InnerException
, "#3");
87 Assert
.IsNotNull (ex
.Message
, "#4");
92 public void Absolute_UriKind_Absolute ()
94 Uri uri
= new Uri (absolute
, UriKind
.Absolute
);
95 Assert
.AreEqual ("www.example.com", uri
.DnsSafeHost
, "#1");
96 Assert
.IsTrue (uri
.IsAbsoluteUri
, "#2");
97 Assert
.AreEqual (absolute
, uri
.OriginalString
, "#3");
98 Assert
.AreEqual (absolute
, uri
.ToString (), "#4");
99 Assert
.IsFalse (uri
.UserEscaped
, "#5");
103 public void Relative_UriKind_Relative ()
105 Uri uri
= new Uri (relative
, UriKind
.Relative
);
106 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#A1");
107 Assert
.AreEqual (relative
, uri
.OriginalString
, "#A2");
108 Assert
.AreEqual (relative
, uri
.ToString (), "#A3");
109 Assert
.IsFalse (uri
.UserEscaped
, "#A4");
111 uri
= new Uri (string.Empty
, UriKind
.Relative
);
112 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#B1");
113 Assert
.AreEqual (string.Empty
, uri
.OriginalString
, "#B2");
114 Assert
.AreEqual (string.Empty
, uri
.ToString (), "#B3");
115 Assert
.IsFalse (uri
.UserEscaped
, "#B4");
117 uri
= new Uri ("foo/bar", UriKind
.Relative
);
118 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#C1");
119 Assert
.AreEqual ("foo/bar", uri
.OriginalString
, "#C2");
120 Assert
.AreEqual ("foo/bar", uri
.ToString (), "#C3");
121 Assert
.IsFalse (uri
.UserEscaped
, "#C4");
123 uri
= new Uri ("/test.aspx", UriKind
.Relative
);
124 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#D1");
125 Assert
.AreEqual ("/test.aspx", uri
.OriginalString
, "#D2");
126 Assert
.AreEqual ("/test.aspx", uri
.ToString (), "#D3");
127 Assert
.IsFalse (uri
.UserEscaped
, "#D4");
129 uri
= new Uri ("", UriKind
.Relative
);
130 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#E1");
131 Assert
.AreEqual ("", uri
.OriginalString
, "#E2");
132 Assert
.AreEqual ("", uri
.ToString (), "#E3");
133 Assert
.IsFalse (uri
.UserEscaped
, "#E4");
135 uri
= new Uri ("a", UriKind
.Relative
);
136 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#F1");
137 Assert
.AreEqual ("a", uri
.OriginalString
, "#F2");
138 Assert
.AreEqual ("a", uri
.ToString (), "#F3");
139 Assert
.IsFalse (uri
.UserEscaped
, "#F4");
143 [ExpectedException (typeof (UriFormatException
))]
144 public void Relative_UriKind_Absolute ()
146 new Uri (relative
, UriKind
.Absolute
);
150 [ExpectedException (typeof (UriFormatException
))]
151 public void Absolute_UriKind_Relative ()
153 new Uri (absolute
, UriKind
.Relative
);
156 [Test
] // TryCreate (String, UriKind, Uri)
157 public void TryCreate1 ()
161 uri
= new Uri ("http://dummy.com");
162 Assert
.IsTrue (Uri
.TryCreate (absolute
, UriKind
.Absolute
, out uri
), "absolute-Absolute");
163 Assert
.AreEqual (absolute
, uri
.AbsoluteUri
, "absolute-Absolute-AbsoluteUri");
165 uri
= new Uri ("http://dummy.com");
166 Assert
.IsTrue (Uri
.TryCreate (absolute
, UriKind
.RelativeOrAbsolute
, out uri
), "absolute-RelativeOrAbsolute");
167 Assert
.AreEqual (absolute
, uri
.AbsoluteUri
, "absolute-RelativeOrAbsolute-AbsoluteUri");
169 uri
= new Uri ("http://dummy.com");
170 Assert
.IsFalse (Uri
.TryCreate (absolute
, UriKind
.Relative
, out uri
), "absolute-Relative");
171 Assert
.IsNull (uri
, "absolute-Relative-uri");
173 uri
= new Uri ("http://dummy.com");
174 Assert
.IsFalse (Uri
.TryCreate (relative
, UriKind
.Absolute
, out uri
), "relative-Absolute");
175 Assert
.IsNull (uri
, "relative-Relative-uri");
177 uri
= new Uri ("http://dummy.com");
178 Assert
.IsTrue (Uri
.TryCreate (relative
, UriKind
.RelativeOrAbsolute
, out uri
), "relative-RelativeOrAbsolute");
179 Assert
.AreEqual (relative
, uri
.OriginalString
, "relative-RelativeOrAbsolute-OriginalString");
181 uri
= new Uri ("http://dummy.com");
182 Assert
.IsTrue (Uri
.TryCreate (relative
, UriKind
.Relative
, out uri
), "relative-Relative");
183 Assert
.AreEqual (relative
, uri
.OriginalString
, "relative-RelativeOrAbsolute-OriginalString");
185 Assert
.IsTrue (Uri
.TryCreate ("http://example.com/☕", UriKind
.Absolute
, out uri
), "highunicode-Absolute");
186 Assert
.AreEqual("http://example.com/%E2%98%95", uri
.AbsoluteUri
, "highunicode-Absolute-AbsoluteUri");
188 string mixedCaseUri
= "http://eXaMpLe.com";
189 uri
= new Uri (mixedCaseUri
);
191 Assert
.IsTrue (Uri
.TryCreate (mixedCaseUri
, UriKind
.Absolute
, out uri2
), "mixedcasehost-absolute");
192 Assert
.AreEqual (uri
.AbsoluteUri
, uri2
.AbsoluteUri
, "mixedcasehost-absoluteuri-absoluteuri");
195 [Test
] // TryCreate (String, UriKind, Uri)
196 public void TryCreate1_UriKind_Invalid ()
198 Uri relativeUri
= new Uri (relative
, UriKind
.Relative
);
199 Uri uri
= relativeUri
;
202 Uri
.TryCreate (absolute
, (UriKind
) 666, out uri
);
204 } catch (ArgumentException ex
) {
205 // The value '666' passed for the UriKind parameter
207 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#A2");
208 Assert
.IsNull (ex
.InnerException
, "#A3");
209 Assert
.IsNotNull (ex
.Message
, "#A4");
210 Assert
.IsNotNull (ex
.Message
.IndexOf ("'666'") != -1, "#A5:" + ex
.Message
);
211 Assert
.IsNotNull (ex
.Message
.IndexOf ("UriKind") != -1, "#A6:" + ex
.Message
);
212 Assert
.IsNull (ex
.ParamName
, "#A7");
214 Assert
.IsNotNull (uri
, "#A8");
215 Assert
.AreSame (relativeUri
, uri
, "#A9");
218 Assert
.IsFalse (Uri
.TryCreate ((string) null, (UriKind
) 666, out uri
), "#B1");
219 Assert
.IsNull (uri
, "#B2");
222 [Test
] // TryCreate (Uri, String, Uri)
223 public void TryCreate2 ()
225 Uri baseUri
= new Uri (absolute
);
228 uri
= new Uri ("http://dummy.com");
229 Assert
.IsTrue (Uri
.TryCreate (baseUri
, relative
, out uri
), "baseUri+relative");
230 Assert
.AreEqual ("http://www.example.com/server.com/directory/", uri
.AbsoluteUri
, "baseUri+relative+AbsoluteUri");
231 Assert
.AreEqual ("http://www.example.com/server.com/directory/", uri
.OriginalString
, "baseUri+relative+OriginalString");
233 uri
= new Uri ("http://dummy.com");
234 Assert
.IsTrue (Uri
.TryCreate (baseUri
, absolute
, out uri
), "baseUri+absolute");
235 Assert
.AreEqual (absolute
, uri
.AbsoluteUri
, "baseUri+absolute+AbsoluteUri");
236 Assert
.AreEqual (absolute
, uri
.OriginalString
, "baseUri+absolute+OriginalString");
238 uri
= new Uri ("http://dummy.com");
239 Uri relativeUri
= new Uri (relative
, UriKind
.Relative
);
240 Assert
.IsFalse (Uri
.TryCreate (relativeUri
, relative
, out uri
), "relativeUri+relative");
241 Assert
.IsNull (uri
, "relativeUri+relative+Uri");
243 uri
= new Uri ("http://dummy.com");
244 Assert
.IsTrue (Uri
.TryCreate (relativeUri
, absolute
, out uri
), "relativeUri+absolute");
245 Assert
.AreEqual (absolute
, uri
.OriginalString
, "relativeUri+absolute+OriginalString");
247 uri
= new Uri ("http://dummy.com");
249 Assert
.IsFalse (Uri
.TryCreate (baseUri
, n
, out uri
), "baseUri+null");
250 Assert
.IsNull (uri
, "baseUri+null+Uri");
251 Assert
.IsFalse (Uri
.TryCreate (relativeUri
, n
, out uri
), "relativeUri+null");
252 Assert
.IsNull (uri
, "relativeUri+null+Uri");
253 Assert
.IsFalse (Uri
.TryCreate (null, relative
, out uri
), "null+relative");
254 Assert
.IsNull (uri
, "null+relative+Uri");
256 uri
= new Uri ("http://dummy.com");
257 Assert
.IsTrue (Uri
.TryCreate (null, absolute
, out uri
), "null+absolute");
258 Assert
.AreEqual (absolute
, uri
.OriginalString
, "null+absolute+OriginalString");
261 [Test
] // TryCreate (Uri, Uri, Uri)
262 public void TryCreate3 ()
264 Uri baseUri
= new Uri (absolute
);
265 Uri relativeUri
= new Uri (relative
, UriKind
.Relative
);
268 uri
= new Uri ("http://dummy.com");
269 Assert
.IsTrue (Uri
.TryCreate (baseUri
, relativeUri
, out uri
), "baseUri+relativeUri");
270 Assert
.AreEqual ("http://www.example.com/server.com/directory/", uri
.AbsoluteUri
, "baseUri+relativeUri+AbsoluteUri");
271 Assert
.AreEqual ("http://www.example.com/server.com/directory/", uri
.OriginalString
, "baseUri+relativeUri+OriginalString");
273 uri
= new Uri ("http://dummy.com");
274 Assert
.IsTrue (Uri
.TryCreate (baseUri
, baseUri
, out uri
), "baseUri+baseUri");
275 Assert
.AreEqual (absolute
, uri
.AbsoluteUri
, "baseUri+baseUri+AbsoluteUri");
276 Assert
.AreEqual (absolute
, uri
.OriginalString
, "baseUri+baseUri+OriginalString");
278 uri
= new Uri ("http://dummy.com");
279 Assert
.IsFalse (Uri
.TryCreate (relativeUri
, relativeUri
, out uri
), "relativeUri+relativeUri");
280 Assert
.IsNull (uri
, "relativeUri+relativeUri+Uri");
282 uri
= new Uri ("http://dummy.com");
283 Assert
.IsFalse (Uri
.TryCreate (relativeUri
, baseUri
, out uri
), "relativeUri+baseUri");
284 Assert
.IsNull (uri
, "relativeUri+baseUri+Uri");
286 uri
= new Uri ("http://dummy.com");
287 Assert
.IsFalse (Uri
.TryCreate (null, relativeUri
, out uri
), "null+relativeUri");
288 Assert
.IsNull (uri
, "null+relativeUri+Uri");
289 Assert
.IsFalse (Uri
.TryCreate (null, baseUri
, out uri
), "null+baseUri");
290 Assert
.IsNull (uri
, "null+baseUri+Uri");
293 [Test
] // TryCreate (Uri, Uri, out Uri)
294 public void TryCreate3_RelativeUri_Null ()
297 Uri baseUri
= new Uri (absolute
);
299 Uri
.TryCreate (baseUri
, (Uri
) null, out uri
);
302 catch (NullReferenceException
) {
303 Assert
.Fail ("does not throw NRE under FX 4.0");
308 public void IsWellFormedUriString ()
310 Assert
.IsFalse (Uri
.IsWellFormedUriString ("http://www.example.com/Main Page", UriKind
.Absolute
), "http/space");
311 Assert
.IsTrue (Uri
.IsWellFormedUriString ("http://www.example.com/Main%20Page", UriKind
.Absolute
), "http/%20");
312 Assert
.IsFalse (Uri
.IsWellFormedUriString (null, UriKind
.Absolute
), "null");
313 Assert
.IsFalse (Uri
.IsWellFormedUriString ("data", UriKind
.Absolute
), "data");
314 Assert
.IsTrue (Uri
.IsWellFormedUriString ("http://www.example.com/Main_Page#1", UriKind
.Absolute
), "http/hex");
315 Assert
.IsTrue (Uri
.IsWellFormedUriString ("test", UriKind
.RelativeOrAbsolute
), "rel1");
316 Assert
.IsTrue (Uri
.IsWellFormedUriString ("/test", UriKind
.RelativeOrAbsolute
), "rel2");
320 public void IsWellFormedUriString_UriKind_Invalid ()
323 Uri
.IsWellFormedUriString ("http://www.example.com/Main Page",
326 } catch (ArgumentException ex
) {
327 // The value '666' passed for the UriKind parameter
329 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
330 Assert
.IsNull (ex
.InnerException
, "#3");
331 Assert
.IsNotNull (ex
.Message
, "#4");
332 Assert
.IsNotNull (ex
.Message
.IndexOf ("'666'") != -1, "#5:" + ex
.Message
);
333 Assert
.IsNotNull (ex
.Message
.IndexOf ("UriKind") != -1, "#6:" + ex
.Message
);
334 Assert
.IsNull (ex
.ParamName
, "#7");
339 public void Compare ()
343 Assert
.AreEqual (0, Uri
.Compare (u1
, u2
, UriComponents
.AbsoluteUri
, UriFormat
.UriEscaped
, StringComparison
.CurrentCulture
), "null-null");
345 u1
= new Uri ("http://www.example.com");
346 Assert
.AreEqual (1, Uri
.Compare (u1
, u2
, UriComponents
.AbsoluteUri
, UriFormat
.UriEscaped
, StringComparison
.CurrentCulture
), "non-null - null");
349 u2
= new Uri ("http://www.example.com");
350 Assert
.AreEqual (-1, Uri
.Compare (u1
, u2
, UriComponents
.AbsoluteUri
, UriFormat
.UriEscaped
, StringComparison
.CurrentCulture
), "null - non-null");
352 u1
= new Uri ("http://www.example.com/Main Page");
353 u2
= new Uri ("http://www.example.com/Main%20Page");
354 Assert
.AreEqual (0, Uri
.Compare (u1
, u2
, UriComponents
.AbsoluteUri
, UriFormat
.Unescaped
, StringComparison
.CurrentCulture
), "http/space-http/%20-unescaped");
355 Assert
.AreEqual (0, Uri
.Compare (u1
, u2
, UriComponents
.AbsoluteUri
, UriFormat
.UriEscaped
, StringComparison
.CurrentCulture
), "http/space-http/%20-escaped");
356 Assert
.AreEqual (0, Uri
.Compare (u1
, u2
, UriComponents
.AbsoluteUri
, UriFormat
.SafeUnescaped
, StringComparison
.CurrentCulture
), "http/space-http/%20-safe");
360 public void IsBaseOf ()
362 Uri http
= new Uri ("http://www.example.com/Main_Page#FAQ?Edit");
363 Assert
.IsTrue (http
.IsBaseOf (http
), "http-http");
365 Uri u
= new Uri ("http://www.example.com/Main_Page#FAQ");
366 Assert
.IsTrue (u
.IsBaseOf (http
), "http-1a");
367 Assert
.IsTrue (http
.IsBaseOf (u
), "http-1b");
369 u
= new Uri ("http://www.example.com/Main_Page");
370 Assert
.IsTrue (u
.IsBaseOf (http
), "http-2a");
371 Assert
.IsTrue (http
.IsBaseOf (u
), "http-2b");
373 u
= new Uri ("http://www.example.com/");
374 Assert
.IsTrue (u
.IsBaseOf (http
), "http-3a");
375 Assert
.IsTrue (http
.IsBaseOf (u
), "http-3b");
377 u
= new Uri ("http://www.example.com/Main_Page/");
378 Assert
.IsFalse (u
.IsBaseOf (http
), "http-4a");
379 Assert
.IsTrue (http
.IsBaseOf (u
), "http-4b");
381 // docs says the UserInfo isn't evaluated, but...
382 u
= new Uri ("http://username:password@www.example.com/Main_Page");
383 Assert
.IsFalse (u
.IsBaseOf (http
), "http-5a");
384 Assert
.IsFalse (http
.IsBaseOf (u
), "http-5b");
386 // scheme case sensitive ? no
387 u
= new Uri ("HTTP://www.example.com/Main_Page");
388 Assert
.IsTrue (u
.IsBaseOf (http
), "http-6a");
389 Assert
.IsTrue (http
.IsBaseOf (u
), "http-6b");
391 // host case sensitive ? no
392 u
= new Uri ("http://www.example.com/Main_Page");
393 Assert
.IsTrue (u
.IsBaseOf (http
), "http-7a");
394 Assert
.IsTrue (http
.IsBaseOf (u
), "http-7b");
396 // path case sensitive ? no
397 u
= new Uri ("http://www.example.com/MAIN_Page");
398 Assert
.IsTrue (u
.IsBaseOf (http
), "http-8a");
399 Assert
.IsTrue (http
.IsBaseOf (u
), "http-8b");
402 u
= new Uri ("ftp://www.example.com/Main_Page");
403 Assert
.IsFalse (u
.IsBaseOf (http
), "http-9a");
404 Assert
.IsFalse (http
.IsBaseOf (u
), "http-9b");
407 u
= new Uri ("http://www.example.org/Main_Page");
408 Assert
.IsFalse (u
.IsBaseOf (http
), "http-10a");
409 Assert
.IsFalse (http
.IsBaseOf (u
), "http-10b");
412 u
= new Uri ("http://www.example.com:8080/");
413 Assert
.IsFalse (u
.IsBaseOf (http
), "http-11a");
414 Assert
.IsFalse (http
.IsBaseOf (u
), "http-11b");
416 // specify default port
417 u
= new Uri ("http://www.example.com:80/");
418 Assert
.IsTrue (u
.IsBaseOf (http
), "http-12a");
419 Assert
.IsTrue (http
.IsBaseOf (u
), "http-12b");
423 public void IsBaseOf_Null ()
425 Uri http
= new Uri ("http://www.example.com/Main_Page#FAQ?Edit");
427 http
.IsBaseOf (null);
430 catch (ArgumentNullException
) {
435 public void MakeRelativeUri ()
437 Uri uri1
= new Uri ("http://www.contoso.com/index.htm?x=2");
438 Uri uri2
= new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
439 Uri uri3
= new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
440 Uri uri4
= new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
441 Uri uri5
= new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
442 Uri uri6
= new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
443 Uri uri7
= new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
444 Uri uri8
= new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
445 Uri uri10
= new Uri ("mailto:xxx@xxx.com");
446 Uri uri11
= new Uri ("mailto:xxx@xxx.com?subject=hola");
447 Uri uri12
= new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
448 Uri uri13
= new Uri ("mailto:xxx@xxx.com/foo/bar");
449 Uri uri14
= new Uri ("http://www.contoso.com/test1/");
450 Uri uri15
= new Uri ("http://www.contoso.com/");
451 Uri uri16
= new Uri ("http://www.contoso.com/test");
453 AssertRelativeUri ("foo/bar/index.htm#fragment", uri1
, uri2
, "#A");
454 AssertRelativeUri ("../../index.htm?x=2", uri2
, uri1
, "#B");
455 AssertRelativeUri ("../../bar/foo/index.htm?y=1", uri2
, uri3
, "#C");
456 AssertRelativeUri ("../../foo/bar/index.htm#fragment", uri3
, uri2
, "#D");
457 AssertRelativeUri ("../foo2/index.htm?x=0", uri3
, uri4
, "#E");
458 AssertRelativeUri ("../foo/index.htm?y=1", uri4
, uri3
, "#F");
459 AssertRelativeUri ("?x=0", uri6
, uri6
, "#G");
460 AssertRelativeUri ("foobar.htm?z=0&y=5", uri6
, uri7
, "#H");
461 AssertRelativeUri ("?subject=hola", uri10
, uri11
, "#I");
462 AssertRelativeUri ("/foo/bar", uri10
, uri13
, "#J");
464 Uri relativeUri
= uri1
.MakeRelativeUri (uri8
);
465 Assert
.IsTrue (relativeUri
.IsAbsoluteUri
, "#K1");
466 Assert
.AreEqual (uri8
.ToString (), relativeUri
.ToString (), "#K2");
467 Assert
.AreEqual (uri8
.OriginalString
, relativeUri
.OriginalString
, "#K3");
469 relativeUri
= uri10
.MakeRelativeUri (uri12
);
470 Assert
.IsTrue (relativeUri
.IsAbsoluteUri
, "#L1");
471 Assert
.AreEqual (uri12
.ToString (), relativeUri
.ToString (), "#L2");
472 Assert
.AreEqual (uri12
.OriginalString
, relativeUri
.OriginalString
, "#L3");
474 relativeUri
= uri4
.MakeRelativeUri (uri6
);
475 Assert
.IsTrue (relativeUri
.IsAbsoluteUri
, "#M1");
476 Assert
.AreEqual (uri6
.ToString (), relativeUri
.ToString (), "#M2");
477 Assert
.AreEqual (uri6
.OriginalString
, relativeUri
.OriginalString
, "#M3");
479 relativeUri
= uri4
.MakeRelativeUri (uri5
);
480 Assert
.IsTrue (relativeUri
.IsAbsoluteUri
, "#N1");
481 Assert
.AreEqual (uri5
.ToString (), relativeUri
.ToString (), "#N2");
482 Assert
.AreEqual (uri5
.OriginalString
, relativeUri
.OriginalString
, "#N3");
484 AssertRelativeUri ("../", uri14
, uri15
, "#O");
485 AssertRelativeUri ("./", uri16
, uri15
, "#P");
487 Uri a1
= new Uri ("http://something/something2/test/");
488 Uri a2
= new Uri ("http://something/something2/");
489 Uri a3
= new Uri ("http://something/something2/test");
490 Uri a4
= new Uri ("http://something/something2");
492 AssertRelativeUri ("../", a1
, a2
, "Q1");
493 AssertRelativeUri ("../../something2", a1
, a4
, "Q2");
494 AssertRelativeUri ("./", a3
, a2
, "Q3");
495 AssertRelativeUri ("../something2", a3
, a4
, "Q4");
497 Uri b1
= new Uri ("http://something/test/");
498 Uri b2
= new Uri ("http://something/");
499 Uri b3
= new Uri ("http://something/test");
500 Uri b4
= new Uri ("http://something");
502 AssertRelativeUri ("../", b1
, b2
, "R1");
503 AssertRelativeUri ("../", b1
, b4
, "R2");
504 AssertRelativeUri ("./", b3
, b2
, "R3");
505 AssertRelativeUri ("./", b3
, b4
, "R4");
507 Uri c1
= new Uri ("C:\\something\\something2\\test\\");
508 Uri c2
= new Uri ("C:\\something\\something2\\");
509 Uri c3
= new Uri ("C:\\something\\something2\\test");
510 Uri c4
= new Uri ("C:\\something\\something2");
512 AssertRelativeUri ("../", c1
, c2
, "S1");
513 AssertRelativeUri ("../../something2", c1
, c4
, "S2");
514 AssertRelativeUri ("./", c3
, c2
, "S3");
515 AssertRelativeUri ("../something2", c3
, c4
, "S4");
517 Uri d1
= new Uri ("C:\\something\\test\\");
518 Uri d2
= new Uri ("C:\\something\\");
519 Uri d3
= new Uri ("C:\\something\\test");
520 Uri d4
= new Uri ("C:\\something");
522 AssertRelativeUri ("../", d1
, d2
, "T1");
523 AssertRelativeUri ("../../something", d1
, d4
, "T2");
524 AssertRelativeUri ("./", d3
, d2
, "T3");
525 AssertRelativeUri ("../something", d3
, d4
, "T4");
527 Uri e1
= new Uri ("C:\\something\\");
528 Uri e2
= new Uri ("C:\\");
529 Uri e3
= new Uri ("C:\\something");
531 AssertRelativeUri ("../", e1
, e2
, "U1");
532 AssertRelativeUri ("./", e3
, e2
, "U2");
533 AssertRelativeUri ("", e1
, e1
, "U3");
534 AssertRelativeUri ("", e3
, e3
, "U4");
535 AssertRelativeUri ("../something", e1
, e3
, "U5");
536 AssertRelativeUri ("something/", e3
, e1
, "U6");
537 AssertRelativeUri ("something", e2
, e3
, "U7");
541 public void MakeRelativeUri_Uri_Null ()
543 Uri uri
= new Uri ("http://test.com");
545 uri
.MakeRelativeUri ((Uri
) null);
548 catch (ArgumentNullException ex
) {
549 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
550 Assert
.IsNull (ex
.InnerException
, "#3");
551 Assert
.IsNotNull (ex
.Message
, "#4");
552 Assert
.IsNotNull (ex
.ParamName
, "#5");
553 Assert
.AreEqual ("uri", ex
.ParamName
, "#6");
557 [Test
] // LAMESPEC: see bug #321113
558 public void OriginalStringRelative ()
560 Uri k1
= new Uri ("http://www.example.com");
561 Uri k2
= new Uri (k1
, "docs");
562 Assert
.AreEqual ("http://www.example.com/docs", k2
.OriginalString
, "#1");
564 Uri a
= new Uri ("http://www.example.com:808/foo");
565 Uri b
= new Uri (a
, "../docs?queryyy#% %20%23%25bar");
566 Assert
.AreEqual ("http://www.example.com:808/docs?queryyy#% %20%23%25bar", b
.OriginalString
, "#2");
568 Uri c
= new Uri ("http://www.example.com:808/foo");
569 Uri d
= new Uri (a
, "../docs?queryyy#%20%23%25bar");
570 Assert
.AreEqual ("http://www.example.com:808/docs?queryyy#%20%23%25bar", d
.OriginalString
, "#3");
572 Uri e
= new Uri ("http://www.example.com:909");
573 Uri f
= new Uri (e
, "http://www.example.com:606/docs");
574 Assert
.AreEqual ("http://www.example.com:606/docs", f
.OriginalString
, "#4");
576 Uri g
= new Uri ("http://www.example.com:303/foo");
577 Uri h
= new Uri (g
, "?query");
578 // it doesn't work. MS.NET also returns incorrect URI: ..303/?query
579 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=412604
580 //Assert.AreEqual ("http://www.example.com:303/foo?query", h.OriginalString, "#5");
584 public void PathAndQuery_RelativeUri ()
586 Uri uri1
= new Uri ("http://www.contoso.com/index.htm?x=2");
587 Uri uri2
= new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
588 Uri relativeUri
= uri1
.MakeRelativeUri (uri2
);
591 string result
= relativeUri
.PathAndQuery
;
592 Assert
.Fail ("#1: " + result
);
593 } catch (InvalidOperationException ex
) {
594 // This operation is not supported for a relative URI
595 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
596 Assert
.IsNull (ex
.InnerException
, "#3");
597 Assert
.IsNotNull (ex
.Message
, "#4");
602 public void Query_RelativeUri ()
604 Uri uri1
= new Uri ("http://www.contoso.com/index.htm?x=2");
605 Uri uri2
= new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
606 Uri relativeUri
= uri1
.MakeRelativeUri (uri2
);
609 string result
= relativeUri
.Query
;
610 Assert
.Fail ("#1: " + result
);
611 } catch (InvalidOperationException ex
) {
612 // This operation is not supported for a relative URI
613 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
614 Assert
.IsNull (ex
.InnerException
, "#3");
615 Assert
.IsNotNull (ex
.Message
, "#4");
620 public void Scheme_RelativeUri ()
622 Uri uri1
= new Uri ("http://www.contoso.com/index.htm?x=2");
623 Uri uri2
= new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
624 Uri relativeUri
= uri1
.MakeRelativeUri (uri2
);
627 string result
= relativeUri
.Scheme
;
628 Assert
.Fail ("#1: " + result
);
629 } catch (InvalidOperationException ex
) {
630 // This operation is not supported for a relative URI
631 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
632 Assert
.IsNull (ex
.InnerException
, "#3");
633 Assert
.IsNotNull (ex
.Message
, "#4");
638 public void UnescapeDataString ()
640 Assert
.AreEqual ("/new folder/", Uri
.UnescapeDataString ("/new%20folder/"));
641 Assert
.AreEqual ("/new folder/", Uri
.UnescapeDataString ("/new%20%66older/"));
642 Assert
.AreEqual ("/new+folder/", Uri
.UnescapeDataString ("/new+folder/"));
645 void AssertRelativeUri (string expected
, Uri uri1
, Uri uri2
, string msg
)
648 relativeUri
= uri1
.MakeRelativeUri (uri2
);
650 Assert
.IsFalse (relativeUri
.IsAbsoluteUri
, msg
+ "1");
651 Assert
.AreEqual (expected
, relativeUri
.ToString (), msg
+ "2");
652 Assert
.AreEqual (expected
, relativeUri
.OriginalString
, msg
+ "3");
653 Assert
.IsFalse (relativeUri
.UserEscaped
, msg
+ "4");
657 public void DontCheckHostWithCustomParsers ()
659 UriParser
.Register (new TolerantUriParser (), "assembly", 0);
661 new Uri ("assembly://Spring.Core, Version=1.2.0.20001, Culture=neutral, "
662 + "PublicKeyToken=null/Spring.Objects.Factory.Xml/spring-objects-1.1.xsd");
663 } catch (UriFormatException
) {
664 Assert
.Fail ("Spring Uri is expected to work.");
669 public void ParseShortNameAsRelativeOrAbsolute ()
671 new Uri ("x", UriKind
.RelativeOrAbsolute
);
674 private class TolerantUriParser
: GenericUriParser
676 private const GenericUriParserOptions DefaultOptions
677 = GenericUriParserOptions
.Default
678 | GenericUriParserOptions
.GenericAuthority
679 | GenericUriParserOptions
.AllowEmptyAuthority
;
681 public TolerantUriParser()
682 : base(DefaultOptions
)
688 public void DomainLabelLength ()
690 UriHostNameType type
= Uri
.CheckHostName ("3.141592653589793238462643383279502884197169399375105820974944592.com");
691 Assert
.AreEqual (UriHostNameType
.Dns
, type
, "DomainLabelLength#1");
692 type
= Uri
.CheckHostName ("3141592653589793238462643383279502884197169399375105820974944592.com");
693 Assert
.AreEqual (UriHostNameType
.Unknown
, type
, "DomainLabelLength#2");
694 type
= Uri
.CheckHostName ("3.1415926535897932384626433832795028841971693993751058209749445923.com");
695 Assert
.AreEqual (UriHostNameType
.Unknown
, type
, "DomainLabelLength#2");
696 type
= Uri
.CheckHostName ("3.141592653589793238462643383279502884197169399375105820974944592._om");
697 Assert
.AreEqual (UriHostNameType
.Unknown
, type
, "DomainLabelLength#3");
700 // https://bugzilla.xamarin.com/show_bug.cgi?id=58400
702 public static void Test_LocalPath_Bug58400()
704 var uriAndExpected
= new []
706 ("file://host/directory/filename", @"\\host\directory\filename"),
707 ("file://host/directory/", @"\\host\directory\"),
708 ("file://host/filename", @"\\host\filename"),
709 ("file://host/", @"\\host\"),
710 ("file://host", @"\\host"),
711 ("file:///directory/filename", "/directory/filename"),
712 ("file:///directory/", "/directory/"),
713 ("file:///filename", "/filename"),
715 // This is an invalid URI, technically
717 ("file://////hello/world", @"\\hello\world"),
718 ("file://hello/////world", @"\\hello\\\\\world"),
721 foreach (var tup
in uriAndExpected
)
723 var originalUri
= tup
.Item1
;
724 var expectedLocalPath
= tup
.Item2
;
725 var uri
= new Uri (originalUri
);
726 var localPath
= uri
.LocalPath
;
728 Assert
.AreEqual (expectedLocalPath
, localPath
, originalUri
);