2 // WebProxyTest.cs - NUnit Test Cases for System.Net.WebProxy
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Gert Driesen (drieseng@users.sourceforge.net)
9 // (C) 2003 Martin Willemoes Hansen
13 using System.Collections;
16 using System.Runtime.Serialization;
17 using System.Runtime.Serialization.Formatters;
18 using System.Runtime.Serialization.Formatters.Binary;
19 using System.Threading;
21 using NUnit.Framework;
23 namespace MonoTests.System.Net
26 public class WebProxyTest
29 public void Constructors ()
31 WebProxy p = new WebProxy ();
32 Assertion.Assert ("#1", p.Address == null);
33 Assertion.AssertEquals ("#2", 0, p.BypassArrayList.Count);
34 Assertion.AssertEquals ("#3", 0, p.BypassList.Length);
35 Assertion.AssertEquals ("#4", false, p.BypassProxyOnLocal);
38 Assertion.Fail ("#5 not spec'd, but should follow ms.net implementation");
39 } catch (ArgumentNullException) { }
41 p = new WebProxy ("webserver.com", 8080);
42 Assertion.AssertEquals ("#6", new Uri ("http://webserver.com:8080/"), p.Address);
44 p = new WebProxy ("webserver");
45 Assertion.AssertEquals ("#7", new Uri ("http://webserver"), p.Address);
47 p = new WebProxy ("webserver.com");
48 Assertion.AssertEquals ("#8", new Uri ("http://webserver.com"), p.Address);
50 p = new WebProxy ("http://webserver.com");
51 Assertion.AssertEquals ("#9", new Uri ("http://webserver.com"), p.Address);
53 p = new WebProxy ("file://webserver");
54 Assertion.AssertEquals ("#10", new Uri ("file://webserver"), p.Address);
56 p = new WebProxy ("http://www.contoso.com", true, null, null);
57 Assertion.AssertEquals ("#11", 0, p.BypassList.Length);
58 Assertion.AssertEquals ("#12", 0, p.BypassArrayList.Count);
61 p = new WebProxy ("http://contoso.com", true,
62 new string [] { "?^!@#$%^&}{][" }, null);
63 Assertion.Fail ("#13: illegal regular expression");
64 } catch (ArgumentException) {
69 public void BypassArrayList ()
71 Uri proxy1 = new Uri ("http://proxy.contoso.com");
72 Uri proxy2 = new Uri ("http://proxy2.contoso.com");
74 WebProxy p = new WebProxy (proxy1, true);
75 p.BypassArrayList.Add ("http://proxy2.contoso.com");
76 p.BypassArrayList.Add ("http://proxy2.contoso.com");
77 Assertion.AssertEquals ("#1", 2, p.BypassList.Length);
78 Assertion.Assert ("#2", !p.IsBypassed (new Uri ("http://www.google.com")));
79 Assertion.Assert ("#3", p.IsBypassed (proxy2));
80 Assertion.AssertEquals ("#4", proxy2, p.GetProxy (proxy2));
82 p.BypassArrayList.Add ("?^!@#$%^&}{][");
83 Assertion.AssertEquals ("#10", 3, p.BypassList.Length);
85 Assertion.Assert ("#11", !p.IsBypassed (proxy2));
86 Assertion.Assert ("#12", !p.IsBypassed (new Uri ("http://www.x.com")));
87 Assertion.AssertEquals ("#13", proxy1, p.GetProxy (proxy2));
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 Assertion.Fail ("#15: illegal regular expression");
99 public void BypassList ()
101 Uri proxy1 = new Uri ("http://proxy.contoso.com");
102 Uri proxy2 = new Uri ("http://proxy2.contoso.com");
104 WebProxy p = new WebProxy (proxy1, true);
106 p.BypassList = new string [] { "http://proxy2.contoso.com", "?^!@#$%^&}{][" };
107 Assertion.Fail ("#1");
108 } catch (ArgumentException) {
109 // weird, this way invalid regex's fail again..
112 Assertion.AssertEquals ("#2", 2, p.BypassList.Length);
113 // but it did apparenly store the regex's !
115 p.BypassList = new string [] { "http://www.x.com" };
116 Assertion.AssertEquals ("#3", 1, p.BypassList.Length);
120 Assertion.Fail ("#4");
121 } catch (ArgumentNullException) { }
123 Assertion.AssertEquals ("#4", 1, p.BypassList.Length);
127 public void GetProxy ()
132 public void IsByPassed ()
134 WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
135 Assertion.Assert ("#1", !p.IsBypassed (new Uri ("http://www.google.com")));
136 Assertion.Assert ("#2", p.IsBypassed (new Uri ("http://localhost/index.html")));
137 Assertion.Assert ("#3", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));
138 Assertion.Assert ("#4", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));
139 Assertion.Assert ("#5", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));
140 Assertion.Assert ("#6", p.IsBypassed (new Uri ("http://webserver/index.html")));
141 Assertion.Assert ("#7", !p.IsBypassed (new Uri ("http://webserver.com/index.html")));
143 p = new WebProxy ("http://proxy.contoso.com", false);
144 Assertion.Assert ("#11", !p.IsBypassed (new Uri ("http://www.google.com")));
145 Assertion.Assert ("#12: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost/index.html")));
146 Assertion.Assert ("#13: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));
147 Assertion.Assert ("#14: lamespec of ms.net", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));
148 Assertion.Assert ("#15: lamespec of ms.net", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));
149 Assertion.Assert ("#16", !p.IsBypassed (new Uri ("http://webserver/index.html")));
151 p.BypassList = new string [] { "google.com", "contoso.com" };
152 Assertion.Assert ("#20", p.IsBypassed (new Uri ("http://www.google.com")));
153 Assertion.Assert ("#21", p.IsBypassed (new Uri ("http://www.GOOGLE.com")));
154 Assertion.Assert ("#22", p.IsBypassed (new Uri ("http://www.contoso.com:8080/foo/bar/index.html")));
155 Assertion.Assert ("#23", !p.IsBypassed (new Uri ("http://www.contoso2.com:8080/foo/bar/index.html")));
156 Assertion.Assert ("#24", !p.IsBypassed (new Uri ("http://www.foo.com:8080/contoso.com.html")));
158 p.BypassList = new string [] { "https" };
159 Assertion.Assert ("#30", !p.IsBypassed (new Uri ("http://www.google.com")));
160 Assertion.Assert ("#31", p.IsBypassed (new Uri ("https://www.google.com")));
164 public void IsByPassed_Address_Null ()
166 WebProxy p = new WebProxy ((Uri) null, false);
167 Assertion.Assert ("#1", p.IsBypassed (new Uri ("http://www.google.com")));
169 p = new WebProxy ((Uri) null, true);
170 Assertion.Assert ("#2", p.IsBypassed (new Uri ("http://www.google.com")));
175 [Ignore ("TD BUG ID: 7213")]
177 public void IsByPassed_Host_Null ()
179 WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
182 Assertion.Fail ("#A1");
184 } catch (ArgumentNullException ex) {
185 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
186 Assert.IsNotNull (ex.Message, "#A3");
187 Assert.IsNotNull (ex.ParamName, "#A4");
188 Assert.AreEqual ("host", ex.ParamName, "#A5");
189 Assert.IsNull (ex.InnerException, "#A6");
192 } catch (NullReferenceException) {
196 p = new WebProxy ((Uri) null);
199 Assertion.Fail ("#B1");
201 } catch (ArgumentNullException ex) {
202 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
203 Assert.IsNotNull (ex.Message, "#B3");
204 Assert.IsNotNull (ex.ParamName, "#B4");
205 Assert.AreEqual ("host", ex.ParamName, "#B5");
206 Assert.IsNull (ex.InnerException, "#B6");
209 } catch (NullReferenceException) {
213 p = new WebProxy ((Uri) null, true);
216 Assertion.Fail ("#C1");
218 } catch (ArgumentNullException ex) {
219 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
220 Assert.IsNotNull (ex.Message, "#C3");
221 Assert.IsNotNull (ex.ParamName, "#C4");
222 Assert.AreEqual ("host", ex.ParamName, "#C5");
223 Assert.IsNull (ex.InnerException, "#C6");
226 } catch (NullReferenceException) {
233 [Ignore ("The MS compliant binary serialization is not supported")]
235 public void GetObjectData ()
237 SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
238 new FormatterConverter ());
240 WebProxy proxy = new WebProxy ("proxy.ximian.com");
241 ((ISerializable) proxy).GetObjectData (si, new StreamingContext ());
243 Assert.AreEqual (4, si.MemberCount, "#A1");
245 Assert.AreEqual (3, si.MemberCount, "#A1");
248 foreach (SerializationEntry entry in si) {
249 Assert.IsNotNull (entry.Name, "#A2:" + i);
250 Assert.IsNotNull (entry.ObjectType, "#A3:" + i);
254 Assert.AreEqual ("_BypassOnLocal", entry.Name, "#A4:" + i);
255 Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
256 Assert.IsNotNull (entry.Value, "#A6:" + i);
257 Assert.AreEqual (false, entry.Value, "#A7:" + i);
260 Assert.AreEqual ("_ProxyAddress", entry.Name, "#A4:" + i);
261 Assert.AreEqual (typeof (Uri), entry.ObjectType, "#A5:" + i);
262 Assert.IsNotNull (entry.Value, "#A6:" + i);
265 Assert.AreEqual ("_BypassList", entry.Name, "#A4:" + i);
266 Assert.AreEqual (typeof (object), entry.ObjectType, "#A5:" + i);
267 Assert.IsNull (entry.Value, "#A6:" + i);
271 Assert.AreEqual ("_UseDefaultCredentials", entry.Name, "#A4:" + i);
272 Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
273 Assert.IsNotNull (entry.Value, "#A6:" + i);
274 Assert.AreEqual (false, entry.Value, "#A7:" + i);