New test.
[mono.git] / mcs / class / System / Test / System.Net / WebProxyTest.cs
1 //\r
2 // WebProxyTest.cs - NUnit Test Cases for System.Net.WebProxy\r
3 //\r
4 // Authors:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)\r
7 //\r
8 // (C) 2003 Martin Willemoes Hansen\r
9 //\r
10 \r
11 using NUnit.Framework;\r
12 using System;\r
13 using System.Collections;\r
14 using System.IO;\r
15 using System.Net;\r
16 using System.Threading;\r
17 \r
18 namespace MonoTests.System.Net\r
19 {\r
20 \r
21 [TestFixture]\r
22 public class WebProxyTest\r
23 {\r
24         private Uri googleUri;\r
25         private Uri yahooUri;\r
26         private Uri apacheUri;\r
27         \r
28         [SetUp]\r
29         public void GetReady () {\r
30                 googleUri = new Uri ("http://www.google.com");\r
31                 yahooUri = new Uri ("http://www.yahoo.com");\r
32                 apacheUri = new Uri ("http://www.apache.org");\r
33         }\r
34 \r
35         [Test]\r
36         public void Constructors ()\r
37         {\r
38                 WebProxy p = new WebProxy ();\r
39                 Assertion.Assert("#1", p.Address == null);\r
40                 Assertion.AssertEquals ("#2", 0, p.BypassArrayList.Count);\r
41                 Assertion.AssertEquals ("#3", 0, p.BypassList.Length);\r
42                 Assertion.AssertEquals ("#4", false, p.BypassProxyOnLocal);\r
43                 try {\r
44                         p.BypassList = null;\r
45                         Assertion.Fail ("#5 not spec'd, but should follow ms.net implementation");\r
46                 } catch (ArgumentNullException) {}\r
47 \r
48                 p = new WebProxy ("webserver.com", 8080);\r
49                 Assertion.AssertEquals ("#6", new Uri ("http://webserver.com:8080/"), p.Address);\r
50                 \r
51                 p = new WebProxy ("webserver");\r
52                 Assertion.AssertEquals ("#7", new Uri ("http://webserver"), p.Address);\r
53 \r
54                 p = new WebProxy ("webserver.com");\r
55                 Assertion.AssertEquals ("#8", new Uri ("http://webserver.com"), p.Address);\r
56 \r
57                 p = new WebProxy ("http://webserver.com");\r
58                 Assertion.AssertEquals ("#9", new Uri ("http://webserver.com"), p.Address);\r
59 \r
60                 p = new WebProxy ("file://webserver");\r
61                 Assertion.AssertEquals ("#10", new Uri ("file://webserver"), p.Address);                \r
62                 \r
63                 p = new WebProxy ("http://www.contoso.com", true, null, null);\r
64                 Assertion.AssertEquals ("#11", 0, p.BypassList.Length);\r
65                 Assertion.AssertEquals ("#12", 0, p.BypassArrayList.Count);\r
66                 \r
67                 try {\r
68                         p = new WebProxy ("http://contoso.com", true, \r
69                                 new string [] {"?^!@#$%^&}{]["}, null);\r
70                         Assertion.Fail ("#13: illegal regular expression");\r
71                 } catch (ArgumentException) {\r
72                 }\r
73         }\r
74         \r
75         [Test]\r
76         public void BypassArrayList ()\r
77         {\r
78                 Uri proxy1 = new Uri("http://proxy.contoso.com");\r
79                 Uri proxy2 = new Uri ("http://proxy2.contoso.com");\r
80                 \r
81                 WebProxy p = new WebProxy (proxy1, true);\r
82                 p.BypassArrayList.Add ("http://proxy2.contoso.com");\r
83                 p.BypassArrayList.Add ("http://proxy2.contoso.com");            \r
84                 Assertion.AssertEquals ("#1", 2, p.BypassList.Length);\r
85                 Assertion.Assert ("#2", !p.IsBypassed (new Uri ("http://www.google.com")));\r
86                 Assertion.Assert ("#3", p.IsBypassed (proxy2));\r
87                 Assertion.AssertEquals ("#4", proxy2, p.GetProxy (proxy2));\r
88 \r
89                 p.BypassArrayList.Add ("?^!@#$%^&}{][");\r
90                 Assertion.AssertEquals ("#10", 3, p.BypassList.Length);\r
91                 try {\r
92                         Assertion.Assert ("#11", !p.IsBypassed (proxy2));\r
93                         Assertion.Assert ("#12", !p.IsBypassed (new Uri ("http://www.x.com")));         \r
94                         Assertion.AssertEquals ("#13", proxy1, p.GetProxy (proxy2));\r
95                         // hmm... although #11 and #13 succeeded before (#3 resp. #4), \r
96                         // it now fails to bypass, and the IsByPassed and GetProxy \r
97                         // methods do not fail.. so when an illegal regular \r
98                         // expression is added through this property it's ignored. \r
99                         // probably an ms.net bug?? :(\r
100                 } catch (ArgumentException) {\r
101                         Assertion.Fail ("#15: illegal regular expression");\r
102                 }               \r
103         }\r
104         \r
105         [Test]\r
106         public void BypassList ()\r
107         {\r
108                 Uri proxy1 = new Uri("http://proxy.contoso.com");\r
109                 Uri proxy2 = new Uri ("http://proxy2.contoso.com");\r
110                 \r
111                 WebProxy p = new WebProxy (proxy1, true);\r
112                 try {\r
113                         p.BypassList = new string [] {"http://proxy2.contoso.com", "?^!@#$%^&}{]["};            \r
114                         Assertion.Fail ("#1");\r
115                 } catch (ArgumentException) {\r
116                         // weird, this way invalid regex's fail again..\r
117                 }\r
118                 \r
119                 Assertion.AssertEquals ("#2", 2, p.BypassList.Length);\r
120                 // but it did apparenly store the regex's !\r
121 \r
122                 p.BypassList = new string [] {"http://www.x.com"};              \r
123                 Assertion.AssertEquals ("#3", 1, p.BypassList.Length);\r
124 \r
125                 try {\r
126                         p.BypassList = null;\r
127                         Assertion.Fail ("#4");\r
128                 } catch (ArgumentNullException) {}\r
129                 \r
130                 Assertion.AssertEquals ("#4", 1, p.BypassList.Length);          \r
131         }\r
132         \r
133         [Test]\r
134         public void GetProxy ()\r
135         {\r
136         }       \r
137         \r
138         [Test]\r
139         public void IsByPassed ()\r
140         {\r
141                 WebProxy p = new WebProxy ("http://proxy.contoso.com", true);\r
142                 Assertion.Assert ("#1", !p.IsBypassed (new Uri ("http://www.google.com")));\r
143                 Assertion.Assert ("#2", p.IsBypassed (new Uri ("http://localhost/index.html")));\r
144                 Assertion.Assert ("#3", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));\r
145                 Assertion.Assert ("#4", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));\r
146                 Assertion.Assert ("#5", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));\r
147                 Assertion.Assert ("#6", p.IsBypassed (new Uri ("http://webserver/index.html")));\r
148                 Assertion.Assert ("#7", !p.IsBypassed (new Uri ("http://webserver.com/index.html")));\r
149                 try {\r
150                         p.IsBypassed (null);\r
151                         Assertion.Fail ("#8 not spec'd, but should follow ms.net implementation");\r
152                 } catch (NullReferenceException) {}\r
153                 \r
154                 p = new WebProxy ("http://proxy.contoso.com", false);\r
155                 Assertion.Assert ("#11", !p.IsBypassed (new Uri ("http://www.google.com")));\r
156                 Assertion.Assert ("#12: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost/index.html")));\r
157                 Assertion.Assert ("#13: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));\r
158                 Assertion.Assert ("#14: lamespec of ms.net", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));\r
159                 Assertion.Assert ("#15: lamespec of ms.net", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));\r
160                 Assertion.Assert ("#16", !p.IsBypassed (new Uri ("http://webserver/index.html")));\r
161                 \r
162                 p.BypassList = new string [] { "google.com", "contoso.com" };\r
163                 Assertion.Assert ("#20", p.IsBypassed (new Uri ("http://www.google.com")));\r
164                 Assertion.Assert ("#21", p.IsBypassed (new Uri ("http://www.GOOGLE.com")));\r
165                 Assertion.Assert ("#22", p.IsBypassed (new Uri ("http://www.contoso.com:8080/foo/bar/index.html")));\r
166                 Assertion.Assert ("#23", !p.IsBypassed (new Uri ("http://www.contoso2.com:8080/foo/bar/index.html")));\r
167                 Assertion.Assert ("#24", !p.IsBypassed (new Uri ("http://www.foo.com:8080/contoso.com.html")));\r
168                 \r
169                 p.BypassList = new string [] { "https" };               \r
170                 Assertion.Assert ("#30", !p.IsBypassed (new Uri ("http://www.google.com")));\r
171                 Assertion.Assert ("#31", p.IsBypassed (new Uri ("https://www.google.com")));\r
172         }\r
173 }\r
174 \r
175 }\r
176 \r