ServicePointTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #if TARGET_JVM
  34. [Ignore ("Unsupported - ServicePointManager.FindServicePoint")]
  35. #endif
  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. Assertion.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. //WriteServicePoint ("google after getting a response", google);
  51. ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));
  52. Assertion.AssertEquals ("#equals", google, google2);
  53. res.Close ();
  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. // unless of course some buffering is taking place.. let's check
  77. Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
  78. ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);
  79. req2 = (HttpWebRequest) WebRequest.Create (uri2);
  80. async = req2.BeginGetResponse (null, null);
  81. //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
  82. // CurrentConnections: 1
  83. res2 = req2.EndGetResponse (async);
  84. //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
  85. // CurrentConnections: 1
  86. // and so it shows
  87. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  88. res2.Close ();
  89. // what's the limit of the cache?
  90. req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
  91. res2 = req2.GetResponse ();
  92. sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
  93. //WriteServicePoint ("apache", sp2);
  94. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  95. // CurrentConnections: 1
  96. res2.Close ();
  97. // curious other effect: address is actually the full Uri of the previous request
  98. // anyways, buffer is probably 4096 bytes
  99. }
  100. // try getting the stream to 5 web response objects
  101. // while ConnectionLimit equals 2
  102. [Test]
  103. [Category ("InetAccess")]
  104. #if TARGET_JVM
  105. [Ignore ("The System.Net.ServicePointManager.FindServicePoint(Uri) is not supported")]
  106. #endif
  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. // Debug code not used now, but could be useful later
  134. /*
  135. private void WriteServicePoint (string label, ServicePoint sp)
  136. {
  137. Console.WriteLine ("\n" + label);
  138. Console.WriteLine ("Address: " + sp.Address);
  139. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  140. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  141. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  142. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  143. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  144. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  145. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  146. }
  147. */
  148. }
  149. }