2
0

WebProxyTest.cs 6.5 KB

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