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 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");
38 Assert.Fail ("#5 not spec'd, but should follow ms.net implementation");
39 } catch (ArgumentNullException) { }
41 p = new WebProxy ("webserver.com", 8080);
42 Assert.AreEqual (new Uri ("http://webserver.com:8080/"), p.Address, "#6");
44 p = new WebProxy ("webserver");
45 Assert.AreEqual (new Uri ("http://webserver"), p.Address, "#7");
47 p = new WebProxy ("webserver.com");
48 Assert.AreEqual (new Uri ("http://webserver.com"), p.Address, "#8");
50 p = new WebProxy ("http://webserver.com");
51 Assert.AreEqual (new Uri ("http://webserver.com"), p.Address, "#9");
53 p = new WebProxy ("file://webserver");
54 Assert.AreEqual (new Uri ("file://webserver"), p.Address, "#10");
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");
61 p = new WebProxy ("http://contoso.com", true,
62 new string [] { "?^!@#$%^&}{][" }, null);
63 Assert.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 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");
82 p.BypassArrayList.Add ("?^!@#$%^&}{][");
83 Assert.AreEqual (3, p.BypassList.Length, "#10");
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");
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", "?^!@#$%^&}{][" };
108 } catch (ArgumentException) {
109 // weird, this way invalid regex's fail again..
112 Assert.AreEqual (2, p.BypassList.Length, "#2");
113 // but it did apparenly store the regex's !
115 p.BypassList = new string [] { "http://www.x.com" };
116 Assert.AreEqual (1, p.BypassList.Length, "#3");
121 } catch (ArgumentNullException) { }
123 Assert.AreEqual (1, p.BypassList.Length, "#4");
127 public void IsByPassed ()
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");
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");
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");
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");
159 public void IsByPassed_Address_Null ()
161 WebProxy p = new WebProxy ((Uri) null, false);
162 Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#1");
164 p = new WebProxy ((Uri) null, true);
165 Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#2");
169 public void IsByPassed_Host_Null ()
171 WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
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");
183 p = new WebProxy ((Uri) null);
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");
195 p = new WebProxy ((Uri) null, true);
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");
209 public void GetObjectData ()
211 SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
212 new FormatterConverter ());
214 WebProxy proxy = new WebProxy ("proxy.ximian.com");
215 ((ISerializable) proxy).GetObjectData (si, new StreamingContext ());
216 Assert.AreEqual (4, si.MemberCount, "#A1");
218 foreach (SerializationEntry entry in si) {
219 Assert.IsNotNull (entry.Name, "#A2:" + i);
220 Assert.IsNotNull (entry.ObjectType, "#A3:" + i);
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);
230 Assert.AreEqual ("_ProxyAddress", entry.Name, "#A4:" + i);
231 Assert.AreEqual (typeof (Uri), entry.ObjectType, "#A5:" + i);
232 Assert.IsNotNull (entry.Value, "#A6:" + i);
235 Assert.AreEqual ("_BypassList", entry.Name, "#A4:" + i);
236 Assert.AreEqual (typeof (object), entry.ObjectType, "#A5:" + i);
237 Assert.IsNull (entry.Value, "#A6:" + i);
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);