Converted ChangeLogs to UTF-8 and recovered incorrectly encoded characters.
[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 //
10
11 using NUnit.Framework;
12 using System;
13
14 namespace MonoTests.System
15 {
16         [TestFixture]
17         public class UriBuilderTest : Assertion
18         {
19                 private UriBuilder b, b2, b3;
20                 
21                 [SetUp]
22                 public void GetReady()
23                 {
24                         b = new UriBuilder ("http", "www.ximian.com", 80, "foo/bar/index.html");
25                 }
26
27                 [Test]
28                 public void Constructor_Empty ()
29                 {
30                         b = new UriBuilder ();
31                         AssertEquals ("#1", "http", b.Scheme);
32 #if NET_2_0
33                         AssertEquals ("#2", "localhost", b.Host);
34 #else
35                         AssertEquals ("#2", "loopback", b.Host);
36 #endif
37                         AssertEquals ("#3", -1, b.Port);
38                 }
39
40                 [Test]
41                 public void Constructor_5 ()
42                 {
43                         b = new UriBuilder ("http", "www.ximian.com", 80, "foo/bar/index.html", "#extras");
44                 }
45
46                 [Test]
47                 [ExpectedException (typeof (ArgumentException))]
48                 public void Constructor_5_BadExtraValue ()
49                 {
50                         b = new UriBuilder ("http", "www.ximian.com", 80, "foo/bar/index.html", "extras");
51                         // should have thrown an ArgumentException because extraValue must start with '?' or '#' character.
52                 }
53                 
54                 [Test]
55                 // This test does not make sense, will fix soon
56                 [Category ("NotWorking")] // bug #75144
57                 public void UserInfo ()
58                 {                       
59                         b = new UriBuilder ("mailto://myname:mypwd@contoso.com?subject=hello");
60 #if NET_2_0
61                         AssertEquals ("#1", String.Empty, b.UserName);
62                         AssertEquals ("#2", String.Empty, b.Password);
63 #else
64                         // NotWorking here for 1.x (bad behaviour in 1.x - may not be worth fixing)
65                         AssertEquals ("#1", "myname", b.UserName);
66                         AssertEquals ("#2", "mypwd", b.Password);
67 #endif                  
68                         b = new UriBuilder ("mailto", "contoso.com");
69                         b.UserName = "myname";
70                         b.Password = "mypwd";
71                         // NotWorking here for 2.0 - worth fixing
72                         AssertEquals ("#3", "myname:mypwd", b.Uri.UserInfo);
73                 }
74
75                 [Test]
76                 public void Path ()
77                 {                       
78                         b.Path = ((char) 0xa9) + " 2002";
79                         AssertEquals ("#1", "%C2%A9%202002", b.Path);                   
80                 }       
81                 
82                 [Test]
83                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
84                 public void BadPort1 ()
85                 {
86                         b.Port = -12345;
87                 }
88 #if NET_2_0
89                 [Test]
90                 public void DefaultPort ()
91                 {
92                         b.Port = -1;
93                         AssertEquals ("Port", -1, b.Port);
94                         AssertEquals ("ToString", "http://www.ximian.com/foo/bar/index.html", b.ToString ());
95                 }
96 #else
97                 [Test]
98                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
99                 public void BadPort3 ()
100                 {
101                         b.Port = -1;
102                 }
103 #endif
104                 [Test]
105                 public void Query ()
106                 {
107                         b.Query = ((char) 0xa9) + " 2002";
108                         AssertEquals ("#1", "?\xA9 2002", b.Query);                     
109                         AssertEquals ("#2", String.Empty, b.Fragment);
110                         b.Query = "?test";
111                         AssertEquals ("#3", "??test", b.Query);
112                         b.Query = null;
113                         AssertEquals ("#4", String.Empty, b.Query);
114                 }
115                 
116                 [Test]
117                 public void Fragment ()
118                 {
119                         b.Fragment = ((char) 0xa9) + " 2002";
120                         AssertEquals ("#1", "#\xA9 2002", b.Fragment);
121                         AssertEquals ("#2", String.Empty, b.Query);
122                         b.Fragment = "#test";
123                         AssertEquals ("#3", "##test", b.Fragment);
124                         b.Fragment = null;
125                         AssertEquals ("#4", String.Empty, b.Fragment);
126                 }
127                 
128                 [Test]
129                 public void Scheme ()
130                 {
131                         b.Scheme = "http";
132                         AssertEquals ("#1", b.Scheme, "http");
133                         b.Scheme = "http:";
134                         AssertEquals ("#2", b.Scheme, "http");
135                         b.Scheme = "http://";
136                         AssertEquals ("#3", b.Scheme, "http");
137                         b.Scheme = "http://foo/bar";
138                         AssertEquals ("#4", b.Scheme, "http");
139                         b.Scheme = "mailto:";
140                         AssertEquals ("#5", b.Scheme, "mailto");
141                         b.Scheme = "unknown";
142                         AssertEquals ("#6", b.Scheme, "unknown");
143                         b.Scheme = "unknown://";
144                         AssertEquals ("#7", b.Scheme, "unknown");
145                 }
146                 
147                 [Test]
148 #if NET_2_0
149                 [Category ("NotWorking")] // equals changed in 2.0
150 #endif
151                 public void Equals ()
152                 {
153                         b = new UriBuilder ("http://", "www.ximian.com", 80, "foo/bar/index.html?item=1");
154                         b2 = new UriBuilder ("http", "www.ximian.com", 80, "/foo/bar/index.html", "?item=1");
155                         b3 = new UriBuilder (new Uri ("http://www.ximian.com/foo/bar/index.html?item=1"));
156 #if NET_2_0
157                         Assert ("#1", !b.Equals (b2));
158                         Assert ("#2", !b.Uri.Equals (b2.Uri));
159                         Assert ("#3", !b.Equals (b3));
160                         Assert ("#5", !b3.Equals (b));
161 #else
162                         Assert ("#1", b.Equals (b2));
163                         Assert ("#2", b.Uri.Equals (b2.Uri));
164                         Assert ("#3", b.Equals (b3));
165                         Assert ("#5", b3.Equals (b));
166 #endif
167                         Assert ("#4", b2.Equals (b3));
168                 }
169                 
170                 [Test]
171                 public void ToStringTest ()
172                 {
173                         AssertEquals ("ToString ()", "http://www.ximian.com:80/foo/bar/index.html", b.ToString ());
174                         AssertEquals ("Uri.ToString ()", "http://www.ximian.com/foo/bar/index.html", b.Uri.ToString ());
175                 }
176
177                 [Test]
178                 public void EmptyQuery () // bug 57082
179                 {
180                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", null);
181                         string noquery = "http://www.ximian.com/lalala/lelele.aspx";
182                         AssertEquals ("#01", b.Uri.ToString (), noquery);
183                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", "?");
184                         AssertEquals ("#02", b.Uri.ToString (), noquery);
185                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", "??");
186                         AssertEquals ("#03", b.Uri.ToString (), noquery + "??");
187                         b = new UriBuilder ("http", "www.ximian.com", 80, "/lalala/lelele.aspx", "?something");
188                         AssertEquals ("#04", b.Uri.ToString (), noquery + "?something");
189                 }
190
191                 [Test] // bug #76501
192                 public void TestToString76501 ()
193                 {
194                         UriBuilder ub = new UriBuilder (
195                                 "http://mondomaine/trucmuche/login.aspx");
196                         ub.Query = ub.Query.TrimStart (new char [] {'?'}) + "&ticket=bla";
197                         Assert (ub.ToString ().IndexOf ("80//") < 0);
198                 }
199         }
200 }
201