WebProxyTest.cs 9.3 KB

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