ServicePointTest.cs 5.8 KB

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