ServicePointTest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. public void All ()
  34. {
  35. ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:[email protected]"));
  36. //WriteServicePoint ("A servicepoint that isn't really", p);
  37. ServicePointManager.MaxServicePoints = 2;
  38. ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));
  39. try {
  40. ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));
  41. Assert.Fail ("#1");
  42. } catch (InvalidOperationException) { }
  43. ServicePointManager.MaxServicePoints = 0;
  44. //WriteServicePoint ("google before getting a webrequest", google);
  45. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  46. HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
  47. #if FOUND_SOME_OTHER_URL
  48. // URL is no longer found, disabled the test until a more reliable URL is found :P
  49. //WriteServicePoint ("google after getting a response", google);
  50. ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));
  51. Assert.AreEqual (google, google2, "#equals");
  52. res.Close ();
  53. #endif
  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. ServicePoint sp2;
  77. #if FOUND_SOME_OTHER_URL
  78. // unless of course some buffering is taking place.. let's check
  79. Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
  80. sp2 = ServicePointManager.FindServicePoint (uri2);
  81. req2 = (HttpWebRequest) WebRequest.Create (uri2);
  82. async = req2.BeginGetResponse (null, null);
  83. //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
  84. // CurrentConnections: 1
  85. res2 = req2.EndGetResponse (async);
  86. //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
  87. // CurrentConnections: 1
  88. // and so it shows
  89. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  90. res2.Close ();
  91. #endif
  92. // what's the limit of the cache?
  93. req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
  94. res2 = req2.GetResponse ();
  95. sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
  96. //WriteServicePoint ("apache", sp2);
  97. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  98. // CurrentConnections: 1
  99. res2.Close ();
  100. // curious other effect: address is actually the full Uri of the previous request
  101. // anyways, buffer is probably 4096 bytes
  102. }
  103. // try getting the stream to 5 web response objects
  104. // while ConnectionLimit equals 2
  105. [Test]
  106. [Category ("InetAccess")]
  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. [Test]
  134. [Category ("InetAccess")]
  135. [Category ("AndroidNotWorking")] // #A1 fails
  136. public void EndPointBind ()
  137. {
  138. Uri uri = new Uri ("http://www.go-mono.com/");
  139. ServicePoint sp = ServicePointManager.FindServicePoint (uri);
  140. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
  141. bool called = false;
  142. sp.BindIPEndPointDelegate = delegate {
  143. Assert.IsTrue (!called);
  144. called = true;
  145. return null;
  146. };
  147. req.GetResponse ().Close ();
  148. Assert.IsTrue (called, "#A1");
  149. req = (HttpWebRequest) WebRequest.Create (uri);
  150. called = false;
  151. sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {
  152. Assert.IsTrue (times < 5);
  153. called = true;
  154. return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);
  155. };
  156. req.GetResponse ().Close ();
  157. Assert.IsTrue (called, "#A2");
  158. }
  159. public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)
  160. {
  161. }
  162. [Test] //Covers #19823
  163. public void CloseConnectionGroupConcurency ()
  164. {
  165. // Try with multiple service points
  166. for (var i = 0; i < 10; i++) {
  167. Uri targetUri = new Uri ("http://" + i + ".mono-project.com");
  168. var req = (HttpWebRequest) HttpWebRequest.Create (targetUri);
  169. req.ContentType = "application/x-www-form-urlencoded";
  170. req.Method = "POST";
  171. req.ConnectionGroupName = "" + i;
  172. req.ServicePoint.MaxIdleTime = 1;
  173. req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);
  174. Thread.Sleep (1);
  175. req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);
  176. }
  177. }
  178. // Debug code not used now, but could be useful later
  179. /*
  180. private void WriteServicePoint (string label, ServicePoint sp)
  181. {
  182. Console.WriteLine ("\n" + label);
  183. Console.WriteLine ("Address: " + sp.Address);
  184. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  185. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  186. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  187. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  188. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  189. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  190. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  191. }
  192. */
  193. }
  194. }