WebProxyTest.cs 9.8 KB

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