Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[mono.git] / mcs / class / System / Test / System.Net / WebProxyTest.cs
1 //
2 // WebProxyTest.cs - NUnit Test Cases for System.Net.WebProxy
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 //
11
12 using System;
13 using System.Collections;
14 using System.IO;
15 using System.Net;
16 using System.Runtime.Serialization;
17 using System.Runtime.Serialization.Formatters;
18 using System.Runtime.Serialization.Formatters.Binary;
19 using System.Threading;
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System.Net
24 {
25         [TestFixture]
26         public class WebProxyTest
27         {
28                 [Test]
29                 public void Constructors ()
30                 {
31                         WebProxy p = new WebProxy ();
32                         Assert.IsTrue (p.Address == null, "#1");
33                         Assert.AreEqual (0, p.BypassArrayList.Count, "#2");
34                         Assert.AreEqual (0, p.BypassList.Length, "#3");
35                         Assert.AreEqual (false, p.BypassProxyOnLocal, "#4");
36                         try {
37                                 p.BypassList = null;
38                                 Assert.Fail ("#5 not spec'd, but should follow ms.net implementation");
39                         } catch (ArgumentNullException) { }
40
41                         p = new WebProxy ("webserver.com", 8080);
42                         Assert.AreEqual (new Uri ("http://webserver.com:8080/"), p.Address, "#6");
43
44                         p = new WebProxy ("webserver");
45                         Assert.AreEqual (new Uri ("http://webserver"), p.Address, "#7");
46
47                         p = new WebProxy ("webserver.com");
48                         Assert.AreEqual (new Uri ("http://webserver.com"), p.Address, "#8");
49
50                         p = new WebProxy ("http://webserver.com");
51                         Assert.AreEqual (new Uri ("http://webserver.com"), p.Address, "#9");
52
53                         p = new WebProxy ("file://webserver");
54                         Assert.AreEqual (new Uri ("file://webserver"), p.Address, "#10");
55
56                         p = new WebProxy ("http://www.contoso.com", true, null, null);
57                         Assert.AreEqual (0, p.BypassList.Length, "#11");
58                         Assert.AreEqual (0, p.BypassArrayList.Count, "#12");
59
60                         try {
61                                 p = new WebProxy ("http://contoso.com", true,
62                                         new string [] { "?^!@#$%^&}{][" }, null);
63                                 Assert.Fail ("#13: illegal regular expression");
64                         } catch (ArgumentException) {
65                         }
66                 }
67
68                 [Test]
69                 public void BypassArrayList ()
70                 {
71                         Uri proxy1 = new Uri ("http://proxy.contoso.com");
72                         Uri proxy2 = new Uri ("http://proxy2.contoso.com");
73
74                         WebProxy p = new WebProxy (proxy1, true);
75                         p.BypassArrayList.Add ("http://proxy2.contoso.com");
76                         p.BypassArrayList.Add ("http://proxy2.contoso.com");
77                         Assert.AreEqual (2, p.BypassList.Length, "#1");
78                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#2");
79                         Assert.IsTrue (p.IsBypassed (proxy2), "#3");
80                         Assert.AreEqual (proxy2, p.GetProxy (proxy2), "#4");
81
82                         p.BypassArrayList.Add ("?^!@#$%^&}{][");
83                         Assert.AreEqual (3, p.BypassList.Length, "#10");
84                         try {
85                                 Assert.IsTrue (!p.IsBypassed (proxy2), "#11");
86                                 Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.x.com")), "#12");
87                                 Assert.AreEqual (proxy1, p.GetProxy (proxy2), "#13");
88                                 // hmm... although #11 and #13 succeeded before (#3 resp. #4), 
89                                 // it now fails to bypass, and the IsByPassed and GetProxy 
90                                 // methods do not fail.. so when an illegal regular 
91                                 // expression is added through this property it's ignored. 
92                                 // probably an ms.net bug?? :(
93                         } catch (ArgumentException) {
94                                 Assert.Fail ("#15: illegal regular expression");
95                         }
96                 }
97
98                 [Test]
99                 public void BypassList ()
100                 {
101                         Uri proxy1 = new Uri ("http://proxy.contoso.com");
102                         Uri proxy2 = new Uri ("http://proxy2.contoso.com");
103
104                         WebProxy p = new WebProxy (proxy1, true);
105                         try {
106                                 p.BypassList = new string [] { "http://proxy2.contoso.com", "?^!@#$%^&}{][" };
107                                 Assert.Fail ("#1");
108                         } catch (ArgumentException) {
109                                 // weird, this way invalid regex's fail again..
110                         }
111
112                         Assert.AreEqual (2, p.BypassList.Length, "#2");
113                         // but it did apparenly store the regex's !
114
115                         p.BypassList = new string [] { "http://www.x.com" };
116                         Assert.AreEqual (1, p.BypassList.Length, "#3");
117
118                         try {
119                                 p.BypassList = null;
120                                 Assert.Fail ("#4");
121                         } catch (ArgumentNullException) { }
122
123                         Assert.AreEqual (1, p.BypassList.Length, "#4");
124                 }
125
126                 [Test]
127                 public void IsByPassed ()
128                 {
129                         WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
130                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#1");
131                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost/index.html")), "#2");
132                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost:8080/index.html")), "#3");
133                         Assert.IsTrue (p.IsBypassed (new Uri ("http://loopback:8080/index.html")), "#4");
134                         Assert.IsTrue (p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")), "#5");
135                         Assert.IsTrue (p.IsBypassed (new Uri ("http://webserver/index.html")), "#6");
136                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://webserver.com/index.html")), "#7");
137
138                         p = new WebProxy ("http://proxy.contoso.com", false);
139                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#11");
140                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost/index.html")), "#12: lamespec of ms.net");
141                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost:8080/index.html")), "#13: lamespec of ms.net");
142                         Assert.IsTrue (p.IsBypassed (new Uri ("http://loopback:8080/index.html")), "#14: lamespec of ms.net");
143                         Assert.IsTrue (p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")), "#15: lamespec of ms.net");
144                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://webserver/index.html")), "#16");
145
146                         p.BypassList = new string [] { "google.com", "contoso.com" };
147                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#20");
148                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.GOOGLE.com")), "#21");
149                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.contoso.com:8080/foo/bar/index.html")), "#22");
150                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.contoso2.com:8080/foo/bar/index.html")), "#23");
151                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.foo.com:8080/contoso.com.html")), "#24");
152
153                         p.BypassList = new string [] { "https" };
154                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#30");
155                         Assert.IsTrue (p.IsBypassed (new Uri ("https://www.google.com")), "#31");
156                 }
157
158                 [Test]
159                 public void IsByPassed_Address_Null ()
160                 {
161                         WebProxy p = new WebProxy ((Uri) null, false);
162                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#1");
163
164                         p = new WebProxy ((Uri) null, true);
165                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#2");
166                 }
167
168                 [Test]
169                 public void IsByPassed_Host_Null ()
170                 {
171                         WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
172                         try {
173                                 p.IsBypassed (null);
174                                 Assert.Fail ("#A1");
175                         } catch (ArgumentNullException ex) {
176                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
177                                 Assert.IsNotNull (ex.Message, "#A3");
178                                 Assert.IsNotNull (ex.ParamName, "#A4");
179                                 Assert.AreEqual ("host", ex.ParamName, "#A5");
180                                 Assert.IsNull (ex.InnerException, "#A6");
181                         }
182
183                         p = new WebProxy ((Uri) null);
184                         try {
185                                 p.IsBypassed (null);
186                                 Assert.Fail ("#B1");
187                         } catch (ArgumentNullException ex) {
188                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
189                                 Assert.IsNotNull (ex.Message, "#B3");
190                                 Assert.IsNotNull (ex.ParamName, "#B4");
191                                 Assert.AreEqual ("host", ex.ParamName, "#B5");
192                                 Assert.IsNull (ex.InnerException, "#B6");
193                         }
194
195                         p = new WebProxy ((Uri) null, true);
196                         try {
197                                 p.IsBypassed (null);
198                                 Assert.Fail ("#C1");
199                         } catch (ArgumentNullException ex) {
200                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
201                                 Assert.IsNotNull (ex.Message, "#C3");
202                                 Assert.IsNotNull (ex.ParamName, "#C4");
203                                 Assert.AreEqual ("host", ex.ParamName, "#C5");
204                                 Assert.IsNull (ex.InnerException, "#C6");
205                         }
206                 }
207
208                 [Test]
209                 public void GetObjectData ()
210                 {
211                         SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
212                                 new FormatterConverter ());
213
214                         WebProxy proxy = new WebProxy ("proxy.ximian.com");
215                         ((ISerializable) proxy).GetObjectData (si, new StreamingContext ());
216                         Assert.AreEqual (4, si.MemberCount, "#A1");
217                         int i = 0;
218                         foreach (SerializationEntry entry in si) {
219                                 Assert.IsNotNull (entry.Name, "#A2:" + i);
220                                 Assert.IsNotNull (entry.ObjectType, "#A3:" + i);
221
222                                 switch (i) {
223                                 case 0:
224                                         Assert.AreEqual ("_BypassOnLocal", entry.Name, "#A4:" + i);
225                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
226                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
227                                         Assert.AreEqual (false, entry.Value, "#A7:" + i);
228                                         break;
229                                 case 1:
230                                         Assert.AreEqual ("_ProxyAddress", entry.Name, "#A4:" + i);
231                                         Assert.AreEqual (typeof (Uri), entry.ObjectType, "#A5:" + i);
232                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
233                                         break;
234                                 case 2:
235                                         Assert.AreEqual ("_BypassList", entry.Name, "#A4:" + i);
236                                         Assert.AreEqual (typeof (object), entry.ObjectType, "#A5:" + i);
237                                         Assert.IsNull (entry.Value, "#A6:" + i);
238                                         break;
239                                 case 3:
240                                         Assert.AreEqual ("_UseDefaultCredentials", entry.Name, "#A4:" + i);
241                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
242                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
243                                         Assert.AreEqual (false, entry.Value, "#A7:" + i);
244                                         break;
245                                 }
246                                 i++;
247                         }
248                 }
249         }
250 }