2 // UriTest.cs - NUnit Test Cases for System.Uri
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Ben Maurer (bmaurer@users.sourceforge.net)
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Ben Maurer
13 using System
.Reflection
;
14 using NUnit
.Framework
;
19 namespace MonoTests
.System
27 public void GetReady ()
29 isWin32
= (Path
.DirectorySeparatorChar
== '\\');
33 public void Constructors ()
38 uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
41 uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
44 uri = new Uri("http://11.22.33.588:9090");
47 uri = new Uri("http://[11:22:33::88]:9090");
50 uri = new Uri("http://[::127.11.22.33]:8080");
53 uri = new Uri("http://[abcde::127.11.22.33]:8080");
58 uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
61 uri = new Uri ("http://www.contoso.com:1234/foo/bar");
64 uri = new Uri ("http://www.contoso.com:1234/");
67 uri = new Uri ("http://www.contoso.com:1234");
71 uri
= new Uri(" \r \n http://test.com\r\n \r\r ");
72 Assert
.AreEqual ("http://test.com/", uri
.ToString(), "#k0");
73 Assert
.AreEqual ("http", uri
.GetComponents (UriComponents
.Scheme
, UriFormat
.UriEscaped
), "#k0-gc");
75 uri
= new Uri ("http://contoso.com?subject=uri");
76 Assert
.AreEqual ("/", uri
.AbsolutePath
, "#k1");
77 Assert
.AreEqual ("http://contoso.com/?subject=uri", uri
.AbsoluteUri
, "#k2");
78 Assert
.AreEqual ("contoso.com", uri
.Authority
, "#k3");
79 Assert
.AreEqual ("", uri
.Fragment
, "#k4");
80 Assert
.AreEqual ("contoso.com", uri
.Host
, "#k5");
81 Assert
.AreEqual (UriHostNameType
.Dns
, uri
.HostNameType
, "#k6");
82 Assert
.AreEqual (true, uri
.IsDefaultPort
, "#k7");
83 Assert
.AreEqual (false, uri
.IsFile
, "#k8");
84 Assert
.AreEqual (false, uri
.IsLoopback
, "#k9");
85 Assert
.AreEqual (false, uri
.IsUnc
, "#k10");
86 Assert
.AreEqual ("/", uri
.LocalPath
, "#k11");
87 Assert
.AreEqual ("/?subject=uri", uri
.PathAndQuery
, "#k12");
88 Assert
.AreEqual (80, uri
.Port
, "#k13");
89 Assert
.AreEqual ("?subject=uri", uri
.Query
, "#k14");
90 Assert
.AreEqual ("http", uri
.Scheme
, "#k15");
91 Assert
.AreEqual (false, uri
.UserEscaped
, "#k16");
92 Assert
.AreEqual ("", uri
.UserInfo
, "#k17");
94 uri
= new Uri ("mailto:user:pwd@contoso.com?subject=uri");
95 Assert
.AreEqual ("", uri
.AbsolutePath
, "#m1");
96 Assert
.AreEqual ("mailto:user:pwd@contoso.com?subject=uri", uri
.AbsoluteUri
, "#m2");
97 Assert
.AreEqual ("contoso.com", uri
.Authority
, "#m3");
98 Assert
.AreEqual ("", uri
.Fragment
, "#m4");
99 Assert
.AreEqual ("contoso.com", uri
.Host
, "#m5");
100 Assert
.AreEqual (UriHostNameType
.Dns
, uri
.HostNameType
, "#m6");
101 Assert
.AreEqual (true, uri
.IsDefaultPort
, "#m7");
102 Assert
.AreEqual (false, uri
.IsFile
, "#m8");
103 Assert
.AreEqual (false, uri
.IsLoopback
, "#m9");
104 Assert
.AreEqual (false, uri
.IsUnc
, "#m10");
105 Assert
.AreEqual ("", uri
.LocalPath
, "#m11");
106 Assert
.AreEqual ("?subject=uri", uri
.PathAndQuery
, "#m12");
107 Assert
.AreEqual (25, uri
.Port
, "#m13");
108 Assert
.AreEqual ("?subject=uri", uri
.Query
, "#m14");
109 Assert
.AreEqual ("mailto", uri
.Scheme
, "#m15");
110 Assert
.AreEqual (false, uri
.UserEscaped
, "#m16");
111 Assert
.AreEqual ("user:pwd", uri
.UserInfo
, "#m17");
113 uri
= new Uri("myscheme://127.0.0.1:5");
114 Assert
.AreEqual ("myscheme://127.0.0.1:5/", uri
.ToString(), "#c1");
116 uri
= new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
117 Assert
.AreEqual ("/mydir/mysubdir/myfile.ext", uri
.AbsolutePath
, "#n1");
118 Assert
.AreEqual ("file://myserver/mydir/mysubdir/myfile.ext", uri
.AbsoluteUri
, "#n2");
119 Assert
.AreEqual ("myserver", uri
.Authority
, "#n3");
120 Assert
.AreEqual ("", uri
.Fragment
, "#n4");
121 Assert
.AreEqual ("myserver", uri
.Host
, "#n5");
122 Assert
.AreEqual (UriHostNameType
.Dns
, uri
.HostNameType
, "#n6");
123 Assert
.AreEqual (true, uri
.IsDefaultPort
, "#n7");
124 Assert
.AreEqual (true, uri
.IsFile
, "#n8");
125 Assert
.AreEqual (false, uri
.IsLoopback
, "#n9");
126 Assert
.AreEqual (true, uri
.IsUnc
, "#n10");
129 Assert
.AreEqual (@"\\myserver\mydir\mysubdir\myfile.ext", uri
.LocalPath
, "#n11");
131 // myserver never could be the part of Unix path.
132 Assert
.AreEqual ("/mydir/mysubdir/myfile.ext", uri
.LocalPath
, "#n11");
134 Assert
.AreEqual ("/mydir/mysubdir/myfile.ext", uri
.PathAndQuery
, "#n12");
135 Assert
.AreEqual (-1, uri
.Port
, "#n13");
136 Assert
.AreEqual ("", uri
.Query
, "#n14");
137 Assert
.AreEqual ("file", uri
.Scheme
, "#n15");
138 Assert
.AreEqual (false, uri
.UserEscaped
, "#n16");
139 Assert
.AreEqual ("", uri
.UserInfo
, "#n17");
141 uri
= new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
142 Assert
.AreEqual ("http://www.contoso.com/Hello World.htm", uri
.AbsoluteUri
, "#rel1a");
143 Assert
.AreEqual (true, uri
.UserEscaped
, "#rel1b");
144 uri
= new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
145 Assert
.AreEqual ("http://www.contoso.com/Hello%20World.htm", uri
.AbsoluteUri
, "#rel2a");
146 Assert
.AreEqual (false, uri
.UserEscaped
, "#rel2b");
147 uri
= new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
148 Assert
.AreEqual ("http://www.xxx.com/Hello%20World.htm", uri
.AbsoluteUri
, "#rel3");
150 uri
= new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
151 Assert
.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri
.AbsoluteUri
, "#rel5");
152 uri
= new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
153 Assert
.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri
.AbsoluteUri
, "#rel6");
154 uri
= new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
155 Assert
.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri
.AbsoluteUri
, "#rel7");
156 uri
= new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
157 Assert
.AreEqual ("http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri
.AbsoluteUri
, "#rel8");
158 uri
= new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
159 Assert
.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri
.AbsoluteUri
, "#rel9");
160 Assert
.AreEqual ("/foo/bar/Hello%20World.htm", uri
.AbsolutePath
, "#rel9-path");
162 uri
= new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
163 Assert
.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri
.AbsoluteUri
, "#rel10");
165 uri
= new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String
.Empty
, false);
166 Assert
.AreEqual ("http://www.contoso.com/foo/bar/index.html?x=0", uri
.ToString (), "#22");
168 uri
= new Uri (new Uri("http://www.xxx.com"), "?x=0");
169 Assert
.AreEqual ("http://www.xxx.com/?x=0", uri
.ToString(), "#rel30");
170 uri
= new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
171 Assert
.AreEqual ("http://www.xxx.com/index.htm#here", uri
.ToString(), "#rel32");
173 uri
= new Uri ("relative", UriKind
.Relative
);
174 uri
= new Uri ("relative/abc", UriKind
.Relative
);
175 uri
= new Uri ("relative", UriKind
.RelativeOrAbsolute
);
177 Assert
.IsTrue (!uri
.IsAbsoluteUri
, "#rel33");
178 Assert
.AreEqual (uri
.OriginalString
, "relative", "#rel34");
179 Assert
.IsTrue (!uri
.UserEscaped
, "#rel35");
183 public void Constructor_DualHostPort ()
185 string relative
= "foo:8080/bar/Hello World.htm";
186 Uri uri
= new Uri (new Uri("http://www.contoso.com"), relative
, false);
187 Assert
.AreEqual ("8080/bar/Hello%20World.htm", uri
.AbsolutePath
, "AbsolutePath");
188 Assert
.AreEqual ("foo:8080/bar/Hello%20World.htm", uri
.AbsoluteUri
, "AbsoluteUri");
189 Assert
.AreEqual (String
.Empty
, uri
.Authority
, "Authority");
190 Assert
.AreEqual (String
.Empty
, uri
.Fragment
, "Fragment");
191 Assert
.AreEqual (String
.Empty
, uri
.Host
, "Host");
192 Assert
.AreEqual ("8080/bar/Hello%20World.htm", uri
.PathAndQuery
, "PathAndQuery");
193 Assert
.AreEqual (-1, uri
.Port
, "Port");
194 Assert
.AreEqual (String
.Empty
, uri
.Query
, "Query");
195 Assert
.AreEqual ("foo", uri
.Scheme
, "Scheme");
196 Assert
.AreEqual (String
.Empty
, uri
.UserInfo
, "Query");
198 Assert
.AreEqual ("8080/", uri
.Segments
[0], "Segments[0]");
199 Assert
.AreEqual ("bar/", uri
.Segments
[1], "Segments[1]");
200 Assert
.AreEqual ("Hello%20World.htm", uri
.Segments
[2], "Segments[2]");
202 Assert
.IsTrue (uri
.IsDefaultPort
, "IsDefaultPort");
203 Assert
.IsTrue (!uri
.IsFile
, "IsFile");
204 Assert
.IsTrue (!uri
.IsLoopback
, "IsLoopback");
205 Assert
.IsTrue (!uri
.IsUnc
, "IsUnc");
206 Assert
.IsTrue (!uri
.UserEscaped
, "UserEscaped");
208 Assert
.AreEqual (UriHostNameType
.Unknown
, uri
.HostNameType
, "HostNameType");
209 Assert
.IsTrue (uri
.IsAbsoluteUri
, "IsAbsoluteUri");
210 Assert
.AreEqual (relative
, uri
.OriginalString
, "OriginalString");
214 [ExpectedException (typeof (ArgumentNullException
))]
215 public void Constructor_NullStringBool ()
217 new Uri (null, "http://www.contoso.com/index.htm", false);
221 public void Constructor_UriNullBool ()
223 new Uri (new Uri ("http://www.contoso.com"), null, false);
226 // regression for bug #47573
228 public void RelativeCtor ()
230 Uri b
= new Uri ("http://a/b/c/d;p?q");
231 Assert
.AreEqual ("http://a/g", new Uri (b
, "/g").ToString (), "#1");
232 Assert
.AreEqual ("http://g/", new Uri (b
, "//g").ToString (), "#2");
233 Assert
.IsTrue (new Uri (b
, "#s").ToString ().EndsWith ("#s"), "#4");
235 Uri u
= new Uri (b
, "/g?q=r");
236 Assert
.AreEqual ("http://a/g?q=r", u
.ToString (), "#5");
237 Assert
.AreEqual ("?q=r", u
.Query
, "#6");
239 u
= new Uri (b
, "/g?q=r;. a");
240 Assert
.AreEqual ("http://a/g?q=r;. a", u
.ToString (), "#5");
241 Assert
.AreEqual ("?q=r;.%20a", u
.Query
, "#6");
246 [ExpectedException (typeof (UriFormatException
))]
247 public void RelativeCtor_11_Crasher ()
249 Uri b
= new Uri ("http://a/b/c/d;p?q");
250 // this causes crash under MS.NET 1.1
251 Assert
.AreEqual ("g:h", new Uri (b
, "g:h").ToString (), "g:h");
255 [ExpectedException (typeof (UriFormatException
))]
256 public void Bad_IPv6 ()
258 new Uri ("http://0:0:0:0::127.1.2.3]/");
262 public void LeadingSlashes_ShouldFailOn1x ()
264 // doesn't (but should) fail under 1.x
265 Assert
.AreEqual ("file:///", new Uri ("file:///").ToString (), "#1");
266 Assert
.AreEqual ("file:///", new Uri ("file://").ToString (), "#2");
270 public void LeadingSlashes_BadResultsOn1x ()
272 // strange behaviours of 1.x - it's probably not worth to fix it
273 // on Mono as 2.0 has been fixed
274 Uri u
= new Uri ("file:///foo/bar");
275 Assert
.AreEqual (String
.Empty
, u
.Host
, "#3a");
276 Assert
.AreEqual (UriHostNameType
.Basic
, u
.HostNameType
, "#3b");
277 Assert
.AreEqual ("file:///foo/bar", u
.ToString (), "#3c");
278 Assert
.AreEqual (false, u
.IsUnc
, "#3d");
280 u
= new Uri ("mailto:/foo");
281 Assert
.AreEqual (String
.Empty
, u
.Host
, "#13a");
282 Assert
.AreEqual (UriHostNameType
.Basic
, u
.HostNameType
, "#13b");
283 Assert
.AreEqual ("mailto:/foo", u
.ToString (), "#13c");
285 u
= new Uri ("mailto://foo");
286 Assert
.AreEqual (String
.Empty
, u
.Host
, "#14a");
287 Assert
.AreEqual (UriHostNameType
.Basic
, u
.HostNameType
, "#14b");
288 Assert
.AreEqual ("mailto://foo", u
.ToString (), "#14c");
290 u
= new Uri ("news:/");
291 Assert
.AreEqual (String
.Empty
, u
.Host
, "#18a");
292 Assert
.AreEqual (UriHostNameType
.Unknown
, u
.HostNameType
, "#18b");
293 Assert
.AreEqual ("news:/", u
.ToString (), "#18c");
294 Assert
.AreEqual ("/", u
.AbsolutePath
, "#18d");
295 Assert
.AreEqual ("news:/", u
.AbsoluteUri
, "#18e");
297 u
= new Uri ("news:/foo");
298 Assert
.AreEqual (String
.Empty
, u
.Host
, "#19a");
299 Assert
.AreEqual (UriHostNameType
.Unknown
, u
.HostNameType
, "#19b");
300 Assert
.AreEqual ("news:/foo", u
.ToString (), "#19c");
301 Assert
.AreEqual ("/foo", u
.AbsolutePath
, "#19d");
302 Assert
.AreEqual ("news:/foo", u
.AbsoluteUri
, "#19e");
304 u
= new Uri ("news://foo");
305 Assert
.AreEqual (String
.Empty
, u
.Host
, "#20a");
306 Assert
.AreEqual (UriHostNameType
.Unknown
, u
.HostNameType
, "#20b");
307 Assert
.AreEqual ("news://foo", u
.ToString (), "#20c");
308 Assert
.AreEqual ("//foo", u
.AbsolutePath
, "#20d");
309 Assert
.AreEqual ("news://foo", u
.AbsoluteUri
, "#20e");
311 u
= new Uri ("news://foo/bar");
312 Assert
.AreEqual (String
.Empty
, u
.Host
, "#22a");
313 Assert
.AreEqual (UriHostNameType
.Unknown
, u
.HostNameType
, "#22b");
314 Assert
.AreEqual ("news://foo/bar", u
.ToString (), "#22c");
315 Assert
.AreEqual ("//foo/bar", u
.AbsolutePath
, "#22d");
316 Assert
.AreEqual ("news://foo/bar", u
.AbsoluteUri
, "#22e");
320 public void LeadingSlashes_FailOn1x ()
322 // 1.x throws an UriFormatException because it can't decode the host name
323 Uri u
= new Uri ("mailto:");
324 Assert
.AreEqual (String
.Empty
, u
.Host
, "#10a");
325 Assert
.AreEqual (UriHostNameType
.Basic
, u
.HostNameType
, "#10b");
326 Assert
.AreEqual ("mailto:", u
.ToString (), "#10c");
328 // 1.x throws an UriFormatException because it can't decode the host name
329 u
= new Uri ("mailto:/");
330 Assert
.AreEqual (String
.Empty
, u
.Host
, "#12a");
331 Assert
.AreEqual (UriHostNameType
.Basic
, u
.HostNameType
, "#12b");
332 Assert
.AreEqual ("mailto:/", u
.ToString (), "#12c");
334 // 1.x throws an UriFormatException because it cannot detect the format
335 u
= new Uri ("mailto:///foo");
336 Assert
.AreEqual (String
.Empty
, u
.Host
, "#15a");
337 Assert
.AreEqual (UriHostNameType
.Basic
, u
.HostNameType
, "#15b");
338 Assert
.AreEqual ("mailto:///foo", u
.ToString (), "#15c");
340 // 1.x throws an UriFormatException because it cannot detect the format
341 u
= new Uri ("news:///foo");
342 Assert
.AreEqual (String
.Empty
, u
.Host
, "#21a");
343 Assert
.AreEqual (UriHostNameType
.Unknown
, u
.HostNameType
, "#21b");
344 Assert
.AreEqual ("news:///foo", u
.ToString (), "#21c");
345 Assert
.AreEqual ("///foo", u
.AbsolutePath
, "#21d");
346 Assert
.AreEqual ("news:///foo", u
.AbsoluteUri
, "#21e");
350 // some tests from bug 75144
351 public void LeadingSlashes ()
353 Uri u
= new Uri ("file://foo/bar");
354 Assert
.AreEqual ("foo", u
.Host
, "#5a");
355 Assert
.AreEqual (UriHostNameType
.Dns
, u
.HostNameType
, "#5b");
356 Assert
.AreEqual ("file://foo/bar", u
.ToString (), "#5c");
357 Assert
.AreEqual (isWin32
, u
.IsUnc
, "#5d");
359 u
= new Uri ("file:////foo/bar");
360 Assert
.AreEqual ("foo", u
.Host
, "#7a");
361 Assert
.AreEqual (UriHostNameType
.Dns
, u
.HostNameType
, "#7b");
362 Assert
.AreEqual ("file://foo/bar", u
.ToString (), "#7c");
363 Assert
.AreEqual (isWin32
, u
.IsUnc
, "#7d");
365 Assert
.AreEqual ("file://foo/bar", new Uri ("file://///foo/bar").ToString(), "#9");
367 u
= new Uri ("mailto:foo");
368 Assert
.AreEqual ("foo", u
.Host
, "#11a");
369 Assert
.AreEqual (UriHostNameType
.Dns
, u
.HostNameType
, "#11b");
370 Assert
.AreEqual ("mailto:foo", u
.ToString (), "#11c");
372 u
= new Uri ("news:");
373 Assert
.AreEqual (String
.Empty
, u
.Host
, "#16a");
374 Assert
.AreEqual (UriHostNameType
.Unknown
, u
.HostNameType
, "#16b");
375 Assert
.AreEqual ("news:", u
.ToString (), "#16c");
377 u
= new Uri ("news:foo");
378 Assert
.AreEqual (String
.Empty
, u
.Host
, "#17a");
379 Assert
.AreEqual (UriHostNameType
.Unknown
, u
.HostNameType
, "#17b");
380 Assert
.AreEqual ("news:foo", u
.ToString (), "#17c");
381 Assert
.AreEqual ("foo", u
.AbsolutePath
, "#17d");
382 Assert
.AreEqual ("news:foo", u
.AbsoluteUri
, "#17e");
386 [ExpectedException (typeof (UriFormatException
))]
387 public void HttpHostname1 ()
393 [ExpectedException (typeof (UriFormatException
))]
394 public void HttpHostname2 ()
400 [ExpectedException (typeof (UriFormatException
))]
401 public void HttpHostname3 ()
407 [ExpectedException (typeof (UriFormatException
))]
408 public void HttpHostname4 ()
410 new Uri ("http:/foo");
414 [ExpectedException (typeof (UriFormatException
))]
415 public void HttpHostname5 ()
421 [ExpectedException (typeof (UriFormatException
))]
422 public void HttpHostname6 ()
424 new Uri ("http:///");
428 [ExpectedException (typeof (UriFormatException
))]
429 public void HttpHostname7 ()
431 new Uri ("http:///foo");
435 [ExpectedException (typeof (UriFormatException
))]
436 public void InvalidFile1 ()
442 [ExpectedException (typeof (UriFormatException
))]
443 public void InvalidFile2 ()
449 [ExpectedException (typeof (UriFormatException
))]
450 public void InvalidFile3 ()
452 new Uri ("file:/foo");
456 [ExpectedException (typeof (UriFormatException
))]
457 public void InvalidScheme ()
463 public void ConstructorsRejectRelativePath ()
465 string [] reluris
= new string [] {
467 "thisdir/childdir/file",
470 string [] winRelUris
= new string [] {
474 for (int i
= 0; i
< reluris
.Length
; i
++) {
476 new Uri (reluris
[i
]);
477 Assert
.Fail ("Should be failed: " + reluris
[i
]);
478 } catch (UriFormatException
) {
483 for (int i
= 0; i
< winRelUris
.Length
; i
++) {
485 new Uri (winRelUris
[i
]);
486 Assert
.Fail ("Should be failed: " + winRelUris
[i
]);
487 } catch (UriFormatException
) {
494 public void LocalPath ()
496 Uri uri
= new Uri ("c:\\tmp\\hello.txt");
497 Assert
.AreEqual ("file:///c:/tmp/hello.txt", uri
.ToString (), "#1a");
498 Assert
.AreEqual ("c:\\tmp\\hello.txt", uri
.LocalPath
, "#1b");
499 Assert
.AreEqual ("file", uri
.Scheme
, "#1c");
500 Assert
.AreEqual ("", uri
.Host
, "#1d");
501 Assert
.AreEqual ("c:/tmp/hello.txt", uri
.AbsolutePath
, "#1e");
503 uri
= new Uri ("file:////////cygwin/tmp/hello.txt");
504 Assert
.AreEqual ("file://cygwin/tmp/hello.txt", uri
.ToString (), "#3a");
505 Assert
.AreEqual ("\\\\cygwin\\tmp\\hello.txt", uri
.LocalPath
, "#3b win32");
506 Assert
.AreEqual ("file", uri
.Scheme
, "#3c");
507 Assert
.AreEqual ("cygwin", uri
.Host
, "#3d");
508 Assert
.AreEqual ("/tmp/hello.txt", uri
.AbsolutePath
, "#3e");
510 uri
= new Uri ("file://mymachine/cygwin/tmp/hello.txt");
511 Assert
.AreEqual ("file://mymachine/cygwin/tmp/hello.txt", uri
.ToString (), "#4a");
512 Assert
.AreEqual ("\\\\mymachine\\cygwin\\tmp\\hello.txt", uri
.LocalPath
, "#4b win32");
513 Assert
.AreEqual ("file", uri
.Scheme
, "#4c");
514 Assert
.AreEqual ("mymachine", uri
.Host
, "#4d");
515 Assert
.AreEqual ("/cygwin/tmp/hello.txt", uri
.AbsolutePath
, "#4e");
517 uri
= new Uri ("file://///c:/cygwin/tmp/hello.txt");
518 Assert
.AreEqual ("file:///c:/cygwin/tmp/hello.txt", uri
.ToString (), "#5a");
519 Assert
.AreEqual ("c:\\cygwin\\tmp\\hello.txt", uri
.LocalPath
, "#5b");
520 Assert
.AreEqual ("file", uri
.Scheme
, "#5c");
521 Assert
.AreEqual ("", uri
.Host
, "#5d");
522 Assert
.AreEqual ("c:/cygwin/tmp/hello.txt", uri
.AbsolutePath
, "#5e");
526 public void LocalPath_FileHost ()
528 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
529 Uri uri
= new Uri("file://one_file.txt");
530 Assert
.AreEqual ("file://one_file.txt/", uri
.ToString(), "#6a");
531 Assert
.AreEqual ("/", uri
.AbsolutePath
, "#6e");
532 Assert
.AreEqual ("/", uri
.PathAndQuery
, "#6f");
533 Assert
.AreEqual ("file://one_file.txt/", uri
.GetLeftPart (UriPartial
.Path
), "#6g");
534 Assert
.AreEqual ("\\\\one_file.txt", uri
.LocalPath
, "#6b");
535 Assert
.AreEqual ("file", uri
.Scheme
, "#6c");
536 Assert
.AreEqual ("one_file.txt", uri
.Host
, "#6d");
538 // same tests - but original Uri is now ending with a '/'
540 uri
= new Uri ("file://one_file.txt/");
541 Assert
.AreEqual ("file://one_file.txt/", uri
.ToString (), "#7a");
542 Assert
.AreEqual ("/", uri
.AbsolutePath
, "#7e");
543 Assert
.AreEqual ("/", uri
.PathAndQuery
, "#7f");
544 Assert
.AreEqual ("file://one_file.txt/", uri
.GetLeftPart (UriPartial
.Path
), "#7g");
545 Assert
.AreEqual ("\\\\one_file.txt\\", uri
.LocalPath
, "#7b");
546 Assert
.AreEqual ("file", uri
.Scheme
, "#7c");
547 Assert
.AreEqual ("one_file.txt", uri
.Host
, "#7d");
551 public void LocalPath_Escape ()
554 Uri uri
= new Uri ("file:///tmp/a%20a");
556 Assert
.IsTrue (uri
.LocalPath
.EndsWith ("/tmp/a a"), "#7a:" + uri
.LocalPath
);
558 Assert
.AreEqual ("/tmp/a a", uri
.LocalPath
, "#7b");
560 uri
= new Uri ("file:///tmp/foo%25bar");
562 Assert
.IsTrue (uri
.LocalPath
.EndsWith ("/tmp/foo%bar"), "#8a:" + uri
.LocalPath
);
563 Assert
.IsTrue (uri
.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri
.ToString ());
565 Assert
.AreEqual ("/tmp/foo%bar", uri
.LocalPath
, "#8b");
566 Assert
.AreEqual ("file:///tmp/foo%25bar", uri
.ToString (), "#8d");
569 uri
= new Uri ("file:///foo%25bar");
571 Assert
.IsTrue (uri
.LocalPath
.EndsWith ("/foo%bar"), "#9a:" + uri
.LocalPath
);
572 // ditto, file://tmp/foo%25bar (bug in 1.x)
573 Assert
.IsTrue (uri
.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri
.ToString ());
575 Assert
.AreEqual ("/foo%bar", uri
.LocalPath
, "#9b");
576 Assert
.AreEqual ("file:///foo%25bar", uri
.ToString (), "#9d");
580 // Novell Bugzilla #320614
582 public void QueryEscape ()
584 Uri u1
= new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
585 Uri u2
= new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
587 Assert
.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u1
.ToString (), "QE1");
589 Assert
.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2
.ToString (), "QE2");
593 public void UnixPath () {
595 Assert
.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
601 Uri uri
= new Uri ("http://www.contoso.com");
602 Assert
.IsTrue (!uri
.IsUnc
, "#1");
604 uri
= new Uri ("news:123456@contoso.com");
605 Assert
.IsTrue (!uri
.IsUnc
, "#2");
607 uri
= new Uri ("file://server/filename.ext");
608 Assert
.AreEqual (isWin32
, uri
.IsUnc
, "#3");
610 uri
= new Uri (@"\\server\share\filename.ext");
611 Assert
.IsTrue (uri
.IsUnc
, "#6");
613 uri
= new Uri (@"a:\dir\filename.ext");
614 Assert
.IsTrue (!uri
.IsUnc
, "#8");
618 [Category("NotDotNet")]
619 public void UncFail ()
622 Uri uri
= new Uri ("/home/user/dir/filename.ext");
623 Assert
.IsTrue (!uri
.IsUnc
, "#7");
628 public void FromHex ()
630 Assert
.AreEqual (0, Uri
.FromHex ('0'), "#1");
631 Assert
.AreEqual (9, Uri
.FromHex ('9'), "#2");
632 Assert
.AreEqual (10, Uri
.FromHex ('a'), "#3");
633 Assert
.AreEqual (15, Uri
.FromHex ('f'), "#4");
634 Assert
.AreEqual (10, Uri
.FromHex ('A'), "#5");
635 Assert
.AreEqual (15, Uri
.FromHex ('F'), "#6");
639 } catch (ArgumentException
) {}
643 } catch (ArgumentException
) {}
647 } catch (ArgumentException
) {}
652 public UriEx (string s
) : base (s
)
656 public string UnescapeString (string s
)
661 public static string UnescapeString (string uri
, string target
)
663 return new UriEx (uri
).UnescapeString (target
);
668 public void Unescape ()
670 Assert
.AreEqual ("#", UriEx
.UnescapeString ("file://localhost/c#", "%23"), "#1");
671 Assert
.AreEqual ("c#", UriEx
.UnescapeString ("file://localhost/c#", "c%23"), "#2");
672 Assert
.AreEqual ("#", UriEx
.UnescapeString ("http://localhost/c#", "%23"), "#1");
673 Assert
.AreEqual ("c#", UriEx
.UnescapeString ("http://localhost/c#", "c%23"), "#2");
674 Assert
.AreEqual ("%A9", UriEx
.UnescapeString ("file://localhost/c#", "%A9"), "#3");
675 Assert
.AreEqual ("%A9", UriEx
.UnescapeString ("http://localhost/c#", "%A9"), "#3");
679 public void HexEscape ()
681 Assert
.AreEqual ("%20", Uri
.HexEscape (' '), "#1");
682 Assert
.AreEqual ("%A9", Uri
.HexEscape ((char) 0xa9), "#2");
683 Assert
.AreEqual ("%41", Uri
.HexEscape ('A'), "#3");
685 Uri
.HexEscape ((char) 0x0369);
687 } catch (ArgumentOutOfRangeException
) {}
691 public void MoreHexEscape()
693 string url
= "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
694 string escapedAbsolutePath
= "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
695 Uri u
= new Uri(url
);
696 Assert
.AreEqual (escapedAbsolutePath
, u
.AbsolutePath
, "Escaped non-english combo");
700 public void HexUnescape ()
703 Assert
.AreEqual (' ', Uri
.HexUnescape ("%20", ref i
), "#1");
704 Assert
.AreEqual (3, i
, "#2");
706 Assert
.AreEqual ((char) 0xa9, Uri
.HexUnescape ("test%a9test", ref i
), "#3");
707 Assert
.AreEqual (7, i
, "#4");
708 Assert
.AreEqual ('t', Uri
.HexUnescape ("test%a9test", ref i
), "#5");
709 Assert
.AreEqual (8, i
, "#6");
711 Assert
.AreEqual ('%', Uri
.HexUnescape ("test%a", ref i
), "#5");
712 Assert
.AreEqual (5, i
, "#6");
713 Assert
.AreEqual ('%', Uri
.HexUnescape ("testx%xx", ref i
), "#7");
714 Assert
.AreEqual (6, i
, "#8");
716 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
718 Assert
.AreEqual (227, (int) Uri
.HexUnescape ("a%E3%81%8B", ref i
), "#9");
719 Assert
.AreEqual (4, i
, "#10");
721 Assert
.AreEqual (240, (int) Uri
.HexUnescape ("a%F0%90%84%80", ref i
), "#11");
722 Assert
.AreEqual (4, i
, "#12");
726 public void HexUnescapeMultiByte ()
728 // Tests from bug 74872
729 // Note: These won't pass exactly with MS.NET, due to differences in the
730 // handling of backslashes/forwardslashes
735 uri
= new Uri ("file:///foo/a%E3%81%8Bb", true);
736 path
= uri
.LocalPath
;
737 Assert
.AreEqual (8, path
.Length
, "#1");
738 Assert
.AreEqual (0x304B, path
[6], "#2");
740 // 4-byte character which should be handled as a surrogate
741 uri
= new Uri ("file:///foo/a%F3%A0%84%80b", true);
742 path
= uri
.LocalPath
;
743 Assert
.AreEqual (9, path
.Length
, "#3");
744 Assert
.AreEqual (0xDB40, path
[6], "#4");
745 Assert
.AreEqual (0xDD00, path
[7], "#5");
746 Assert
.AreEqual (0x62, path
[8], "#6");
748 // 2-byte escape sequence, 2 individual characters
749 uri
= new Uri ("file:///foo/a%C2%F8b", true);
750 path
= uri
.LocalPath
;
751 Assert
.AreEqual ("/foo/a%C2%F8b", path
, "#7");
755 public void IsHexDigit ()
757 Assert
.IsTrue (Uri
.IsHexDigit ('a'), "#1");
758 Assert
.IsTrue (Uri
.IsHexDigit ('f'), "#2");
759 Assert
.IsTrue (!Uri
.IsHexDigit ('g'), "#3");
760 Assert
.IsTrue (Uri
.IsHexDigit ('0'), "#4");
761 Assert
.IsTrue (Uri
.IsHexDigit ('9'), "#5");
762 Assert
.IsTrue (Uri
.IsHexDigit ('A'), "#6");
763 Assert
.IsTrue (Uri
.IsHexDigit ('F'), "#7");
764 Assert
.IsTrue (!Uri
.IsHexDigit ('G'), "#8");
768 public void IsHexEncoding ()
770 Assert
.IsTrue (Uri
.IsHexEncoding ("test%a9test", 4), "#1");
771 Assert
.IsTrue (!Uri
.IsHexEncoding ("test%a9test", 3), "#2");
772 Assert
.IsTrue (Uri
.IsHexEncoding ("test%a9", 4), "#3");
773 Assert
.IsTrue (!Uri
.IsHexEncoding ("test%a", 4), "#4");
777 public void GetLeftPart ()
779 Uri uri
= new Uri ("http://www.contoso.com/index.htm#main");
780 Assert
.AreEqual ("http://", uri
.GetLeftPart (UriPartial
.Scheme
), "#1");
781 Assert
.AreEqual ("http://www.contoso.com", uri
.GetLeftPart (UriPartial
.Authority
), "#2");
782 Assert
.AreEqual ("http://www.contoso.com/index.htm", uri
.GetLeftPart (UriPartial
.Path
), "#3");
784 uri
= new Uri ("mailto:user@contoso.com?subject=uri");
785 Assert
.AreEqual ("mailto:", uri
.GetLeftPart (UriPartial
.Scheme
), "#4");
786 Assert
.AreEqual ("", uri
.GetLeftPart (UriPartial
.Authority
), "#5");
787 Assert
.AreEqual ("mailto:user@contoso.com", uri
.GetLeftPart (UriPartial
.Path
), "#6");
789 uri
= new Uri ("nntp://news.contoso.com/123456@contoso.com");
790 Assert
.AreEqual ("nntp://", uri
.GetLeftPart (UriPartial
.Scheme
), "#7");
791 Assert
.AreEqual ("nntp://news.contoso.com", uri
.GetLeftPart (UriPartial
.Authority
), "#8");
792 Assert
.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri
.GetLeftPart (UriPartial
.Path
), "#9");
794 uri
= new Uri ("news:123456@contoso.com");
795 Assert
.AreEqual ("news:", uri
.GetLeftPart (UriPartial
.Scheme
), "#10");
796 Assert
.AreEqual ("", uri
.GetLeftPart (UriPartial
.Authority
), "#11");
797 Assert
.AreEqual ("news:123456@contoso.com", uri
.GetLeftPart (UriPartial
.Path
), "#12");
799 uri
= new Uri ("file://server/filename.ext");
800 Assert
.AreEqual ("file://", uri
.GetLeftPart (UriPartial
.Scheme
), "#13");
801 Assert
.AreEqual ("file://server", uri
.GetLeftPart (UriPartial
.Authority
), "#14");
802 Assert
.AreEqual ("file://server/filename.ext", uri
.GetLeftPart (UriPartial
.Path
), "#15");
804 uri
= new Uri (@"\\server\share\filename.ext");
805 Assert
.AreEqual ("file://", uri
.GetLeftPart (UriPartial
.Scheme
), "#20");
806 Assert
.AreEqual ("file://server", uri
.GetLeftPart (UriPartial
.Authority
), "#21");
807 Assert
.AreEqual ("file://server/share/filename.ext", uri
.GetLeftPart (UriPartial
.Path
), "#22");
809 uri
= new Uri ("http://www.contoso.com:8080/index.htm#main");
810 Assert
.AreEqual ("http://", uri
.GetLeftPart (UriPartial
.Scheme
), "#23");
811 Assert
.AreEqual ("http://www.contoso.com:8080", uri
.GetLeftPart (UriPartial
.Authority
), "#24");
812 Assert
.AreEqual ("http://www.contoso.com:8080/index.htm", uri
.GetLeftPart (UriPartial
.Path
), "#25");
816 public void NewsDefaultPort ()
818 Uri uri
= new Uri("news://localhost:119/");
819 Assert
.AreEqual (uri
.IsDefaultPort
, true, "#1");
823 public void Fragment_Escape ()
825 Uri u
= new Uri("http://localhost/index.asp#main#start", false);
827 Assert
.AreEqual (u
.Fragment
, "#main#start", "#1");
829 u
= new Uri("http://localhost/index.asp#main#start", true);
830 Assert
.AreEqual (u
.Fragment
, "#main#start", "#2");
832 // The other code path uses a BaseUri
834 Uri b
= new Uri ("http://www.gnome.org");
835 Uri n
= new Uri (b
, "blah#main#start");
836 Assert
.AreEqual (n
.Fragment
, "#main#start", "#3");
838 n
= new Uri (b
, "blah#main#start", true);
839 Assert
.AreEqual (n
.Fragment
, "#main#start", "#4");
843 public void Fragment_RelativeUri ()
845 Uri uri1
= new Uri ("http://www.contoso.com/index.htm?x=2");
846 Uri uri2
= new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
847 Uri relativeUri
= uri1
.MakeRelativeUri (uri2
);
850 string fragment
= relativeUri
.Fragment
;
851 Assert
.Fail ("#1: " + fragment
);
852 } catch (InvalidOperationException ex
) {
853 // This operation is not supported for a relative URI
854 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
855 Assert
.IsNull (ex
.InnerException
, "#3");
856 Assert
.IsNotNull (ex
.Message
, "#4");
861 [ExpectedException(typeof(UriFormatException
))]
862 public void IncompleteSchemeDelimiter ()
864 new Uri ("file:/filename.ext");
868 public void CheckHostName1 ()
870 // reported to MSDN Product Feedback Center (FDBK28671)
871 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName (":11:22:33:44:55:66:77:88"), "#36 known to fail with ms.net: this is not a valid IPv6 address.");
875 public void CheckHostName2 ()
877 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName (null), "#1");
878 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName (""), "#2");
879 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("^&()~`!@"), "#3");
880 Assert
.AreEqual (UriHostNameType
.Dns
, Uri
.CheckHostName ("x"), "#4");
881 Assert
.AreEqual (UriHostNameType
.IPv4
, Uri
.CheckHostName ("1.2.3.4"), "#5");
882 Assert
.AreEqual (UriHostNameType
.IPv4
, Uri
.CheckHostName ("0001.002.03.4"), "#6");
883 Assert
.AreEqual (UriHostNameType
.Dns
, Uri
.CheckHostName ("0001.002.03.256"), "#7");
884 Assert
.AreEqual (UriHostNameType
.Dns
, Uri
.CheckHostName ("9001.002.03.4"), "#8");
885 Assert
.AreEqual (UriHostNameType
.Dns
, Uri
.CheckHostName ("www.contoso.com"), "#9");
886 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName (".www.contoso.com"), "#10");
887 Assert
.AreEqual (UriHostNameType
.Dns
, Uri
.CheckHostName ("www.contoso.com."), "#11");
888 Assert
.AreEqual (UriHostNameType
.Dns
, Uri
.CheckHostName ("www.con-toso.com"), "#12");
889 Assert
.AreEqual (UriHostNameType
.Dns
, Uri
.CheckHostName ("www.con_toso.com"), "#13");
890 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("www.con,toso.com"), "#14");
893 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
894 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("11::33:44:55:66:77:88"), "#16");
895 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
896 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
897 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("11::88"), "#19");
898 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("11::77:88"), "#20");
899 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("11:22::88"), "#21");
900 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("11::"), "#22");
901 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("::88"), "#23");
902 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("::1"), "#24");
903 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("::"), "#25");
904 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
905 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("::127.0.0.1"), "#27");
906 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("::ffFF:169.32.14.5"), "#28");
907 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("2001:03A0::/35"), "#29");
908 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("[2001:03A0::/35]"), "#30");
909 Assert
.AreEqual (UriHostNameType
.IPv6
, Uri
.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
911 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("2001::03A0::/35"), "#31");
912 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("2001:03A0::/35a"), "#32");
913 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("::ffff:123.256.155.43"), "#34");
914 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName (":127.0.0.1"), "#35");
915 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
916 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
917 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
918 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("::acbde"), "#40");
919 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("::abce:"), "#41");
920 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("::abcg"), "#42");
921 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName (":::"), "#43");
922 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName (":"), "#44");
924 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("*"), "#45");
925 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("*.example.com"), "#46");
926 Assert
.AreEqual (UriHostNameType
.Unknown
, Uri
.CheckHostName ("www*.example.com"), "#47");
930 public void IsLoopback ()
932 Uri uri
= new Uri("http://loopback:8080");
933 Assert
.AreEqual (true, uri
.IsLoopback
, "#1");
934 uri
= new Uri("http://localhost:8080");
935 Assert
.AreEqual (true, uri
.IsLoopback
, "#2");
936 uri
= new Uri("http://127.0.0.1:8080");
937 Assert
.AreEqual (true, uri
.IsLoopback
, "#3");
938 uri
= new Uri("http://127.0.0.001:8080");
939 Assert
.AreEqual (true, uri
.IsLoopback
, "#4");
940 uri
= new Uri("http://[::1]");
941 Assert
.AreEqual (true, uri
.IsLoopback
, "#5");
942 uri
= new Uri("http://[::1]:8080");
943 Assert
.AreEqual (true, uri
.IsLoopback
, "#6");
944 uri
= new Uri("http://[::0001]:8080");
945 Assert
.AreEqual (true, uri
.IsLoopback
, "#7");
946 uri
= new Uri("http://[0:0:0:0::1]:8080");
947 Assert
.AreEqual (true, uri
.IsLoopback
, "#8");
948 uri
= new Uri("http://[0:0:0:0::127.0.0.1]:8080");
949 Assert
.AreEqual (true, uri
.IsLoopback
, "#9");
950 uri
= new Uri("http://[0:0:0:0::127.11.22.33]:8080");
951 Assert
.AreEqual (false, uri
.IsLoopback
, "#10");
952 uri
= new Uri("http://[::ffff:127.11.22.33]:8080");
953 Assert
.AreEqual (false, uri
.IsLoopback
, "#11");
954 uri
= new Uri("http://[::ff00:7f11:2233]:8080");
955 Assert
.AreEqual (false, uri
.IsLoopback
, "#12");
956 uri
= new Uri("http://[1:0:0:0::1]:8080");
957 Assert
.AreEqual (false, uri
.IsLoopback
, "#13");
961 public void IsLoopback_File ()
963 Uri uri
= new Uri ("file:///index.html");
964 Assert
.IsTrue (uri
.IsLoopback
, "file");
968 public void IsLoopback_Relative_Http ()
970 string relative
= "http:8080/bar/Hello World.htm";
971 Uri uri
= new Uri (new Uri ("http://www.contoso.com"), relative
, false);
972 Assert
.IsTrue (!uri
.IsLoopback
, "http");
976 public void IsLoopback_Relative_Unknown ()
978 string relative
= "foo:8080/bar/Hello World.htm";
979 Uri uri
= new Uri (new Uri ("http://www.contoso.com"), relative
, false);
980 Assert
.IsTrue (!uri
.IsLoopback
, "foo");
984 public void Equals1 ()
986 Uri uri1
= new Uri ("http://www.contoso.com/index.htm#main");
987 Uri uri2
= new Uri ("http://www.contoso.com/index.htm#fragment");
988 Assert
.IsTrue (uri1
.Equals (uri2
), "#1");
989 Assert
.IsTrue (!uri2
.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
990 Assert
.IsTrue (!uri1
.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
994 public void Equals2 ()
996 Uri uri1
= new Uri ("http://www.contoso.com/index.htm#main");
997 Uri uri2
= new Uri ("http://www.contoso.com/index.htm?x=1");
998 Assert
.IsTrue (!uri1
.Equals (uri2
), "#2 known to fail with ms.net 1.x");
1002 public void Equals3 ()
1004 Uri uri1
= new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1005 Uri uri2
= new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1006 Assert
.IsTrue (uri1
.Equals (uri2
));
1010 public void Equals4 ()
1012 var uri
= new Uri ("http://w3.org");
1013 Assert
.IsFalse (uri
.Equals ("-"));
1017 public void TestEquals2 ()
1019 Uri a
= new Uri ("http://www.example.com");
1020 Uri b
= new Uri ("http://www.example.com");
1022 Assert
.AreEqual (a
, b
, "#1");
1024 a
= new Uri ("mailto:user:pwd@example.com?subject=uri");
1025 b
= new Uri ("MAILTO:USER:PWD@EXAMPLE.COM?SUBJECT=URI");
1027 Assert
.IsTrue (a
!= b
, "#2");
1028 Assert
.AreEqual ("mailto:user:pwd@example.com?subject=uri", a
.ToString (), "#2a");
1029 Assert
.AreEqual ("mailto:USER:PWD@example.com?SUBJECT=URI", b
.ToString (), "#2b");
1031 a
= new Uri ("http://www.example.com/ports/");
1032 b
= new Uri ("http://www.example.com/PORTS/");
1034 Assert
.IsTrue (!a
.Equals (b
), "#3");
1038 public void CaseSensitivity ()
1040 Uri mailto
= new Uri ("MAILTO:USER:PWD@EXAMPLE.COM?SUBJECT=URI");
1041 Assert
.AreEqual ("mailto", mailto
.Scheme
, "#1");
1042 Assert
.AreEqual ("example.com", mailto
.Host
, "#2");
1043 Assert
.AreEqual ("mailto:USER:PWD@example.com?SUBJECT=URI", mailto
.ToString (), "#3");
1045 Uri http
= new Uri ("HTTP://EXAMPLE.COM/INDEX.HTML");
1046 Assert
.AreEqual ("http", http
.Scheme
, "#4");
1047 Assert
.AreEqual ("example.com", http
.Host
, "#5");
1048 Assert
.AreEqual ("http://example.com/INDEX.HTML", http
.ToString (), "#6");
1051 Uri ftp
= new Uri ("FTP://[::ffFF:169.32.14.5]/");
1052 Assert
.AreEqual ("ftp", ftp
.Scheme
, "#7");
1054 Assert
.AreEqual ("[::ffff:169.32.14.5]", ftp
.Host
, "#8");
1055 Assert
.AreEqual ("ftp://[::ffff:169.32.14.5]/", ftp
.ToString (), "#9");
1059 public void GetHashCodeTest ()
1061 Uri uri1
= new Uri ("http://www.contoso.com/index.htm#main");
1062 Uri uri2
= new Uri ("http://www.contoso.com/index.htm#fragment");
1063 Assert
.AreEqual (uri1
.GetHashCode (), uri2
.GetHashCode (), "#1");
1064 uri2
= new Uri ("http://www.contoso.com/index.htm?x=1");
1065 Assert
.IsTrue (uri1
.GetHashCode () != uri2
.GetHashCode (), "#2");
1066 uri2
= new Uri ("http://www.contoso.com:80/index.htm");
1067 Assert
.AreEqual (uri1
.GetHashCode (), uri2
.GetHashCode (), "#3");
1068 uri2
= new Uri ("http://www.contoso.com:8080/index.htm");
1069 Assert
.IsTrue (uri1
.GetHashCode () != uri2
.GetHashCode (), "#4");
1073 public void RelativeEqualsTest()
1075 Uri uri1
= new Uri ("foo/bar", UriKind
.Relative
);
1076 Uri uri2
= new Uri ("foo/bar", UriKind
.Relative
);
1077 Uri uri3
= new Uri ("bar/man", UriKind
.Relative
);
1078 Uri uri4
= new Uri ("BAR/MAN", UriKind
.Relative
);
1079 Assert
.IsTrue (uri1
== uri2
, "#1a");
1080 Assert
.IsTrue (uri1
.Equals(uri2
), "#1b");
1081 Assert
.IsTrue (uri1
!= uri3
, "#2a");
1082 Assert
.IsTrue (!uri1
.Equals(uri3
), "#2b");
1083 Assert
.IsTrue (uri1
== uri2
, "#3a");
1084 Assert
.IsTrue (uri1
.Equals(uri2
), "#3b");
1085 Assert
.IsTrue (uri1
!= uri3
, "#4a");
1086 Assert
.IsTrue (!uri1
.Equals(uri3
), "#4b");
1087 Assert
.IsTrue (uri3
!= uri4
, "#5a");
1088 Assert
.IsTrue (!uri3
.Equals(uri4
), "#5b");
1092 [ExpectedException(typeof(InvalidOperationException
))]
1093 public void GetLeftPart_Partial1 ()
1095 Uri u
= new Uri ("foo", UriKind
.Relative
);
1096 u
.GetLeftPart (UriPartial
.Scheme
);
1099 [ExpectedException(typeof(InvalidOperationException
))]
1101 public void GetLeftPart_Partial2 ()
1103 Uri u
= new Uri ("foo", UriKind
.Relative
);
1104 u
.GetLeftPart (UriPartial
.Authority
);
1107 [ExpectedException(typeof(InvalidOperationException
))]
1109 public void GetLeftPart_Partial3 ()
1111 Uri u
= new Uri ("foo", UriKind
.Relative
);
1112 u
.GetLeftPart (UriPartial
.Path
);
1116 public void TestPartialToString ()
1118 Assert
.AreEqual (new Uri ("foo", UriKind
.Relative
).ToString (), "foo", "#1");
1119 Assert
.AreEqual (new Uri ("foo#aa", UriKind
.Relative
).ToString (), "foo#aa", "#2");
1120 Assert
.AreEqual (new Uri ("foo?aa", UriKind
.Relative
).ToString (), "foo?aa", "#3");
1121 Assert
.AreEqual (new Uri ("foo#dingus?aa", UriKind
.Relative
).ToString (), "foo#dingus?aa", "#4");
1122 Assert
.AreEqual (new Uri ("foo?dingus#aa", UriKind
.Relative
).ToString (), "foo?dingus#aa", "#4");
1126 public void RelativeGetHashCodeTest()
1128 Uri uri1
= new Uri ("foo/bar", UriKind
.Relative
);
1129 Uri uri2
= new Uri ("foo/bar", UriKind
.Relative
);
1130 Uri uri3
= new Uri ("bar/man", UriKind
.Relative
);
1131 Assert
.AreEqual (uri1
.GetHashCode(), uri2
.GetHashCode(), "#1");
1132 Assert
.IsTrue (uri1
.GetHashCode() != uri3
.GetHashCode(), "#2");
1136 public void MakeRelative ()
1138 Uri uri1
= new Uri ("http://www.contoso.com/index.htm?x=2");
1139 Uri uri2
= new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1140 Uri uri3
= new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1141 Uri uri4
= new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1142 Uri uri5
= new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1143 Uri uri6
= new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1144 Uri uri7
= new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1145 Uri uri8
= new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1147 Assert
.AreEqual ("foo/bar/index.htm", uri1
.MakeRelative (uri2
), "#1");
1148 Assert
.AreEqual ("../../index.htm", uri2
.MakeRelative (uri1
), "#2");
1150 Assert
.AreEqual ("../../bar/foo/index.htm", uri2
.MakeRelative (uri3
), "#3");
1151 Assert
.AreEqual ("../../foo/bar/index.htm", uri3
.MakeRelative (uri2
), "#4");
1153 Assert
.AreEqual ("../foo2/index.htm", uri3
.MakeRelative (uri4
), "#5");
1154 Assert
.AreEqual ("../foo/index.htm", uri4
.MakeRelative (uri3
), "#6");
1156 Assert
.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4
.MakeRelative (uri5
), "#7");
1158 Assert
.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4
.MakeRelative (uri6
), "#8");
1160 Assert
.AreEqual ("", uri6
.MakeRelative (uri6
), "#9");
1161 Assert
.AreEqual ("foobar.htm", uri6
.MakeRelative (uri7
), "#10");
1163 Uri uri10
= new Uri ("mailto:xxx@xxx.com");
1164 Uri uri11
= new Uri ("mailto:xxx@xxx.com?subject=hola");
1165 Assert
.AreEqual ("", uri10
.MakeRelative (uri11
), "#11");
1167 Uri uri12
= new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1168 Assert
.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10
.MakeRelative (uri12
), "#12");
1170 Uri uri13
= new Uri ("mailto:xxx@xxx.com/foo/bar");
1171 Assert
.AreEqual ("/foo/bar", uri10
.MakeRelative (uri13
), "#13");
1173 Assert
.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1
.MakeRelative (uri8
), "#14");
1177 public void RelativeFragmentUri ()
1179 Uri u
= new Uri("http://localhost/../../../a");
1180 Assert
.AreEqual ("http://localhost/a", u
.ToString ());
1182 u
= new Uri ("http://localhost/../c/b/../a");
1183 Assert
.AreEqual ("http://localhost/c/a", u
.ToString ());
1187 public void RelativeFragmentUri2 ()
1189 Assert
.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1191 Assert
.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1192 Assert
.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1193 Assert
.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1194 Assert
.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1196 Assert
.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1197 Assert
.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1198 Assert
.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1199 Assert
.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1201 Assert
.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1205 public void ToStringTest()
1207 Uri uri
= new Uri ("dummy://xxx");
1208 Assert
.AreEqual ("dummy://xxx/", uri
.ToString (), "#1");
1212 public void CheckSchemeName ()
1214 Assert
.AreEqual (false, Uri
.CheckSchemeName (null), "#01");
1215 Assert
.AreEqual (false, Uri
.CheckSchemeName (""), "#02");
1216 Assert
.AreEqual (true, Uri
.CheckSchemeName ("http"), "#03");
1217 Assert
.AreEqual (true, Uri
.CheckSchemeName ("http-"), "#04");
1218 Assert
.AreEqual (false, Uri
.CheckSchemeName ("6http-"), "#05");
1219 Assert
.AreEqual (true, Uri
.CheckSchemeName ("http6-"), "#06");
1220 Assert
.AreEqual (false, Uri
.CheckSchemeName ("http6,"), "#07");
1221 Assert
.AreEqual (true, Uri
.CheckSchemeName ("http6."), "#08");
1222 Assert
.AreEqual (false, Uri
.CheckSchemeName ("+http"), "#09");
1223 Assert
.AreEqual (true, Uri
.CheckSchemeName ("htt+p6"), "#10");
1224 // 0x00E1 -> ã
1225 Assert
.IsTrue (!Uri
.CheckSchemeName ("htt\u00E1+p6"), "#11");
1229 public void CheckSchemeName_FirstChar ()
1231 for (int i
= 0; i
< 256; i
++) {
1232 string s
= String
.Format ("#{0}", i
);
1234 bool b
= Uri
.CheckSchemeName (c
.ToString ());
1235 bool valid
= (((i
>= 0x41) && (i
<= 0x5A)) || ((i
>= 0x61) && (i
<= 0x7A)));
1236 Assert
.AreEqual (valid
, b
, s
);
1241 public void CheckSchemeName_AnyOtherChar ()
1243 for (int i
= 0; i
< 256; i
++) {
1244 string s
= String
.Format ("#{0}", i
);
1246 string scheme
= String
.Format ("a+b-c.d{0}", c
);
1247 bool b
= Uri
.CheckSchemeName (scheme
);
1248 bool common
= Char
.IsDigit (c
) || (c
== '+') || (c
== '-') || (c
== '.');
1249 bool valid
= (common
|| ((i
>= 0x41) && (i
<= 0x5A)) || ((i
>= 0x61) && (i
<= 0x7A)));
1250 Assert
.AreEqual (valid
, b
, s
);
1255 [ExpectedException (typeof (UriFormatException
))]
1256 public void NoHostname ()
1258 Uri uri
= new Uri ("http://");
1262 public void NoHostname2 ()
1265 Uri uri
= new Uri ("file://");
1266 Assert
.AreEqual (true, uri
.IsFile
, "#1");
1267 Assert
.AreEqual (false, uri
.IsUnc
, "#2");
1268 Assert
.AreEqual ("file", uri
.Scheme
, "#3");
1269 Assert
.AreEqual ("/", uri
.LocalPath
, "#4");
1270 Assert
.AreEqual (string.Empty
, uri
.Query
, "#5");
1271 Assert
.AreEqual ("/", uri
.AbsolutePath
, "#6");
1272 Assert
.AreEqual ("file:///", uri
.AbsoluteUri
, "#7");
1273 Assert
.AreEqual (string.Empty
, uri
.Authority
, "#8");
1274 Assert
.AreEqual (string.Empty
, uri
.Host
, "#9");
1275 Assert
.AreEqual (UriHostNameType
.Basic
, uri
.HostNameType
, "#10");
1276 Assert
.AreEqual (string.Empty
, uri
.Fragment
, "#11");
1277 Assert
.AreEqual (true, uri
.IsDefaultPort
, "#12");
1278 Assert
.IsTrue (uri
.IsLoopback
, "#13");
1279 Assert
.AreEqual ("/", uri
.PathAndQuery
, "#14");
1280 Assert
.AreEqual (false, uri
.UserEscaped
, "#15");
1281 Assert
.AreEqual (string.Empty
, uri
.UserInfo
, "#16");
1282 Assert
.AreEqual ("file://", uri
.GetLeftPart (UriPartial
.Authority
), "#17");
1283 Assert
.AreEqual ("file:///", uri
.GetLeftPart (UriPartial
.Path
), "#18");
1284 Assert
.AreEqual ("file://", uri
.GetLeftPart (UriPartial
.Scheme
), "#19");
1288 public void Segments1 ()
1290 Uri uri
= new Uri ("http://localhost/");
1291 string [] segments
= uri
.Segments
;
1292 Assert
.AreEqual (1, segments
.Length
, "#01");
1293 Assert
.AreEqual ("/", segments
[0], "#02");
1297 public void Segments2 ()
1299 Uri uri
= new Uri ("http://localhost/dir/dummypage.html");
1300 string [] segments
= uri
.Segments
;
1301 Assert
.AreEqual (3, segments
.Length
, "#01");
1302 Assert
.AreEqual ("/", segments
[0], "#02");
1303 Assert
.AreEqual ("dir/", segments
[1], "#03");
1304 Assert
.AreEqual ("dummypage.html", segments
[2], "#04");
1308 public void CachingSegments ()
1310 Uri uri
= new Uri ("http://localhost/dir/dummypage.html");
1311 uri
.Segments
[0] = uri
.Segments
[1] = uri
.Segments
[2] = "*";
1312 string [] segments
= uri
.Segments
;
1313 Assert
.AreEqual (3, segments
.Length
, "#01");
1314 Assert
.AreEqual ("/", segments
[0], "#02");
1315 Assert
.AreEqual ("dir/", segments
[1], "#03");
1316 Assert
.AreEqual ("dummypage.html", segments
[2], "#04");
1320 public void Segments3 ()
1322 Uri uri
= new Uri ("http://localhost/dir/dummypage/");
1323 string [] segments
= uri
.Segments
;
1324 Assert
.AreEqual (3, segments
.Length
, "#01");
1325 Assert
.AreEqual ("/", segments
[0], "#02");
1326 Assert
.AreEqual ("dir/", segments
[1], "#03");
1327 Assert
.AreEqual ("dummypage/", segments
[2], "#04");
1331 public void Segments4 ()
1333 Uri uri
= new Uri ("file:///c:/hello");
1335 Assert
.AreEqual ("c:/hello", uri
.AbsolutePath
, "AbsolutePath");
1336 Assert
.AreEqual ("c:\\hello", uri
.LocalPath
, "LocalPath");
1338 string [] segments
= uri
.Segments
;
1339 Assert
.AreEqual (3, segments
.Length
, "#01");
1340 Assert
.AreEqual ("/", segments
[0], "#02");
1341 Assert
.AreEqual ("c:/", segments
[1], "#03");
1342 Assert
.AreEqual ("hello", segments
[2], "#04");
1346 public void Segments5 ()
1348 Uri uri
= new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1349 string [] segments
= uri
.Segments
;
1350 Assert
.AreEqual (4, segments
.Length
, "#01");
1351 Assert
.AreEqual ("/", segments
[0], "#02");
1352 Assert
.AreEqual ("bar/", segments
[1], "#03");
1353 Assert
.AreEqual ("foo/", segments
[2], "#04");
1354 Assert
.AreEqual ("foobar.htm", segments
[3], "#05");
1358 [ExpectedException (typeof (UriFormatException
))]
1359 public void UriStartingWithColon()
1365 [ExpectedException (typeof (UriFormatException
))]
1366 public void EmptyScheme ()
1372 public void SchemeWithDigits ()
1374 Uri uri
= new Uri ("net.p2p://foobar");
1375 Assert
.AreEqual ("net.p2p", uri
.Scheme
);
1378 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1379 [ExpectedException (typeof (UriFormatException
))]
1381 public void InvalidPort1 ()
1383 Uri uri
= new Uri ("http://www.contoso.com:65536/foo/bar/");
1384 Assert
.AreEqual (65536, uri
.Port
);
1387 [ExpectedException (typeof (UriFormatException
))]
1389 public void InvalidPort2 ()
1391 // UInt32.MaxValue gives port == -1 !!!
1392 Uri uri
= new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1393 Assert
.AreEqual (-1, uri
.Port
);
1396 [ExpectedException (typeof (UriFormatException
))]
1398 public void InvalidPort3 ()
1400 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1401 Uri uri
= new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1402 Assert
.AreEqual (-2147483648, uri
.Port
);
1406 public void PortMax ()
1408 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1409 Uri uri
= new Uri ("http://www.contoso.com:65535/foo/bar/");
1410 Assert
.AreEqual (65535, uri
.Port
);
1415 public UriEx2 (string s
) : base (s
)
1419 protected override void Parse ()
1424 // Parse method is no longer used on .NET 2.0
1425 [ExpectedException (typeof (UriFormatException
))]
1427 public void ParseOverride ()
1429 // If this does not override base's Parse(), it will
1430 // fail since this argument is not Absolute URI.
1431 UriEx2 ex
= new UriEx2 ("readme.txt");
1435 public void UnixLocalPath ()
1437 // This works--the location is not part of the absolute path
1438 string path
= "file://localhost/tmp/foo/bar";
1439 Uri fileUri
= new Uri( path
);
1440 Assert
.AreEqual ("/tmp/foo/bar", fileUri
.AbsolutePath
, path
);
1442 // Empty path == localhost, in theory
1443 path
= "file:///c:/tmp/foo/bar";
1444 fileUri
= new Uri( path
);
1445 Assert
.AreEqual ("c:/tmp/foo/bar", fileUri
.AbsolutePath
, path
);
1449 public void WindowsLocalPath ()
1451 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1455 public void TestEscapeDataString ()
1457 StringBuilder sb
= new StringBuilder ();
1459 for (int i
= 0; i
< 128; i
++)
1460 sb
.Append ((char) i
);
1463 "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F",
1464 Uri
.EscapeDataString (sb
.ToString ()));
1466 Assert
.AreEqual ("%C3%A1", Uri
.EscapeDataString ("á"));
1469 public void TestEscapeUriString ()
1471 StringBuilder sb
= new StringBuilder ();
1472 for (int i
= 0; i
< 128; i
++)
1473 sb
.Append ((char) i
);
1476 "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[%5C]%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F",
1477 Uri
.EscapeUriString (sb
.ToString ()));
1479 Assert
.AreEqual ("%C3%A1", Uri
.EscapeDataString ("á"));
1484 public void TestUTF8Strings ()
1487 "file:///tmp/x (%232).jpg",
1488 "file:///tmp/ü (%232).jpg" };
1490 foreach (string test
in tests
) {
1491 Uri uri
= new Uri (test
);
1492 Assert
.IsFalse (uri
.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test
);
1493 Assert
.AreEqual (test
, uri
.OriginalString
, "OriginalString/" + test
);
1494 Assert
.AreEqual (test
, uri
.ToString (), "ToString/" + test
);
1498 // This test doesn't work on Linux, and arguably shouldn't work.
1499 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1500 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1501 // Apparently "tmp" is supposed to be a hostname (!)...
1502 // Since "correct" behavior would confuse all Linux developers, and having
1503 // an expected failure is evil, we'll just ignore this for now...
1505 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1507 // You are surrounded by conditional-compilation code, all alike.
1508 // You are likely to be eaten by a Grue...
1510 public void UnixLocalPath_WTF ()
1512 // Empty path == localhost, in theory
1513 string path
= "file:///tmp/foo/bar";
1514 Uri fileUri
= new Uri( path
);
1515 Assert
.AreEqual ("/tmp/foo/bar", fileUri
.AbsolutePath
, path
);
1518 string path2
= "file:///foo%25bar";
1519 fileUri
= new Uri (path2
);
1520 Assert
.AreEqual ("file:///foo%25bar", fileUri
.ToString (), path2
);
1523 public static void Print (Uri uri
)
1525 Console
.WriteLine ("ToString: " + uri
.ToString ());
1527 Console
.WriteLine ("AbsolutePath: " + uri
.AbsolutePath
);
1528 Console
.WriteLine ("AbsoluteUri: " + uri
.AbsoluteUri
);
1529 Console
.WriteLine ("Authority: " + uri
.Authority
);
1530 Console
.WriteLine ("Fragment: " + uri
.Fragment
);
1531 Console
.WriteLine ("Host: " + uri
.Host
);
1532 Console
.WriteLine ("HostNameType: " + uri
.HostNameType
);
1533 Console
.WriteLine ("IsDefaultPort: " + uri
.IsDefaultPort
);
1534 Console
.WriteLine ("IsFile: " + uri
.IsFile
);
1535 Console
.WriteLine ("IsLoopback: " + uri
.IsLoopback
);
1536 Console
.WriteLine ("IsUnc: " + uri
.IsUnc
);
1537 Console
.WriteLine ("LocalPath: " + uri
.LocalPath
);
1538 Console
.WriteLine ("PathAndQuery : " + uri
.PathAndQuery
);
1539 Console
.WriteLine ("Port: " + uri
.Port
);
1540 Console
.WriteLine ("Query: " + uri
.Query
);
1541 Console
.WriteLine ("Scheme: " + uri
.Scheme
);
1542 Console
.WriteLine ("UserEscaped: " + uri
.UserEscaped
);
1543 Console
.WriteLine ("UserInfo: " + uri
.UserInfo
);
1545 Console
.WriteLine ("Segments:");
1546 string [] segments
= uri
.Segments
;
1547 if (segments
== null)
1548 Console
.WriteLine ("\tNo Segments");
1550 for (int i
= 0; i
< segments
.Length
; i
++)
1551 Console
.WriteLine ("\t" + segments
[i
]);
1552 Console
.WriteLine ("");
1556 public void FtpRootPath ()
1558 Uri u
= new Uri ("ftp://a.b/%2fabc/def");
1559 string p
= u
.PathAndQuery
;
1560 Assert
.AreEqual ("/%2fabc/def", p
);
1561 p
= Uri
.UnescapeDataString (p
).Substring (1);
1562 Assert
.AreEqual ("/abc/def", p
);
1563 u
= new Uri (new Uri ("ftp://a.b/c/d/e/f"), p
);
1564 Assert
.AreEqual ("/abc/def", u
.PathAndQuery
);
1569 public void LocalPath_FileNameWithAtSign1 ()
1571 string path
= "/some/path/file_with_an_@_sign.mp3";
1572 string fullpath
= "http://thehost" + path
;
1573 Uri fileUri
= new Uri (fullpath
);
1575 Assert
.AreEqual (fileUri
.UserInfo
, String
.Empty
, "LocalPath_FileNameWithAtSign UserInfo");
1576 Assert
.AreEqual (fileUri
.Host
, "thehost", "LocalPath_FileNameWithAtSign Host");
1577 Assert
.IsFalse (fileUri
.IsFile
, "LocalPath_FileNameWithAtSign IsFile");
1578 Assert
.IsTrue (fileUri
.IsAbsoluteUri
, "LocalPath_FileNameWithAtSign IsAbsUri");
1579 Assert
.IsFalse (fileUri
.IsUnc
, "LocalPath_FileNameWithAtSign IsUnc");
1581 Assert
.AreEqual (fullpath
, fileUri
.OriginalString
, "LocalPath_FileNameWithAtSign OriginalString");
1582 Assert
.AreEqual (path
, new DerivedUri (fullpath
).TestUnescape (path
), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1583 Assert
.AreEqual (path
, fileUri
.AbsolutePath
, "LocalPath_FileNameWithAtSign AbsPath");
1584 Assert
.AreEqual (path
, fileUri
.LocalPath
, "LocalPath_FileNameWithAtSign LocalPath");
1588 public void LocalPath_FileNameWithAtSign2 ()
1590 string path
= "/some/path/file_with_an_@_sign.mp3";
1591 string fullpath
= "http://user:password@thehost" + path
;
1592 Uri fileUri
= new Uri (fullpath
);
1594 Assert
.AreEqual (fileUri
.UserInfo
, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1595 Assert
.AreEqual (fileUri
.Host
, "thehost", "LocalPath_FileNameWithAtSign Host");
1596 Assert
.IsFalse (fileUri
.IsFile
, "LocalPath_FileNameWithAtSign IsFile");
1597 Assert
.IsTrue (fileUri
.IsAbsoluteUri
, "LocalPath_FileNameWithAtSign IsAbsUri");
1598 Assert
.IsFalse (fileUri
.IsUnc
, "LocalPath_FileNameWithAtSign IsUnc");
1600 Assert
.AreEqual (fullpath
, fileUri
.OriginalString
, "LocalPath_FileNameWithAtSign OriginalString");
1601 Assert
.AreEqual (path
, new DerivedUri (fullpath
).TestUnescape (path
), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1602 Assert
.AreEqual (path
, fileUri
.AbsolutePath
, "LocalPath_FileNameWithAtSign AbsPath");
1603 Assert
.AreEqual (path
, fileUri
.LocalPath
, "LocalPath_FileNameWithAtSign LocalPath");
1607 public void LocalPath_FileNameWithAtSign3 ()
1609 string path
= "/some/path/file_with_an_@_sign.mp3";
1610 string fullpath
= "file://" + path
;
1611 Uri fileUri
= new Uri (fullpath
);
1613 Assert
.AreEqual (fileUri
.UserInfo
, String
.Empty
, "LocalPath_FileNameWithAtSign UserInfo");
1614 Assert
.AreEqual (fileUri
.Host
, String
.Empty
, "LocalPath_FileNameWithAtSign Host");
1615 Assert
.IsTrue (fileUri
.IsFile
, "LocalPath_FileNameWithAtSign IsFile");
1616 Assert
.IsTrue (fileUri
.IsAbsoluteUri
, "LocalPath_FileNameWithAtSign IsAbsUri");
1617 Assert
.IsFalse (fileUri
.IsUnc
, "LocalPath_FileNameWithAtSign IsUnc");
1619 Assert
.AreEqual (fullpath
, fileUri
.OriginalString
, "LocalPath_FileNameWithAtSign OriginalString");
1620 Assert
.AreEqual (path
, new DerivedUri (fullpath
).TestUnescape (path
), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1621 Assert
.AreEqual (path
, fileUri
.AbsolutePath
, "LocalPath_FileNameWithAtSign AbsPath");
1622 Assert
.AreEqual (path
, fileUri
.LocalPath
, "LocalPath_FileNameWithAtSign LocalPath");
1626 public void LocalPath_FileNameWithAtSign4 ()
1628 string path
= "/some/path/file_with_an_@_sign.mp3";
1629 string fullpath
= "file://localhost" + path
;
1630 Uri fileUri
= new Uri (fullpath
);
1632 Assert
.AreEqual (fileUri
.UserInfo
, String
.Empty
, "LocalPath_FileNameWithAtSign UserInfo");
1633 Assert
.AreEqual (fileUri
.Host
, "localhost", "LocalPath_FileNameWithAtSign Host");
1634 Assert
.IsTrue (fileUri
.IsFile
, "LocalPath_FileNameWithAtSign IsFile");
1635 Assert
.IsTrue (fileUri
.IsAbsoluteUri
, "LocalPath_FileNameWithAtSign IsAbsUri");
1636 Assert
.AreEqual (isWin32
, fileUri
.IsUnc
, "LocalPath_FileNameWithAtSign IsUnc");
1638 Assert
.AreEqual (fullpath
, fileUri
.OriginalString
, "LocalPath_FileNameWithAtSign OriginalString");
1639 Assert
.AreEqual (path
, new DerivedUri (fullpath
).TestUnescape (path
), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1640 Assert
.AreEqual (path
, fileUri
.AbsolutePath
, "LocalPath_FileNameWithAtSign AbsPath");
1641 //this test is marked as NotWorking below:
1642 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1646 public void LocalPath_FileNameWithAtSign5 ()
1648 string path
= "/some/path/file_with_an_@_sign.mp3";
1649 string fullpath
= "file://localhost" + path
;
1650 Uri fileUri
= new Uri (fullpath
);
1652 string expected
= isWin32
? "\\\\localhost" + path
.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1653 Assert
.AreEqual (expected
, fileUri
.LocalPath
, "LocalPath_FileNameWithAtSign LocalPath");
1657 [ExpectedException (typeof (UriFormatException
))]
1658 public void LocalPath_FileNameWithAtSign6 ()
1660 string path
= "/some/path/file_with_an_@_sign.mp3";
1661 string fullpath
= "file://user:password@localhost" + path
;
1662 Uri fileUri
= new Uri (fullpath
);
1666 [Category ("NotDotNet")]
1667 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1672 Uri unixuri
= new Uri ("/home/user/a@b");
1673 Assert
.AreEqual ("file", unixuri
.Scheme
, "UnixAbsoluteFilePath_WithSpecialChars #1");
1677 [Category ("NotDotNet")]
1678 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1683 Uri unixuri
= new Uri ("/home/user/a:b");
1684 Assert
.AreEqual ("file", unixuri
.Scheme
, "UnixAbsoluteFilePath_WithSpecialChars #2");
1688 [Category ("NotDotNet")]
1689 public void UnixAbsolutePath_ReplaceRelative ()
1694 var u1
= new Uri ("/Users/demo/Projects/file.xml");
1695 var u2
= new Uri (u1
, "b.jpg");
1697 Assert
.AreEqual ("file:///Users/demo/Projects/b.jpg", u2
.ToString ());
1701 public void RelativeUriWithColons ()
1703 string s
= @"Transform?args=[{""__type"":""Record:#Nostr"",""Code"":""%22test%22SomeGloss"",""ID"":""1"",""Table"":""Glossary""},{""__type"":""Record:#Nostr"",""Code"":""%22test%22All"",""ID"":""2"",""Table"":""GlossView""}, {""__type"":""Record:#Nostr"",""Code"":""%22test%22Q"",""ID"":""3"",""Table"":""Glossary""}]"; // with related to bug #573795
1704 new Uri (s
, UriKind
.Relative
);
1705 new Uri (":", UriKind
.Relative
);
1706 new Uri ("1:", UriKind
.Relative
);
1710 public void ConsecutiveSlashes ()
1712 Uri uri
= new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1713 Assert
.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri
.ToString ());
1716 public class DerivedUri
: Uri
{
1717 public DerivedUri (string uriString
)
1722 internal string TestUnescape (string path
)
1724 return base.Unescape (path
);
1729 [ExpectedException (typeof (InvalidOperationException
))]
1730 public void GetComponents_Relative ()
1732 Uri rel
= new Uri ("/relative/path/with?query", UriKind
.Relative
);
1733 rel
.GetComponents (UriComponents
.Query
, UriFormat
.SafeUnescaped
);
1737 public void GetComponents_AbsoluteUri ()
1739 Uri uri
= new Uri ("http://example.com/list?id=1%262&sort=asc#fragment%263");
1741 Assert
.AreEqual ("http://example.com/list?id=1%262&sort=asc#fragment%263", uri
.AbsoluteUri
, "AbsoluteUri");
1743 string safe
= uri
.GetComponents (UriComponents
.AbsoluteUri
, UriFormat
.SafeUnescaped
);
1744 Assert
.AreEqual ("http://example.com/list?id=1%262&sort=asc#fragment%263", safe
, "SafeUnescaped");
1746 string unescaped
= uri
.GetComponents (UriComponents
.AbsoluteUri
, UriFormat
.Unescaped
);
1747 Assert
.AreEqual ("http://example.com/list?id=1&2&sort=asc#fragment&3", unescaped
, "Unescaped");
1749 string escaped
= uri
.GetComponents (UriComponents
.AbsoluteUri
, UriFormat
.UriEscaped
);
1750 Assert
.AreEqual ("http://example.com/list?id=1%262&sort=asc#fragment%263", escaped
, "UriEscaped");
1754 public void GetComponents_HttpRequestUrl ()
1756 Uri uri
= new Uri ("http://example.com/list?id=1%262&sort=asc#fragment%263");
1758 string safe
= uri
.GetComponents (UriComponents
.HttpRequestUrl
, UriFormat
.SafeUnescaped
);
1759 Assert
.AreEqual ("http://example.com/list?id=1%262&sort=asc", safe
, "SafeUnescaped");
1761 string unescaped
= uri
.GetComponents (UriComponents
.HttpRequestUrl
, UriFormat
.Unescaped
);
1762 Assert
.AreEqual ("http://example.com/list?id=1&2&sort=asc", unescaped
, "Unescaped");
1764 string escaped
= uri
.GetComponents (UriComponents
.HttpRequestUrl
, UriFormat
.UriEscaped
);
1765 Assert
.AreEqual ("http://example.com/list?id=1%262&sort=asc", escaped
, "UriEscaped");
1769 public void GetComponents_KeepDelimiter ()
1771 Uri uri
= new Uri ("http://example.com/list?id=1%262&sort=asc#fragment%263");
1773 string safe
= uri
.GetComponents (UriComponents
.KeepDelimiter
, UriFormat
.SafeUnescaped
);
1774 Assert
.AreEqual (String
.Empty
, safe
, "SafeUnescaped");
1776 string unescaped
= uri
.GetComponents (UriComponents
.KeepDelimiter
, UriFormat
.Unescaped
);
1777 Assert
.AreEqual (String
.Empty
, unescaped
, "Unescaped");
1779 string escaped
= uri
.GetComponents (UriComponents
.KeepDelimiter
, UriFormat
.UriEscaped
);
1780 Assert
.AreEqual (String
.Empty
, escaped
, "UriEscaped");
1784 public void GetComponents_StrongAuthority ()
1786 Uri uri
= new Uri ("http://example.com/list?id=1%262&sort=asc#fragment%263");
1788 string safe
= uri
.GetComponents (UriComponents
.StrongAuthority
, UriFormat
.SafeUnescaped
);
1789 Assert
.AreEqual ("example.com:80", safe
, "SafeUnescaped");
1791 string unescaped
= uri
.GetComponents (UriComponents
.StrongAuthority
, UriFormat
.Unescaped
);
1792 Assert
.AreEqual ("example.com:80", unescaped
, "Unescaped");
1794 string escaped
= uri
.GetComponents (UriComponents
.StrongAuthority
, UriFormat
.UriEscaped
);
1795 Assert
.AreEqual ("example.com:80", escaped
, "UriEscaped");
1799 public void GetComponents_Path ()
1801 Uri uri1
= new Uri ("http://example.com/Main%20Page");
1802 Assert
.AreEqual ("/Main Page", uri1
.LocalPath
, "Path1");
1804 string safe
= uri1
.GetComponents (UriComponents
.Path
, UriFormat
.SafeUnescaped
);
1805 Assert
.AreEqual ("Main Page", safe
, "SafeUnescaped1");
1807 string unescaped
= uri1
.GetComponents (UriComponents
.Path
, UriFormat
.Unescaped
);
1808 Assert
.AreEqual ("Main Page", unescaped
, "Unescaped1");
1810 string escaped
= uri1
.GetComponents (UriComponents
.Path
, UriFormat
.UriEscaped
);
1811 Assert
.AreEqual ("Main%20Page", escaped
, "UriEscaped1");
1813 // same result is unescaped original string
1814 Uri uri2
= new Uri ("http://example.com/Main Page");
1815 Assert
.AreEqual ("/Main Page", uri2
.LocalPath
, "Path2");
1817 safe
= uri2
.GetComponents (UriComponents
.Path
, UriFormat
.SafeUnescaped
);
1818 Assert
.AreEqual ("Main Page", safe
, "SafeUnescaped2");
1820 unescaped
= uri2
.GetComponents (UriComponents
.Path
, UriFormat
.Unescaped
);
1821 Assert
.AreEqual ("Main Page", unescaped
, "Unescaped2");
1823 escaped
= uri2
.GetComponents (UriComponents
.Path
, UriFormat
.UriEscaped
);
1824 Assert
.AreEqual ("Main%20Page", escaped
, "UriEscaped2");
1828 public void GetComponents_PathAndQuery ()
1830 Uri uri
= new Uri ("http://example.com/MåÏn Påge?id=1%262&sort=asc");
1832 Assert
.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri
.PathAndQuery
, "PathAndQuery");
1834 string safe
= uri
.GetComponents (UriComponents
.PathAndQuery
, UriFormat
.SafeUnescaped
);
1835 Assert
.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe
, "SafeUnescaped");
1837 string unescaped
= uri
.GetComponents (UriComponents
.PathAndQuery
, UriFormat
.Unescaped
);
1838 Assert
.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped
, "Unescaped");
1840 string escaped
= uri
.GetComponents (UriComponents
.PathAndQuery
, UriFormat
.UriEscaped
);
1841 Assert
.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped
, "UriEscaped");
1845 public void GetComponents_Query ()
1847 Uri uri
= new Uri ("http://example.com/list?id=1%262&sort=asc");
1849 Assert
.AreEqual ("?id=1%262&sort=asc", uri
.Query
, "Query");
1851 string safe
= uri
.GetComponents (UriComponents
.Query
, UriFormat
.SafeUnescaped
);
1852 Assert
.AreEqual ("id=1%262&sort=asc", safe
, "SafeUnescaped");
1854 string unescaped
= uri
.GetComponents (UriComponents
.Query
, UriFormat
.Unescaped
);
1855 Assert
.AreEqual ("id=1&2&sort=asc", unescaped
, "Unescaped");
1857 string escaped
= uri
.GetComponents (UriComponents
.Query
, UriFormat
.UriEscaped
);
1858 Assert
.AreEqual ("id=1%262&sort=asc", escaped
, "UriEscaped");
1862 public void GetComponents_Fragment ()
1864 Uri uri
= new Uri ("http://example.com/list#id=1%262&sort=asc");
1866 Assert
.AreEqual ("#id=1%262&sort=asc", uri
.Fragment
, "Fragment");
1868 string safe
= uri
.GetComponents (UriComponents
.Fragment
, UriFormat
.SafeUnescaped
);
1869 Assert
.AreEqual ("id=1%262&sort=asc", safe
, "SafeUnescaped");
1871 string unescaped
= uri
.GetComponents (UriComponents
.Fragment
, UriFormat
.Unescaped
);
1872 Assert
.AreEqual ("id=1&2&sort=asc", unescaped
, "Unescaped");
1874 string escaped
= uri
.GetComponents (UriComponents
.Fragment
, UriFormat
.UriEscaped
);
1875 Assert
.AreEqual ("id=1%262&sort=asc", escaped
, "UriEscaped");
1878 // When used, paths such as "/foo" are assumed relative.
1879 static UriKind DotNetRelativeOrAbsolute
= (Type
.GetType ("Mono.Runtime") == null)? UriKind
.RelativeOrAbsolute
: (UriKind
) 300;
1882 public void DotNetRelativeOrAbsoluteTest ()
1884 // On windows the path /foo is parsed as BadFormat and checking
1885 // if this is relative or absolute doesn't make sense.
1889 FieldInfo useDotNetRelativeOrAbsoluteField
= null;
1890 bool useDotNetRelativeOrAbsoluteOld
= false;
1892 if (Type
.GetType ("Mono.Runtime") != null) {
1893 useDotNetRelativeOrAbsoluteField
= typeof (Uri
).GetField ("useDotNetRelativeOrAbsolute",
1894 BindingFlags
.Static
| BindingFlags
.GetField
| BindingFlags
.NonPublic
);
1895 useDotNetRelativeOrAbsoluteOld
= (bool) useDotNetRelativeOrAbsoluteField
.GetValue (null);
1896 useDotNetRelativeOrAbsoluteField
.SetValue (null, false);
1902 uri
= new Uri ("/foo", DotNetRelativeOrAbsolute
);
1903 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#2");
1905 Assert
.IsTrue (Uri
.TryCreate("/foo", DotNetRelativeOrAbsolute
, out uri
), "#3");
1906 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#3a");
1908 if (useDotNetRelativeOrAbsoluteField
!= null) {
1909 uri
= new Uri ("/foo", UriKind
.RelativeOrAbsolute
);
1910 Assert
.IsTrue (uri
.IsAbsoluteUri
, "#4");
1912 Assert
.IsTrue (Uri
.TryCreate("/foo", UriKind
.RelativeOrAbsolute
, out uri
), "#5");
1913 Assert
.IsTrue (uri
.IsAbsoluteUri
, "#5a");
1915 useDotNetRelativeOrAbsoluteField
.SetValue (null, true);
1918 uri
= new Uri ("/foo", UriKind
.RelativeOrAbsolute
);
1919 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#10");
1921 Assert
.IsTrue (Uri
.TryCreate("/foo", UriKind
.RelativeOrAbsolute
, out uri
), "#11");
1922 Assert
.IsFalse (uri
.IsAbsoluteUri
, "#11a");
1924 if (useDotNetRelativeOrAbsoluteField
!= null)
1925 useDotNetRelativeOrAbsoluteField
.SetValue (null, useDotNetRelativeOrAbsoluteOld
);
1931 public void LocalPathWithBaseUrl ()
1933 var mainUri
= new Uri ("http://www.imdb.com");
1934 var uriPath
= "/title/tt0106521";
1937 Assert
.IsTrue (Uri
.TryCreate (mainUri
, uriPath
, out result
), "#1");
1938 Assert
.AreEqual ("http://www.imdb.com/title/tt0106521", result
.ToString (), "#2");
1942 public void GetSerializationInfoStringOnRelativeUri ()
1944 var uri
= new Uri ("/relative/path", UriKind
.Relative
);
1945 var result
= uri
.GetComponents (UriComponents
.SerializationInfoString
, UriFormat
.UriEscaped
);
1947 Assert
.AreEqual (uri
.OriginalString
, result
);
1951 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1952 public void GetSerializationInfoStringException ()
1954 var uri
= new Uri ("/relative/path", UriKind
.Relative
);
1955 uri
.GetComponents (UriComponents
.SerializationInfoString
| UriComponents
.Host
, UriFormat
.UriEscaped
);
1959 public void UserInfo_EscapedLetter ()
1961 var uri
= new Uri ("https://first%61second@host");
1962 Assert
.AreEqual ("firstasecond", uri
.UserInfo
);
1966 public void UserInfo_EscapedAt ()
1968 var userinfo
= "first%40second";
1969 var uri
= new Uri ("https://" + userinfo
+ "@host");
1970 Assert
.AreEqual (userinfo
, uri
.UserInfo
);
1974 public void UserInfo_EscapedChars ()
1976 for (var ch
= (char) 1; ch
< 128; ch
++) {
1977 var userinfo
= Uri
.EscapeDataString (ch
.ToString ());
1979 new Uri (string.Format("http://{0}@localhost:80/", userinfo
));
1980 } catch (Exception e
) {
1981 Assert
.Fail (string.Format("Unexpected {0} while building URI with username {1}", e
.GetType ().Name
, userinfo
));
1987 public void UserInfo_Spaces ()
1989 const string userinfo
= "test 1:pass 1";
1990 const string expected
= "test%201:pass%201";
1993 var uri
= new Uri (string.Format ("rtmp://{0}@test.com:333/live", userinfo
));
1994 Assert
.AreEqual (expected
, uri
.UserInfo
);
1995 } catch (Exception e
) {
1996 Assert
.Fail (string.Format ("Unexpected {0} while building URI with username {1}", e
.GetType ().Name
, userinfo
));
2002 public void PathDotTrim ()
2004 var baseUri
= new Uri ("http://test.com", UriKind
.Absolute
);
2005 var relUri
= new Uri ("path/dot./", UriKind
.Relative
);
2006 var uri
= new Uri (baseUri
, relUri
);
2007 Assert
.AreEqual ("http://test.com/path/dot./", uri
.ToString ());
2011 public void GuardedIPv6Address ()
2013 var x
= new Uri ("asfd://[::1]:123/");
2014 Assert
.AreEqual ("[::1]", x
.Host
, "#1");
2018 public void CombineWithUserSchema ()
2020 var baseUri
= new Uri ("zip:mem:///");
2021 var relativeUrl
= "zip:mem:///foo/bar.txt";
2023 var result
= new Uri (baseUri
, relativeUrl
);
2025 Assert
.AreEqual ("zip:mem:///foo/bar.txt", result
.ToString ());
2029 public void Scheme_msapp ()
2031 var uri
= new Uri ("ms-app://s-1-15-2-1613647288");
2032 Assert
.AreEqual ("ms-app", uri
.Scheme
);
2036 public void CombineWithUnixAbsolutePath ()
2038 var a
= new Uri ("http://localhost/");
2039 var b
= new Uri ("/foo", UriKind
.RelativeOrAbsolute
);
2040 var res
= new Uri (a
, b
);
2042 Assert
.AreEqual ("http://localhost/foo", res
.ToString ());
2046 public void ImplicitUnixFileWithUnicode ()
2052 Assert
.IsTrue (Uri
.TryCreate ("/Library/Frameworks/System.Runtim…ee", UriKind
.Absolute
, out uri
), "#1");
2053 Assert
.IsTrue (Uri
.TryCreate (" /A/…", UriKind
.Absolute
, out uri
), "#2");
2057 public void UncValidPath ()
2059 var uri
= new Uri ("https://_foo/bar.html");
2060 Assert
.AreEqual ("https", uri
.Scheme
);
2064 public void ImplicitUnixFileWithUnicodeGetAbsoluleUri ()
2069 string escFilePath
= "/Users/Текст.txt";
2070 string escUrl
= new Uri (escFilePath
, UriKind
.Absolute
).AbsoluteUri
;
2071 Assert
.AreEqual ("file:///Users/%D0%A2%D0%B5%D0%BA%D1%81%D1%82.txt", escUrl
);