4e5452ad8fd2274056985dc4826a53cc1746724e
[mono.git] / mcs / class / System / Test / System / UriTest2.cs
1 using System;\r
2 using System.IO;
3 using System.Reflection;\r
4 using System.Text;\r
5 using NUnit.Framework;\r
6 \r
7 namespace MonoTests.System\r
8 {\r
9         [TestFixture]\r
10         public class UriTest2 : Assertion\r
11         {\r
12                 // Segments cannot be validated here...\r
13                 public void AssertUri (string relsrc, Uri uri,\r
14                         string toString,\r
15                         string absoluteUri,\r
16                         string scheme,\r
17                         string host,\r
18                         string localPath,\r
19                         string query,\r
20                         int port,\r
21                         bool isFile,\r
22                         bool isUnc,\r
23                         bool isLoopback,\r
24                         bool userEscaped,\r
25                         UriHostNameType hostNameType,\r
26                         string absolutePath,\r
27                         string pathAndQuery,\r
28                         string authority,\r
29                         string fragment,\r
30                         string userInfo)\r
31                 {\r
32                         AssertEquals (relsrc + " AbsoluteUri", absoluteUri, uri.AbsoluteUri);\r
33                         AssertEquals (relsrc + " Scheme", scheme, uri.Scheme);\r
34                         AssertEquals (relsrc + " Host", host, uri.Host);\r
35                         AssertEquals (relsrc + " Port", port, uri.Port);\r
36                         // Windows UNC path is not automatically testable on *nix environment,\r
37                         if (relsrc.StartsWith ("\\\\") && Path.DirectorySeparatorChar == '\\')\r
38                                 AssertEquals (relsrc + " LocalPath", localPath, uri.LocalPath);\r
39                         AssertEquals (relsrc + " Query", query, uri.Query);\r
40                         AssertEquals (relsrc + " Fragment", fragment, uri.Fragment);\r
41                         AssertEquals (relsrc + " IsFile", isFile, uri.IsFile);\r
42                         AssertEquals (relsrc + " IsUnc", isUnc, uri.IsUnc);\r
43                         AssertEquals (relsrc + " IsLoopback", isLoopback, uri.IsLoopback);\r
44                         AssertEquals (relsrc + " Authority", authority, uri.Authority);\r
45                         AssertEquals (relsrc + " UserEscaped", userEscaped, uri.UserEscaped);\r
46                         AssertEquals (relsrc + " UserInfo", userInfo, uri.UserInfo);\r
47                         AssertEquals (relsrc + " HostNameType", hostNameType, uri.HostNameType);\r
48                         AssertEquals (relsrc + " AbsolutePath", absolutePath, uri.AbsolutePath);\r
49                         AssertEquals (relsrc + " PathAndQuery", pathAndQuery, uri.PathAndQuery);\r
50                         AssertEquals (relsrc + " ToString()", toString, uri.ToString ());\r
51                 }\r
52 \r
53                 [Test]\r
54                 public void AbsoluteUriFromFile ()\r
55                 {\r
56                         FromResource ("test-uri-props.txt", null);\r
57                 }\r
58                 \r
59                 [Test]\r
60                 [Category("NotDotNet")]\r
61                 public void AbsoluteUriFromFileManual ()\r
62                 {\r
63                         FromResource ("test-uri-props-manual.txt", null);\r
64                 }\r
65                 \r
66                 [Test]\r
67                 public void RelativeUriFromFile ()\r
68                 {\r
69                         FromResource ("test-uri-relative-props.txt", new Uri ("http://www.go-mono.com"));\r
70                 }\r
71                 \r
72                 private void FromResource (string res, Uri baseUri)\r
73                 {
74                         Assembly a = Assembly.GetExecutingAssembly ();
75                         Stream s = a.GetManifestResourceStream (res);\r
76                         StreamReader sr = new StreamReader (s, Encoding.UTF8);\r
77                         while (sr.Peek () > 0) {\r
78                                 sr.ReadLine (); // skip\r
79                                 string uriString = sr.ReadLine ();\r
80 /*\r
81 TextWriter sw = Console.Out;\r
82                                 sw.WriteLine ("-------------------------");\r
83                                 sw.WriteLine (uriString);\r
84 */\r
85                                 if (uriString == null || uriString.Length == 0)\r
86                                         break;\r
87 \r
88                                 try {\r
89                                         Uri uri = baseUri == null ? new Uri (uriString) : new Uri (baseUri, uriString);\r
90 /*\r
91                                 sw.WriteLine ("ToString(): " + uri.ToString ());\r
92                                 sw.WriteLine (uri.AbsoluteUri);\r
93                                 sw.WriteLine (uri.Scheme);\r
94                                 sw.WriteLine (uri.Host);\r
95                                 sw.WriteLine (uri.LocalPath);\r
96                                 sw.WriteLine (uri.Query);\r
97                                 sw.WriteLine ("Port: " + uri.Port);\r
98                                 sw.WriteLine (uri.IsFile);\r
99                                 sw.WriteLine (uri.IsUnc);\r
100                                 sw.WriteLine (uri.IsLoopback);\r
101                                 sw.WriteLine (uri.UserEscaped);\r
102                                 sw.WriteLine ("HostNameType: " + uri.HostNameType);\r
103                                 sw.WriteLine (uri.AbsolutePath);\r
104                                 sw.WriteLine ("PathAndQuery: " + uri.PathAndQuery);\r
105                                 sw.WriteLine (uri.Authority);\r
106                                 sw.WriteLine (uri.Fragment);\r
107                                 sw.WriteLine (uri.UserInfo);\r
108 */\r
109                                         AssertUri (uriString, uri,\r
110                                                 sr.ReadLine (),\r
111                                                 sr.ReadLine (),\r
112                                                 sr.ReadLine (),\r
113                                                 sr.ReadLine (),\r
114                                                 sr.ReadLine (),\r
115                                                 sr.ReadLine (),\r
116                                                 int.Parse (sr.ReadLine ()),\r
117                                                 bool.Parse (sr.ReadLine ()),\r
118                                                 bool.Parse (sr.ReadLine ()),\r
119                                                 bool.Parse (sr.ReadLine ()),\r
120                                                 bool.Parse (sr.ReadLine ()),\r
121                                                 (UriHostNameType) Enum.Parse (typeof (UriHostNameType), sr.ReadLine (), false),\r
122                                                 sr.ReadLine (),\r
123                                                 sr.ReadLine (),\r
124                                                 sr.ReadLine (),\r
125                                                 sr.ReadLine (),\r
126                                                 sr.ReadLine ());\r
127 //                              Console.WriteLine ("Passed: " + uriString);\r
128                                 } catch (UriFormatException ex) {\r
129                                         Fail (String.Format ("Construction failed: [{0}] {1}", uriString, ex.Message));\r
130                                 }\r
131                         }\r
132                 }\r
133
134                 [Test]
135                 public void MoreUnescape () // bug 733316
136                 {
137                         int index = 0;
138                         char unesc = Uri.HexUnescape ("%F6", ref index);
139                         AssertEquals ("#01", 3, index);
140                         AssertEquals ("#02", 0xf6, unesc);
141                 }
142 \r
143         }\r
144 }\r