ServicePointTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.Reflection;
  16. using System.Threading;
  17. namespace MonoTests.System.Net
  18. {
  19. [TestFixture]
  20. public class ServicePointTest
  21. {
  22. static private int max;
  23. [SetUp]
  24. public void SaveMax () {
  25. max = ServicePointManager.MaxServicePoints;
  26. ServicePointManager.MaxServicePoints = 0;
  27. }
  28. [TearDown]
  29. public void RestoreMax () {
  30. ServicePointManager.MaxServicePoints = max;
  31. }
  32. [Test]
  33. [Category ("InetAccess")]
  34. public void All ()
  35. {
  36. ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:[email protected]"));
  37. //WriteServicePoint ("A servicepoint that isn't really", p);
  38. ServicePointManager.MaxServicePoints = 2;
  39. ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));
  40. try {
  41. ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));
  42. Assert.Fail ("#1");
  43. } catch (InvalidOperationException) { }
  44. ServicePointManager.MaxServicePoints = 0;
  45. //WriteServicePoint ("google before getting a webrequest", google);
  46. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  47. HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
  48. #if FOUND_SOME_OTHER_URL
  49. // URL is no longer found, disabled the test until a more reliable URL is found :P
  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. #endif
  55. // in both instances property CurrentConnections is 0 according to ms.net.
  56. // let's see what it says when we do async operations...
  57. HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  58. req2.Method = "PUT";
  59. IAsyncResult async = req2.BeginGetRequestStream (null, null);
  60. //WriteServicePoint ("after async BeginGetRequestStream", google);
  61. // CurrentConnections: 1
  62. Stream stream2 = req2.EndGetRequestStream (async);
  63. //WriteServicePoint ("after async EndGetRequestStream", google);
  64. // CurrentConnections: 1
  65. stream2.Close ();
  66. req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  67. async = req2.BeginGetResponse (null, null);
  68. //WriteServicePoint ("after async BeginGetResponse", google);
  69. // CurrentConnections: 2
  70. WebResponse res2 = req2.EndGetResponse (async);
  71. //WriteServicePoint ("after async EndGetResponse", google);
  72. // CurrentConnections: 0
  73. // curious that after you get the webresponse object CurrentConnections is set to 0.
  74. // you'd think that you'd still be connected until you close the webresponse..
  75. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  76. res2.Close ();
  77. ServicePoint sp2;
  78. #if FOUND_SOME_OTHER_URL
  79. // unless of course some buffering is taking place.. let's check
  80. Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
  81. sp2 = ServicePointManager.FindServicePoint (uri2);
  82. req2 = (HttpWebRequest) WebRequest.Create (uri2);
  83. async = req2.BeginGetResponse (null, null);
  84. //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
  85. // CurrentConnections: 1
  86. res2 = req2.EndGetResponse (async);
  87. //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
  88. // CurrentConnections: 1
  89. // and so it shows
  90. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  91. res2.Close ();
  92. #endif
  93. // what's the limit of the cache?
  94. req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
  95. res2 = req2.GetResponse ();
  96. sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
  97. //WriteServicePoint ("apache", sp2);
  98. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  99. // CurrentConnections: 1
  100. res2.Close ();
  101. // curious other effect: address is actually the full Uri of the previous request
  102. // anyways, buffer is probably 4096 bytes
  103. }
  104. // try getting the stream to 5 web response objects
  105. // while ConnectionLimit equals 2
  106. [Test]
  107. [Category ("InetAccess")]
  108. public void ConnectionLimit ()
  109. {
  110. // the default is already 2, just in case it isn't..
  111. ServicePointManager.DefaultConnectionLimit = 5;
  112. Uri uri = new Uri ("http://www.go-mono.com/");
  113. ServicePoint sp = ServicePointManager.FindServicePoint (uri);
  114. WebResponse [] res = new WebResponse [5];
  115. for (int i = 0; i < 5; i++) {
  116. //Console.WriteLine ("GOT1 : " + i);
  117. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
  118. //Console.WriteLine ("GOT2 : " + i);
  119. res [i] = req.GetResponse ();
  120. //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);
  121. }
  122. for (int i = 0; i < 5; i++) {
  123. Stream stream = res [i].GetResponseStream();
  124. //Console.WriteLine ("Reading stream: " + i + " : " + stream);
  125. int len = 0;
  126. while (stream.ReadByte () != -1)
  127. len++;
  128. //Console.WriteLine ("Finished reading: " + len + " bytes");
  129. }
  130. for (int i = 0; i < 5; i++) {
  131. res [i].Close ();
  132. }
  133. }
  134. [Test]
  135. [Category ("InetAccess")]
  136. [Category ("AndroidNotWorking")] // #A1 fails
  137. public void EndPointBind ()
  138. {
  139. Uri uri = new Uri ("http://www.go-mono.com/");
  140. ServicePoint sp = ServicePointManager.FindServicePoint (uri);
  141. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
  142. bool called = false;
  143. sp.BindIPEndPointDelegate = delegate {
  144. Assert.IsTrue (!called);
  145. called = true;
  146. return null;
  147. };
  148. req.GetResponse ().Close ();
  149. Assert.IsTrue (called, "#A1");
  150. req = (HttpWebRequest) WebRequest.Create (uri);
  151. called = false;
  152. sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {
  153. Assert.IsTrue (times < 5);
  154. called = true;
  155. return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);
  156. };
  157. req.GetResponse ().Close ();
  158. Assert.IsTrue (called, "#A2");
  159. }
  160. public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)
  161. {
  162. }
  163. [Test] //Covers #19823
  164. #if FEATURE_NO_BSD_SOCKETS
  165. // This test uses HttpWebRequest
  166. [ExpectedException (typeof (PlatformNotSupportedException))]
  167. #endif
  168. public void CloseConnectionGroupConcurency ()
  169. {
  170. // Try with multiple service points
  171. for (var i = 0; i < 10; i++) {
  172. Uri targetUri = new Uri ("http://" + i + ".mono-project.com");
  173. var req = (HttpWebRequest) HttpWebRequest.Create (targetUri);
  174. req.ContentType = "application/x-www-form-urlencoded";
  175. req.Method = "POST";
  176. req.ConnectionGroupName = "" + i;
  177. req.ServicePoint.MaxIdleTime = 1;
  178. req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);
  179. Thread.Sleep (1);
  180. req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);
  181. }
  182. }
  183. [Test]
  184. [Category ("RequiresBSDSockets")] // Tests internals, so it doesn't make sense to assert that PlatformNotSupportedExceptions are thrown.
  185. public void DnsRefreshTimeout ()
  186. {
  187. const int dnsRefreshTimeout = 2000;
  188. ServicePoint sp;
  189. IPHostEntry host0, host1, host2;
  190. Uri uri;
  191. PropertyInfo hostEntryProperty;
  192. ServicePointManager.DnsRefreshTimeout = dnsRefreshTimeout;
  193. uri = new Uri ("http://localhost/");
  194. sp = ServicePointManager.FindServicePoint (uri);
  195. hostEntryProperty = typeof (ServicePoint).GetProperty ("HostEntry", BindingFlags.NonPublic | BindingFlags.Instance);
  196. host0 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
  197. host1 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
  198. Assert.AreSame (host0, host1, "HostEntry should result in the same IPHostEntry object.");
  199. Thread.Sleep (dnsRefreshTimeout * 2);
  200. host2 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
  201. Assert.AreNotSame(host0, host2, "HostEntry should result in a new IPHostEntry " +
  202. "object when DnsRefreshTimeout is reached.");
  203. }
  204. // Debug code not used now, but could be useful later
  205. /*
  206. private void WriteServicePoint (string label, ServicePoint sp)
  207. {
  208. Console.WriteLine ("\n" + label);
  209. Console.WriteLine ("Address: " + sp.Address);
  210. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  211. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  212. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  213. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  214. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  215. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  216. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  217. }
  218. */
  219. }
  220. }