ServicePointTest.cs 8.5 KB

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