WebProxyTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // WebProxyTest.cs - NUnit Test Cases for System.Net.WebProxy
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Collections;
  10. using System.IO;
  11. using System.Net;
  12. using System.Threading;
  13. namespace MonoTests.System.Net
  14. {
  15. public class WebProxyTest : TestCase
  16. {
  17. private Uri googleUri;
  18. private Uri yahooUri;
  19. private Uri apacheUri;
  20. public WebProxyTest () :
  21. base ("[MonoTests.System.Net.WebProxyTest]") {}
  22. public WebProxyTest (string name) : base (name) {}
  23. protected override void SetUp () {
  24. googleUri = new Uri ("http://www.google.com");
  25. yahooUri = new Uri ("http://www.yahoo.com");
  26. apacheUri = new Uri ("http://www.apache.org");
  27. }
  28. protected override void TearDown () {}
  29. public static ITest Suite
  30. {
  31. get {
  32. return new TestSuite (typeof (WebProxyTest));
  33. }
  34. }
  35. public void TestConstructors ()
  36. {
  37. WebProxy p = new WebProxy ();
  38. Assert("#1", p.Address == null);
  39. AssertEquals ("#2", 0, p.BypassArrayList.Count);
  40. AssertEquals ("#3", 0, p.BypassList.Length);
  41. AssertEquals ("#4", false, p.BypassProxyOnLocal);
  42. try {
  43. p.BypassList = null;
  44. Fail ("#5 not spec'd, but should follow ms.net implementation");
  45. } catch (ArgumentNullException) {}
  46. p = new WebProxy ("webserver.com", 8080);
  47. AssertEquals ("#6", new Uri ("http://webserver.com:8080/"), p.Address);
  48. p = new WebProxy ("webserver");
  49. AssertEquals ("#7", new Uri ("http://webserver"), p.Address);
  50. p = new WebProxy ("webserver.com");
  51. AssertEquals ("#8", new Uri ("http://webserver.com"), p.Address);
  52. p = new WebProxy ("http://webserver.com");
  53. AssertEquals ("#9", new Uri ("http://webserver.com"), p.Address);
  54. p = new WebProxy ("file://webserver");
  55. AssertEquals ("#10", new Uri ("file://webserver"), p.Address);
  56. p = new WebProxy ("http://www.contoso.com", true, null, null);
  57. AssertEquals ("#11", 0, p.BypassList.Length);
  58. AssertEquals ("#12", 0, p.BypassArrayList.Count);
  59. try {
  60. p = new WebProxy ("http://contoso.com", true,
  61. new string [] {"?^!@#$%^&}{]["}, null);
  62. Fail ("#13: illegal regular expression");
  63. } catch (ArgumentException) {
  64. }
  65. }
  66. public void TestBypassArrayList ()
  67. {
  68. Uri proxy1 = new Uri("http://proxy.contoso.com");
  69. Uri proxy2 = new Uri ("http://proxy2.contoso.com");
  70. WebProxy p = new WebProxy (proxy1, true);
  71. p.BypassArrayList.Add ("http://proxy2.contoso.com");
  72. p.BypassArrayList.Add ("http://proxy2.contoso.com");
  73. AssertEquals ("#1", 2, p.BypassList.Length);
  74. Assert ("#2", !p.IsBypassed (new Uri ("http://www.google.com")));
  75. Assert ("#3", p.IsBypassed (proxy2));
  76. AssertEquals ("#4", proxy2, p.GetProxy (proxy2));
  77. p.BypassArrayList.Add ("?^!@#$%^&}{][");
  78. AssertEquals ("#10", 3, p.BypassList.Length);
  79. try {
  80. Assert ("#11", !p.IsBypassed (proxy2));
  81. Assert ("#12", !p.IsBypassed (new Uri ("http://www.x.com")));
  82. AssertEquals ("#13", proxy1, p.GetProxy (proxy2));
  83. // hmm... although #11 and #13 succeeded before (#3 resp. #4),
  84. // it now fails to bypass, and the IsByPassed and GetProxy
  85. // methods do not fail.. so when an illegal regular
  86. // expression is added through this property it's ignored.
  87. // probably an ms.net bug?? :(
  88. } catch (ArgumentException) {
  89. Fail ("#15: illegal regular expression");
  90. }
  91. }
  92. public void TestBypassList ()
  93. {
  94. Uri proxy1 = new Uri("http://proxy.contoso.com");
  95. Uri proxy2 = new Uri ("http://proxy2.contoso.com");
  96. WebProxy p = new WebProxy (proxy1, true);
  97. try {
  98. p.BypassList = new string [] {"http://proxy2.contoso.com", "?^!@#$%^&}{]["};
  99. Fail ("#1");
  100. } catch (ArgumentException) {
  101. // weird, this way invalid regex's fail again..
  102. }
  103. AssertEquals ("#2", 2, p.BypassList.Length);
  104. // but it did apparenly store the regex's !
  105. p.BypassList = new string [] {"http://www.x.com"};
  106. AssertEquals ("#3", 1, p.BypassList.Length);
  107. try {
  108. p.BypassList = null;
  109. Fail ("#4");
  110. } catch (ArgumentNullException) {}
  111. AssertEquals ("#4", 1, p.BypassList.Length);
  112. }
  113. public void TestGetProxy ()
  114. {
  115. }
  116. public void TestIsByPassed ()
  117. {
  118. WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
  119. Assert ("#1", !p.IsBypassed (new Uri ("http://www.google.com")));
  120. Assert ("#2", p.IsBypassed (new Uri ("http://localhost/index.html")));
  121. Assert ("#3", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));
  122. Assert ("#4", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));
  123. Assert ("#5", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));
  124. Assert ("#6", p.IsBypassed (new Uri ("http://webserver/index.html")));
  125. Assert ("#7", !p.IsBypassed (new Uri ("http://webserver.com/index.html")));
  126. try {
  127. p.IsBypassed (null);
  128. Fail ("#8 not spec'd, but should follow ms.net implementation");
  129. } catch (NullReferenceException) {}
  130. p = new WebProxy ("http://proxy.contoso.com", false);
  131. Assert ("#11", !p.IsBypassed (new Uri ("http://www.google.com")));
  132. Assert ("#12: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost/index.html")));
  133. Assert ("#13: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));
  134. Assert ("#14: lamespec of ms.net", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));
  135. Assert ("#15: lamespec of ms.net", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));
  136. Assert ("#16", !p.IsBypassed (new Uri ("http://webserver/index.html")));
  137. p.BypassList = new string [] { "google.com", "contoso.com" };
  138. Assert ("#20", p.IsBypassed (new Uri ("http://www.google.com")));
  139. Assert ("#21", p.IsBypassed (new Uri ("http://www.GOOGLE.com")));
  140. Assert ("#22", p.IsBypassed (new Uri ("http://www.contoso.com:8080/foo/bar/index.html")));
  141. Assert ("#23", !p.IsBypassed (new Uri ("http://www.contoso2.com:8080/foo/bar/index.html")));
  142. Assert ("#24", !p.IsBypassed (new Uri ("http://www.foo.com:8080/contoso.com.html")));
  143. p.BypassList = new string [] { "https" };
  144. Assert ("#30", !p.IsBypassed (new Uri ("http://www.google.com")));
  145. Assert ("#31", p.IsBypassed (new Uri ("https://www.google.com")));
  146. }
  147. }
  148. }