ServicePointTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // ServicePointTest.cs - NUnit Test Cases for System.Net.ServicePoint
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Collections;
  10. using System.IO;
  11. using System.Net;
  12. using System.Threading;
  13. namespace MonoTests.System.Net
  14. {
  15. public class ServicePointTest : TestCase
  16. {
  17. public ServicePointTest () :
  18. base ("[MonoTests.System.Net.ServicePointTest]") {}
  19. public ServicePointTest (string name) : base (name) {}
  20. protected override void SetUp () {}
  21. protected override void TearDown () {}
  22. public static ITest Suite
  23. {
  24. get {
  25. return new TestSuite (typeof (ServicePointTest));
  26. }
  27. }
  28. public void TestAll ()
  29. {
  30. try {
  31. ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:[email protected]"));
  32. WriteServicePoint ("A servicepoint that isn't really", p);
  33. ServicePointManager.MaxServicePoints = 2;
  34. ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));
  35. try {
  36. ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));
  37. Fail ("#1");
  38. } catch (InvalidOperationException) { }
  39. ServicePointManager.MaxServicePoints = 0;
  40. WriteServicePoint ("google before getting a webrequest", google);
  41. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  42. HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
  43. WriteServicePoint ("google after getting a response", google);
  44. ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));
  45. AssertEquals ("#equals", google, google2);
  46. res.Close ();
  47. // in both instances property CurrentConnections is 0 according to ms.net.
  48. // let's see what it says when we do async operations...
  49. HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  50. req2.Method = "PUT";
  51. IAsyncResult async = req2.BeginGetRequestStream (null, null);
  52. WriteServicePoint ("after async BeginGetRequestStream", google);
  53. // CurrentConnections: 1
  54. Stream stream2 = req2.EndGetRequestStream (async);
  55. WriteServicePoint ("after async EndGetRequestStream", google);
  56. // CurrentConnections: 1
  57. stream2.Close ();
  58. req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  59. async = req2.BeginGetResponse (null, null);
  60. WriteServicePoint ("after async BeginGetResponse", google);
  61. // CurrentConnections: 2
  62. WebResponse res2 = req2.EndGetResponse (async);
  63. WriteServicePoint ("after async EndGetResponse", google);
  64. // CurrentConnections: 0
  65. // curious that after you get the webresponse object CurrentConnections is set to 0.
  66. // you'd think that you'd still be connected until you close the webresponse..
  67. Console.WriteLine ("ContentLength: " + res2.ContentLength);
  68. res2.Close ();
  69. // unless of course some buffering is taking place.. let's check
  70. Uri uri2 = new Uri ("http://www.apache.org/dist/httpd/httpd-2.0.36.tar.gz");
  71. ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);
  72. req2 = (HttpWebRequest) WebRequest.Create (uri2);
  73. async = req2.BeginGetResponse (null, null);
  74. WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
  75. // CurrentConnections: 1
  76. res2 = req2.EndGetResponse (async);
  77. WriteServicePoint ("Large file: after async EndGetResponse", sp2);
  78. // CurrentConnections: 1
  79. // and so it shows
  80. Console.WriteLine ("ContentLength: " + res2.ContentLength);
  81. res2.Close ();
  82. // what's the limit of the cache?
  83. req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
  84. res2 = req2.GetResponse ();
  85. sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
  86. WriteServicePoint ("apache", sp2);
  87. Console.WriteLine ("ContentLength: " + res2.ContentLength);
  88. // CurrentConnections: 1
  89. res2.Close ();
  90. // curious other effect: address is actually the full Uri of the previous request
  91. // anyways, buffer is probably 4096 bytes
  92. } catch (WebException e) {
  93. Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);
  94. }
  95. }
  96. // try getting the stream to 5 web response objects
  97. // while ConnectionLimit equals 2
  98. /*
  99. public void TestConnectionLimit ()
  100. {
  101. try {
  102. // the default is already 2, just in case it isn't..
  103. ServicePointManager.DefaultConnectionLimit = 2;
  104. Uri uri = new Uri ("http://www.apache.org/dist/httpd/httpd-2.0.36.tar.gz");
  105. ServicePoint sp = ServicePointManager.FindServicePoint (uri);
  106. WebResponse [] res = new WebResponse [5];
  107. for (int i = 0; i < 5; i++) {
  108. Console.WriteLine ("GOT1 : " + i);
  109. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
  110. Console.WriteLine ("GOT2 : " + i);
  111. res [i] = req.GetResponse ();
  112. WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);
  113. }
  114. for (int i = 0; i < 5; i++) {
  115. Stream stream = res [i].GetResponseStream();
  116. Console.WriteLine ("Reading stream: " + i + " : " + stream);
  117. int len = 0;
  118. while (stream.ReadByte () != -1)
  119. len++;
  120. Console.WriteLine ("Finished reading: " + len + " bytes");
  121. }
  122. for (int i = 0; i < 5; i++) {
  123. res [i].Close ();
  124. }
  125. } catch (WebException e) {
  126. Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);
  127. }
  128. }
  129. */
  130. private void WriteServicePoint (string label, ServicePoint sp)
  131. {
  132. Console.WriteLine ("\n" + label);
  133. Console.WriteLine ("Address: " + sp.Address);
  134. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  135. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  136. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  137. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  138. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  139. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  140. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  141. }
  142. }
  143. }