[build] Prevent cyclic targets from being built in parallel
[mono.git] / mcs / class / System / Test / System / UriBuilderTest.cs
1 //
2 // UriBuilderTest.cs - NUnit Test Cases for System.UriBuilder
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
10 //
11
12 using NUnit.Framework;
13 using System;
14
15 namespace MonoTests.System
16 {
17         [TestFixture]
18         public class UriBuilderTest
19         {
20                 private UriBuilder b, b2, b3;
21                 
22                 [SetUp]
23                 public void GetReady()
24                 {
25                         b = new UriBuilder ("http", "www.ximian.com", 80, "foo/bar/index.html");
26                 }
27
28                 [Test] // ctor ()
29                 public void Constructor_Empty ()
30                 {
31                         b = new UriBuilder ();
32                         Assert.AreEqual ("http", b.Scheme, "#1");
33                         Assert.AreEqual ("localhost", b.Host, "#2");
34                         Assert.AreEqual (-1, b.Port, "#4");
35                         Assert.AreEqual (string.Empty, b.Query, "#5");
36                         Assert.AreEqual (string.Empty, b.Fragment, "#6");
37                 }
38
39                 [Test] // ctor (string)
40                 public void Constructor1 ()
41                 {
42                         b = new UriBuilder ("http://www.ximian.com:8001#test?name=50");
43                         Assert.AreEqual ("#test?name=50", b.Fragment, "#A1");
44                         Assert.AreEqual ("www.ximian.com", b.Host, "#A2");
45                         Assert.AreEqual (string.Empty, b.Password, "#A3");
46                         Assert.AreEqual ("/", b.Path, "#A4");
47                         Assert.AreEqual (8001, b.Port, "#A5");
48                         Assert.AreEqual (string.Empty, b.Query, "#A5");
49                         Assert.AreEqual ("http", b.Scheme, "#A6");
50                         Assert.AreEqual ("http://www.ximian.com:8001/#test?name=50", b.Uri.ToString (), "#A7");
51                         Assert.AreEqual (string.Empty, b.UserName, "#A8");
52
53                         b = new UriBuilder ("http://www.ximian.com?name=50#test");
54                         Assert.AreEqual ("#test", b.Fragment, "#B1");
55                         Assert.AreEqual ("www.ximian.com", b.Host, "#B2");
56                         Assert.AreEqual (string.Empty, b.Password, "#B3");
57                         Assert.AreEqual ("/", b.Path, "#B4");
58                         Assert.AreEqual (80, b.Port, "#B5");
59                         Assert.AreEqual ("?name=50", b.Query, "#B5");
60                         Assert.AreEqual ("http", b.Scheme, "#B6");
61                         Assert.AreEqual ("http://www.ximian.com/?name=50#test", b.Uri.ToString (), "#B7");
62                         Assert.AreEqual (string.Empty, b.UserName, "#B8");
63                 }
64
65                 [Test] // ctor (string)
66                 public void Constructor1_Uri_Null ()
67                 {
68                         try {
69                                 new UriBuilder ((string) null);
70                                 Assert.Fail ("#1");
71                         } catch (ArgumentNullException ex) {
72                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
73                                 Assert.IsNull (ex.InnerException, "#3");
74                                 Assert.IsNotNull (ex.Message, "#4");
75                                 Assert.IsNotNull (ex.ParamName, "#5");
76                                 Assert.AreEqual ("uriString", ex.ParamName, "#6");
77                         }
78                 }
79
80                 [Test] // ctor (Uri)
81 #if NET_4_0
82                 [ExpectedException (typeof (ArgumentNullException))]
83 #else
84                 [ExpectedException (typeof (NullReferenceException))]
85 #endif
86                 public void Constructor2_Uri_Null ()
87                 {
88                         new UriBuilder ((Uri) null);
89                 }
90
91                 [Test]
92                 public void Constructor_5 ()
93                 {
94                         b = new UriBuilder ("http", "www.ximian.com", 80, "foo/bar/index.html", "#extras");
95                 }
96
97                 [Test]
98                 [ExpectedException (typeof (ArgumentException))]
99                 public void Constructor_5_BadExtraValue ()
100                 {
101                         b = new UriBuilder ("http", "www.ximian.com", 80, "foo/bar/index.html", "extras");
102                         // should have thrown an ArgumentException because extraValue must start with '?' or '#' character.
103                 }
104                 
105                 [Test]
106                 public void Constructor_StringStringInt ()
107                 {
108                         UriBuilder ub = new UriBuilder ("http", "www.mono-project.com", 80);
109                         Assert.AreEqual ("http://www.mono-project.com/", ub.Uri.AbsoluteUri, "Uri.AbsoluteUri");
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (InvalidOperationException))]
114                 public void Constructor_RelativeUri ()
115                 {
116                         Uri relative = new Uri ("../dir/subdir/file", UriKind.RelativeOrAbsolute);
117                         UriBuilder ub = new UriBuilder (relative);
118                 }
119
120                 [Test]
121                 public void UserInfo ()
122                 {
123                         string s = "mailto://myname:mypwd@contoso.com?subject=hello";
124                         b = new UriBuilder (s);
125                         Assert.AreEqual (s, b.ToString (), "1.ToString");
126                         Assert.AreEqual (string.Empty, b.UserName, "1.UserName");
127                         Assert.AreEqual (string.Empty, b.Password, "1.Password");
128                         Assert.AreEqual ("//myname:mypwd@contoso.com", b.Uri.LocalPath, "1.Uri.LocalPath");
129
130                         // weird ?caching? issue, UserInfo is not updated if we look at the value of UserName before setting Password
131                         b = new UriBuilder ("mailto", "contoso.com");
132                         b.UserName = "myname";
133                         Assert.AreEqual ("myname", b.Uri.UserInfo, "2.UserName");
134                         b.Password = "mypwd";
135                         Assert.AreEqual ("myname", b.Uri.UserInfo, "2.Password");
136                         Assert.AreEqual ("/", b.Uri.LocalPath, "2.Uri.LocalPath");
137
138                         b = new UriBuilder ("mailto", "contoso.com");
139                         b.UserName = "myname";
140                         b.Password = "mypwd";
141                         Assert.AreEqual ("myname:mypwd", b.Uri.UserInfo, "3.Uri.UserInfo");
142                         Assert.AreEqual ("/", b.Uri.LocalPath, "3.Uri.LocalPath");
143                 }
144
145                 [Test]
146                 public void Path ()
147                 {
148                         b.Path = ((char) 0xa9) + " 2002";
149                         Assert.AreEqual ("%C2%A9%202002", b.Path);
150                 }
151                 
152                 [Test]
153                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
154                 public void BadPort1 ()
155                 {
156                         b.Port = -12345;
157                 }
158
159                 [Test]
160                 public void DefaultPort ()
161                 {
162                         b.Port = -1;
163                         Assert.AreEqual (-1, b.Port, "#1");
164                         Assert.AreEqual ("http://www.ximian.com/foo/bar/index.html", b.ToString (), "#2");
165                 }
166
167                 [Test]
168                 public void Query ()
169                 {
170                         b.Query = ((char) 0xa9) + " 2002";
171                         Assert.AreEqual ("?\xA9 2002", b.Query, "#1");
172                         Assert.AreEqual (string.Empty, b.Fragment, "#2");
173                         b.Query = "?test";
174                         Assert.AreEqual ("??test", b.Query, "#3");
175                         b.Query = null;
176                         Assert.AreEqual (string.Empty, b.Query, "#4");
177                         b.Fragment = "test";
178                         Assert.AreEqual ("#test", b.Fragment, "#5");
179                         b.Query = "name";
180                         Assert.AreEqual ("#test", b.Fragment, "#6");
181                         Assert.AreEqual ("?name", b.Query, "#7");
182                         b.Fragment = "run";
183                         Assert.AreEqual ("#run", b.Fragment, "#8");
184                         b.Query = null;
185                         Assert.AreEqual ("#run", b.Fragment, "#9");
186                         Assert.AreEqual (string.Empty, b.Query, "#10");
187                 }
188                 
189                 [Test]
190                 public void Fragment ()
191                 {
192                         b.Fragment = ((char) 0xa9) + " 2002";
193                         Assert.AreEqual ("#\xA9 2002", b.Fragment, "#1");
194                         Assert.AreEqual (string.Empty, b.Query, "#2");
195                         b.Fragment = "#test";
196                         Assert.AreEqual ("##test", b.Fragment, "#3");
197                         b.Fragment = null;
198                         Assert.AreEqual (String.Empty, b.Fragment, "#4");
199                         b.Query = "name";
200                         Assert.AreEqual ("?name", b.Query, "#5");
201                         b.Fragment = null;
202                         Assert.AreEqual ("?name", b.Query, "#6");
203                         Assert.AreEqual (string.Empty, b.Fragment, "#7");
204                 }
205                 
206                 [Test]
207                 public void Scheme ()
208                 {
209                         b.Scheme = "http";
210                         Assert.AreEqual ("http", b.Scheme, "#1");
211                         b.Scheme = "http:";
212                         Assert.AreEqual ("http", b.Scheme, "#2");
213                         b.Scheme = "http://";
214                         Assert.AreEqual ("http", b.Scheme, "#3");
215                         b.Scheme = "http://foo/bar";
216                         Assert.AreEqual ("http", b.Scheme, "#4");
217                         b.Scheme = "mailto:";
218                         Assert.AreEqual ("mailto", b.Scheme, "#5");
219                         b.Scheme = "unknown";
220                         Assert.AreEqual ("unknown", b.Scheme, "#6");
221                         b.Scheme = "unknown://";
222                         Assert.AreEqual ("unknown", b.Scheme, "#7");
223                 }
224                 
225                 [Test]
226                 public void Equals ()
227                 {
228                         b = new UriBuilder ("http://", "www.ximian.com", 80, "foo/bar/index.html?item=1");
229                         Assert.AreEqual ("foo/bar/index.html%3Fitem=1", b.Path, "1.Path");
230                         Assert.AreEqual ("http://www.ximian.com:80/foo/bar/index.html%3Fitem=1", b.ToString (), "1.ToString");
231
232                         b2 = new UriBuilder ("http", "www.ximian.com", 80, "/foo/bar/index.html", "?item=1");
233                         Assert.AreEqual ("http://www.ximian.com:80/foo/bar/index.html?item=1", b2.ToString (), "2.ToString");
234
235                         b3 = new UriBuilder (new Uri ("http://www.ximian.com/foo/bar/index.html?item=1"));
236                         Assert.AreEqual ("http://www.ximian.com:80/foo/bar/index.html?item=1", b3.ToString (), "3.ToString");
237
238                         Assert.IsFalse (b.Equals (b2), "#1");
239                         Assert.IsFalse (b.Uri.Equals (b2.Uri), "#2");
240                         Assert.IsFalse (b.Equals (b3), "#3");
241                         Assert.IsFalse (b3.Equals (b), "#4");
242                         Assert.IsTrue (b2.Equals (b3), "#5");
243                 }
244                 
245                 [Test]
246                 public void ToStringTest ()
247                 {
248                         Assert.AreEqual ("http://www.ximian.com:80/foo/bar/index.html", b.ToString (), "#1");
249                         Assert.AreEqual ("http://www.ximian.com/foo/bar/index.html", b.Uri.ToString (), "#2");
250                 }
251
252                 [Test]
253                 public void EmptyQuery () // bug 57082
254                 {
255                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", null);
256                         string noquery = "http://www.ximian.com/lalala/lelele.aspx";
257                         Assert.AreEqual (noquery, b.Uri.ToString (), "#1");
258                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", "?");
259                         Assert.AreEqual (noquery, b.Uri.ToString (), "#2");
260                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", "??");
261                         Assert.AreEqual (noquery + "??", b.Uri.ToString (), "#3");
262                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", "?something");
263                         Assert.AreEqual (noquery + "?something", b.Uri.ToString (), "#4");
264                 }
265
266                 [Test] // bug #76501
267                 public void TestToString76501 ()
268                 {
269                         UriBuilder ub = new UriBuilder (
270                                 "http://mondomaine/trucmuche/login.aspx");
271                         ub.Query = ub.Query.TrimStart (new char [] {'?'}) + "&ticket=bla";
272                         Assert.IsTrue (ub.ToString ().IndexOf ("80//") < 0);
273                 }
274
275                 [Test]
276                 public void TestAppendFragment ()
277                 {
278                         UriBuilder uri = new UriBuilder ("http://www.mono-project.com/Main_Page");
279                         uri.Fragment = "Features";
280                         Assert.AreEqual ("#Features", uri.Fragment, "#1");
281                         Assert.AreEqual ("http://www.mono-project.com/Main_Page#Features", uri.Uri.ToString (), "#2");
282                 }
283
284                 [Test]
285                 public void IPv6_Host ()
286                 {
287                         UriBuilder ub = new UriBuilder ("http", "[1:2:3:4:5:6:7:8]", 8080, "/dir/subdir/file");
288                         Assert.AreEqual ("[1:2:3:4:5:6:7:8]", ub.Host, "Host.1");
289                         Assert.AreEqual ("[0001:0002:0003:0004:0005:0006:0007:0008]", ub.Uri.Host, "Uri.Host");
290                         // once the Uri is created then some builder properties may change
291                         Assert.AreEqual ("[0001:0002:0003:0004:0005:0006:0007:0008]", ub.Host, "Host.2");
292                 }
293
294                 [Test]
295                 public void IPv6_Host_IncompleteAddress ()
296                 {
297                         UriBuilder ub = new UriBuilder ("http", "1:2:3:4:5:6:7:8", 8080, "/dir/subdir/file");
298                         Assert.AreEqual ("[1:2:3:4:5:6:7:8]", ub.Host, "1.Host");
299                         Assert.AreEqual ("http://[1:2:3:4:5:6:7:8]:8080/dir/subdir/file", ub.ToString (), "1.ToString ()");
300
301                         ub = new UriBuilder ("http", "1:", 8080, "/dir/subdir/file");
302                         Assert.AreEqual ("[1:]", ub.Host, "2.Host");
303                         Assert.AreEqual ("http://[1:]:8080/dir/subdir/file", ub.ToString (), "2.ToString ()");
304
305                         ub = new UriBuilder ("http", "[1:", 8080, "/dir/subdir/file");
306                         Assert.AreEqual ("[1:", ub.Host, "3.Host");
307                         Assert.AreEqual ("http://[1::8080/dir/subdir/file", ub.ToString (), "3.ToString ()");
308
309                         ub = new UriBuilder ("http", "1:2]", 8080, "/dir/subdir/file");
310                         Assert.AreEqual ("[1:2]]", ub.Host, "4.Host");
311                         Assert.AreEqual ("http://[1:2]]:8080/dir/subdir/file", ub.ToString (), "4.ToString ()");
312                 }
313
314                 [Test]
315                 public void Path_UriAbsolutePath_Path ()
316                 {
317                         UriBuilder ub = new UriBuilder ("http", "127.0.0.1", 80, "dir/subdir/file");
318                         Assert.AreEqual ("dir/subdir/file", ub.Path, "Path.1");
319                         Assert.AreEqual ("/dir/subdir/file", ub.Uri.AbsolutePath, "Uri.AbsolutePath");
320                         // once the Uri is created then some builder properties may change
321                         Assert.AreEqual ("/dir/subdir/file", ub.Path, "Path.2");
322                 }
323
324                 [Test]
325                 public void UnparsableUri ()
326                 {
327                         // some URI can't be parsed by System.Uri but are accepted by UriBuilder
328                         Uri u = null;
329                         string uri = "www.mono-project.com";
330                         Assert.IsFalse (Uri.TryCreate (uri, UriKind.Absolute, out u), "1.Uri.TryCreate");
331                         UriBuilder ub = new UriBuilder (uri);
332                         Assert.AreEqual ("www.mono-project.com", ub.Host, "1.Host");
333                         Assert.AreEqual ("http", ub.Scheme, "1.Scheme");
334                         Assert.AreEqual (80, ub.Port, "1.Port");
335                         Assert.AreEqual ("/", ub.Path, "1.Path");
336
337                         // always assume http, port 80
338                         uri = "ftp.novell.com/dir/subdir/file";
339                         ub = new UriBuilder (uri);
340                         Assert.IsFalse (Uri.TryCreate (uri, UriKind.Absolute, out u), "2.Uri.TryCreate");
341                         Assert.AreEqual ("ftp.novell.com", ub.Host, "2.Host");
342                         Assert.AreEqual ("http", ub.Scheme, "2.Scheme");
343                         Assert.AreEqual (80, ub.Port, "2.Port");
344                         Assert.AreEqual ("/dir/subdir/file", ub.Path, "2.Path");
345                 }
346
347                 [Test]
348                 public void AspNetRedirectUsage_Old ()
349                 {
350                         Uri uri = new Uri ("http://192.168.0.21:80/WebResource.axd?d=AAAAAAAAAAEAAAAAAAAAAA2");
351                         UriBuilder ub = new UriBuilder (uri);
352                         ub.Path = "error404.aspx?aspxerrorpath=/WebResource.axd";
353                         ub.Fragment = null;
354                         ub.Password = null;
355                         ub.Query = null;
356                         ub.UserName = null;
357                         // a bug in older UriBuilder did not encode the ? - existing ASP.NET depends on buggy behavior
358                         Assert.AreEqual ("http://192.168.0.21/error404.aspx%3Faspxerrorpath=/WebResource.axd", ub.Uri.ToString ());
359                 }
360
361                 [Test]
362                 public void AspNetRedirectUsage_New ()
363                 {
364                         string path = "error404.aspx?aspxerrorpath=/WebResource.axd";
365                         Uri uri = new Uri ("http://192.168.0.21:80/WebResource.axd?d=AAAAAAAAAAEAAAAAAAAAAA2");
366                         UriBuilder ub = new UriBuilder (uri);
367                         int qpos = path.IndexOf ('?');
368                         ub.Path = path.Substring (0, qpos);
369                         ub.Fragment = null;
370                         ub.Password = null;
371                         ub.Query = path.Substring (qpos + 1);
372                         ub.UserName = null;
373                         // this is what ASP.NET really means (the ?)
374                         Assert.AreEqual ("http://192.168.0.21/error404.aspx?aspxerrorpath=/WebResource.axd", ub.Uri.ToString ());
375                 }
376         }
377 }
378