WebProxyTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. public void IsByPassed_Host_Null ()
  148. {
  149. WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
  150. try {
  151. p.IsBypassed (null);
  152. Assert.Fail ("#A1");
  153. #if NET_2_0
  154. } catch (ArgumentNullException ex) {
  155. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  156. Assert.IsNotNull (ex.Message, "#A3");
  157. Assert.IsNotNull (ex.ParamName, "#A4");
  158. Assert.AreEqual ("host", ex.ParamName, "#A5");
  159. Assert.IsNull (ex.InnerException, "#A6");
  160. }
  161. #else
  162. } catch (NullReferenceException) {
  163. }
  164. #endif
  165. p = new WebProxy ((Uri) null);
  166. try {
  167. p.IsBypassed (null);
  168. Assert.Fail ("#B1");
  169. #if NET_2_0
  170. } catch (ArgumentNullException ex) {
  171. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  172. Assert.IsNotNull (ex.Message, "#B3");
  173. Assert.IsNotNull (ex.ParamName, "#B4");
  174. Assert.AreEqual ("host", ex.ParamName, "#B5");
  175. Assert.IsNull (ex.InnerException, "#B6");
  176. }
  177. #else
  178. } catch (NullReferenceException) {
  179. }
  180. #endif
  181. p = new WebProxy ((Uri) null, true);
  182. try {
  183. p.IsBypassed (null);
  184. Assert.Fail ("#C1");
  185. #if NET_2_0
  186. } catch (ArgumentNullException ex) {
  187. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
  188. Assert.IsNotNull (ex.Message, "#C3");
  189. Assert.IsNotNull (ex.ParamName, "#C4");
  190. Assert.AreEqual ("host", ex.ParamName, "#C5");
  191. Assert.IsNull (ex.InnerException, "#C6");
  192. }
  193. #else
  194. } catch (NullReferenceException) {
  195. }
  196. #endif
  197. }
  198. [Test]
  199. public void GetObjectData ()
  200. {
  201. SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
  202. new FormatterConverter ());
  203. WebProxy proxy = new WebProxy ("proxy.ximian.com");
  204. ((ISerializable) proxy).GetObjectData (si, new StreamingContext ());
  205. #if NET_2_0
  206. Assert.AreEqual (4, si.MemberCount, "#A1");
  207. #else
  208. Assert.AreEqual (3, si.MemberCount, "#A1");
  209. #endif
  210. int i = 0;
  211. foreach (SerializationEntry entry in si) {
  212. Assert.IsNotNull (entry.Name, "#A2:" + i);
  213. Assert.IsNotNull (entry.ObjectType, "#A3:" + i);
  214. switch (i) {
  215. case 0:
  216. Assert.AreEqual ("_BypassOnLocal", entry.Name, "#A4:" + i);
  217. Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
  218. Assert.IsNotNull (entry.Value, "#A6:" + i);
  219. Assert.AreEqual (false, entry.Value, "#A7:" + i);
  220. break;
  221. case 1:
  222. Assert.AreEqual ("_ProxyAddress", entry.Name, "#A4:" + i);
  223. Assert.AreEqual (typeof (Uri), entry.ObjectType, "#A5:" + i);
  224. Assert.IsNotNull (entry.Value, "#A6:" + i);
  225. break;
  226. case 2:
  227. Assert.AreEqual ("_BypassList", entry.Name, "#A4:" + i);
  228. Assert.AreEqual (typeof (object), entry.ObjectType, "#A5:" + i);
  229. Assert.IsNull (entry.Value, "#A6:" + i);
  230. break;
  231. #if NET_2_0
  232. case 3:
  233. Assert.AreEqual ("_UseDefaultCredentials", entry.Name, "#A4:" + i);
  234. Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
  235. Assert.IsNotNull (entry.Value, "#A6:" + i);
  236. Assert.AreEqual (false, entry.Value, "#A7:" + i);
  237. break;
  238. #endif
  239. }
  240. i++;
  241. }
  242. }
  243. }
  244. }