ServicePointTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // ServicePointTest.cs - NUnit Test Cases for System.Net.ServicePoint
  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 ServicePointTest
  20. {
  21. static private int max;
  22. [SetUp]
  23. public void SaveMax () {
  24. max = ServicePointManager.MaxServicePoints;
  25. ServicePointManager.MaxServicePoints = 0;
  26. }
  27. [TearDown]
  28. public void RestoreMax () {
  29. ServicePointManager.MaxServicePoints = max;
  30. }
  31. [Test]
  32. [Category ("InetAccess")]
  33. #if TARGET_JVM
  34. [Ignore ("Unsupported - ServicePointManager.FindServicePoint")]
  35. #endif
  36. public void All ()
  37. {
  38. ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:[email protected]"));
  39. //WriteServicePoint ("A servicepoint that isn't really", p);
  40. ServicePointManager.MaxServicePoints = 2;
  41. ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));
  42. try {
  43. ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));
  44. Assert.Fail ("#1");
  45. } catch (InvalidOperationException) { }
  46. ServicePointManager.MaxServicePoints = 0;
  47. //WriteServicePoint ("google before getting a webrequest", google);
  48. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  49. HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
  50. //WriteServicePoint ("google after getting a response", google);
  51. ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));
  52. Assert.AreEqual (google, google2, "#equals");
  53. res.Close ();
  54. // in both instances property CurrentConnections is 0 according to ms.net.
  55. // let's see what it says when we do async operations...
  56. HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  57. req2.Method = "PUT";
  58. IAsyncResult async = req2.BeginGetRequestStream (null, null);
  59. //WriteServicePoint ("after async BeginGetRequestStream", google);
  60. // CurrentConnections: 1
  61. Stream stream2 = req2.EndGetRequestStream (async);
  62. //WriteServicePoint ("after async EndGetRequestStream", google);
  63. // CurrentConnections: 1
  64. stream2.Close ();
  65. req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  66. async = req2.BeginGetResponse (null, null);
  67. //WriteServicePoint ("after async BeginGetResponse", google);
  68. // CurrentConnections: 2
  69. WebResponse res2 = req2.EndGetResponse (async);
  70. //WriteServicePoint ("after async EndGetResponse", google);
  71. // CurrentConnections: 0
  72. // curious that after you get the webresponse object CurrentConnections is set to 0.
  73. // you'd think that you'd still be connected until you close the webresponse..
  74. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  75. res2.Close ();
  76. // unless of course some buffering is taking place.. let's check
  77. Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
  78. ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);
  79. req2 = (HttpWebRequest) WebRequest.Create (uri2);
  80. async = req2.BeginGetResponse (null, null);
  81. //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
  82. // CurrentConnections: 1
  83. res2 = req2.EndGetResponse (async);
  84. //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
  85. // CurrentConnections: 1
  86. // and so it shows
  87. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  88. res2.Close ();
  89. // what's the limit of the cache?
  90. req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
  91. res2 = req2.GetResponse ();
  92. sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
  93. //WriteServicePoint ("apache", sp2);
  94. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  95. // CurrentConnections: 1
  96. res2.Close ();
  97. // curious other effect: address is actually the full Uri of the previous request
  98. // anyways, buffer is probably 4096 bytes
  99. }
  100. // try getting the stream to 5 web response objects
  101. // while ConnectionLimit equals 2
  102. [Test]
  103. [Category ("InetAccess")]
  104. #if TARGET_JVM
  105. [Ignore ("The System.Net.ServicePointManager.FindServicePoint(Uri) is not supported")]
  106. #endif
  107. public void ConnectionLimit ()
  108. {
  109. // the default is already 2, just in case it isn't..
  110. ServicePointManager.DefaultConnectionLimit = 5;
  111. Uri uri = new Uri ("http://www.go-mono.com/");
  112. ServicePoint sp = ServicePointManager.FindServicePoint (uri);
  113. WebResponse [] res = new WebResponse [5];
  114. for (int i = 0; i < 5; i++) {
  115. //Console.WriteLine ("GOT1 : " + i);
  116. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
  117. //Console.WriteLine ("GOT2 : " + i);
  118. res [i] = req.GetResponse ();
  119. //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);
  120. }
  121. for (int i = 0; i < 5; i++) {
  122. Stream stream = res [i].GetResponseStream();
  123. //Console.WriteLine ("Reading stream: " + i + " : " + stream);
  124. int len = 0;
  125. while (stream.ReadByte () != -1)
  126. len++;
  127. //Console.WriteLine ("Finished reading: " + len + " bytes");
  128. }
  129. for (int i = 0; i < 5; i++) {
  130. res [i].Close ();
  131. }
  132. }
  133. #if NET_2_0
  134. [Test]
  135. [Category ("InetAccess")]
  136. #if TARGET_JVM
  137. [Ignore ("The System.Net.ServicePointManager.FindServicePoint(Uri) is not supported")]
  138. #endif
  139. public void EndPointBind ()
  140. {
  141. Uri uri = new Uri ("http://www.go-mono.com/");
  142. ServicePoint sp = ServicePointManager.FindServicePoint (uri);
  143. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
  144. bool called = false;
  145. #if !TARGET_JVM
  146. sp.BindIPEndPointDelegate = delegate {
  147. Assert.IsTrue (!called);
  148. called = true;
  149. return null;
  150. };
  151. #endif
  152. req.GetResponse ().Close ();
  153. Assert.IsTrue (called);
  154. req = (HttpWebRequest) WebRequest.Create (uri);
  155. called = false;
  156. #if !TARGET_JVM
  157. sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {
  158. Assert.IsTrue (times < 5);
  159. called = true;
  160. return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);
  161. };
  162. #endif
  163. req.GetResponse ().Close ();
  164. Assert.IsTrue (called);
  165. }
  166. #endif
  167. // Debug code not used now, but could be useful later
  168. /*
  169. private void WriteServicePoint (string label, ServicePoint sp)
  170. {
  171. Console.WriteLine ("\n" + label);
  172. Console.WriteLine ("Address: " + sp.Address);
  173. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  174. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  175. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  176. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  177. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  178. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  179. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  180. }
  181. */
  182. }
  183. }