ServicePointManagerTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // ServicePointManagerTest.cs - NUnit Test Cases for System.Net.ServicePointManager
  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 ServicePointManagerTest
  20. {
  21. private Uri googleUri;
  22. private Uri yahooUri;
  23. private Uri apacheUri;
  24. private int maxIdle;
  25. [SetUp]
  26. public void GetReady ()
  27. {
  28. #if !FEATURE_NO_BSD_SOCKETS
  29. maxIdle = ServicePointManager.MaxServicePointIdleTime;
  30. ServicePointManager.MaxServicePointIdleTime = 10;
  31. #endif
  32. googleUri = new Uri ("http://www.google.com");
  33. yahooUri = new Uri ("http://www.yahoo.com");
  34. apacheUri = new Uri ("http://www.apache.org");
  35. }
  36. [TearDown]
  37. public void Finish ()
  38. {
  39. #if !FEATURE_NO_BSD_SOCKETS
  40. ServicePointManager.MaxServicePointIdleTime = maxIdle;
  41. #endif
  42. }
  43. [Test, ExpectedException (typeof (InvalidOperationException))]
  44. [Category ("InetAccess")]
  45. public void MaxServicePointManagers ()
  46. {
  47. Assert.AreEqual (0, ServicePointManager.MaxServicePoints, "#1");
  48. DoWebRequest (googleUri);
  49. Thread.Sleep (100);
  50. DoWebRequest (yahooUri);
  51. Thread.Sleep (100);
  52. DoWebRequest (apacheUri);
  53. Thread.Sleep (100);
  54. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);
  55. //WriteServicePoint (sp);
  56. sp = ServicePointManager.FindServicePoint (yahooUri);
  57. //WriteServicePoint (sp);
  58. sp = ServicePointManager.FindServicePoint (apacheUri);
  59. //WriteServicePoint (sp);
  60. ServicePointManager.MaxServicePoints = 1;
  61. sp = ServicePointManager.FindServicePoint (googleUri);
  62. //WriteServicePoint (sp);
  63. sp = ServicePointManager.FindServicePoint (yahooUri);
  64. //WriteServicePoint (sp);
  65. sp = ServicePointManager.FindServicePoint (apacheUri);
  66. //WriteServicePoint (sp);
  67. GC.Collect ();
  68. // hmm... aparently ms.net still has the service points even
  69. // though I set it to a max of 1.
  70. // this should force an exception then...
  71. sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));
  72. //WriteServicePoint (sp);
  73. }
  74. [Test]
  75. #if FEATURE_NO_BSD_SOCKETS
  76. [ExpectedException (typeof (PlatformNotSupportedException))]
  77. #endif
  78. public void FindServicePoint ()
  79. {
  80. ServicePointManager.MaxServicePoints = 0;
  81. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));
  82. Assert.AreEqual (apacheUri, sp.Address, "#1");
  83. #if MOBILE
  84. Assert.AreEqual (10, sp.ConnectionLimit, "#2");
  85. #else
  86. Assert.AreEqual (2, sp.ConnectionLimit, "#2");
  87. #endif
  88. Assert.AreEqual ("http", sp.ConnectionName, "#3");
  89. }
  90. private void DoWebRequest (Uri uri)
  91. {
  92. WebRequest.Create (uri).GetResponse ().Close ();
  93. }
  94. /* Unused code for now, but might be useful later for debugging
  95. private void WriteServicePoint (ServicePoint sp)
  96. {
  97. Console.WriteLine ("\nAddress: " + sp.Address);
  98. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  99. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  100. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  101. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  102. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  103. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  104. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  105. }
  106. */
  107. }
  108. }