ServicePointTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Assertion.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. //WriteServicePoint ("google after getting a response", google);
  48. ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));
  49. Assertion.AssertEquals ("#equals", google, google2);
  50. res.Close ();
  51. // in both instances property CurrentConnections is 0 according to ms.net.
  52. // let's see what it says when we do async operations...
  53. HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  54. req2.Method = "PUT";
  55. IAsyncResult async = req2.BeginGetRequestStream (null, null);
  56. //WriteServicePoint ("after async BeginGetRequestStream", google);
  57. // CurrentConnections: 1
  58. Stream stream2 = req2.EndGetRequestStream (async);
  59. //WriteServicePoint ("after async EndGetRequestStream", google);
  60. // CurrentConnections: 1
  61. stream2.Close ();
  62. req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  63. async = req2.BeginGetResponse (null, null);
  64. //WriteServicePoint ("after async BeginGetResponse", google);
  65. // CurrentConnections: 2
  66. WebResponse res2 = req2.EndGetResponse (async);
  67. //WriteServicePoint ("after async EndGetResponse", google);
  68. // CurrentConnections: 0
  69. // curious that after you get the webresponse object CurrentConnections is set to 0.
  70. // you'd think that you'd still be connected until you close the webresponse..
  71. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  72. res2.Close ();
  73. // unless of course some buffering is taking place.. let's check
  74. Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
  75. ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);
  76. req2 = (HttpWebRequest) WebRequest.Create (uri2);
  77. async = req2.BeginGetResponse (null, null);
  78. //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
  79. // CurrentConnections: 1
  80. res2 = req2.EndGetResponse (async);
  81. //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
  82. // CurrentConnections: 1
  83. // and so it shows
  84. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  85. res2.Close ();
  86. // what's the limit of the cache?
  87. req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
  88. res2 = req2.GetResponse ();
  89. sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
  90. //WriteServicePoint ("apache", sp2);
  91. //Console.WriteLine ("ContentLength: " + res2.ContentLength);
  92. // CurrentConnections: 1
  93. res2.Close ();
  94. // curious other effect: address is actually the full Uri of the previous request
  95. // anyways, buffer is probably 4096 bytes
  96. }
  97. // try getting the stream to 5 web response objects
  98. // while ConnectionLimit equals 2
  99. [Test]
  100. [Category ("InetAccess")]
  101. public void ConnectionLimit ()
  102. {
  103. // the default is already 2, just in case it isn't..
  104. ServicePointManager.DefaultConnectionLimit = 5;
  105. Uri uri = new Uri ("http://www.go-mono.com/");
  106. ServicePoint sp = ServicePointManager.FindServicePoint (uri);
  107. WebResponse [] res = new WebResponse [5];
  108. for (int i = 0; i < 5; i++) {
  109. //Console.WriteLine ("GOT1 : " + i);
  110. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
  111. //Console.WriteLine ("GOT2 : " + i);
  112. res [i] = req.GetResponse ();
  113. //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);
  114. }
  115. for (int i = 0; i < 5; i++) {
  116. Stream stream = res [i].GetResponseStream();
  117. //Console.WriteLine ("Reading stream: " + i + " : " + stream);
  118. int len = 0;
  119. while (stream.ReadByte () != -1)
  120. len++;
  121. //Console.WriteLine ("Finished reading: " + len + " bytes");
  122. }
  123. for (int i = 0; i < 5; i++) {
  124. res [i].Close ();
  125. }
  126. }
  127. // Debug code not used now, but could be useful later
  128. /*
  129. private void WriteServicePoint (string label, ServicePoint sp)
  130. {
  131. Console.WriteLine ("\n" + label);
  132. Console.WriteLine ("Address: " + sp.Address);
  133. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  134. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  135. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  136. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  137. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  138. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  139. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  140. }
  141. */
  142. }
  143. }