ServicePointManagerTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 ("NotWorking")]
  45. [Category ("InetAccess")]
  46. public void MaxServicePointManagers ()
  47. {
  48. Assert.AreEqual (0, ServicePointManager.MaxServicePoints, "#1");
  49. DoWebRequest (googleUri);
  50. Thread.Sleep (100);
  51. DoWebRequest (yahooUri);
  52. Thread.Sleep (100);
  53. DoWebRequest (apacheUri);
  54. Thread.Sleep (100);
  55. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);
  56. //WriteServicePoint (sp);
  57. sp = ServicePointManager.FindServicePoint (yahooUri);
  58. //WriteServicePoint (sp);
  59. sp = ServicePointManager.FindServicePoint (apacheUri);
  60. //WriteServicePoint (sp);
  61. ServicePointManager.MaxServicePoints = 1;
  62. sp = ServicePointManager.FindServicePoint (googleUri);
  63. //WriteServicePoint (sp);
  64. sp = ServicePointManager.FindServicePoint (yahooUri);
  65. //WriteServicePoint (sp);
  66. sp = ServicePointManager.FindServicePoint (apacheUri);
  67. //WriteServicePoint (sp);
  68. GC.Collect ();
  69. // hmm... aparently ms.net still has the service points even
  70. // though I set it to a max of 1.
  71. // this should force an exception then...
  72. sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));
  73. //WriteServicePoint (sp);
  74. }
  75. [Test]
  76. [Category ("InetAccess")]
  77. #if FEATURE_NO_BSD_SOCKETS
  78. [ExpectedException (typeof (PlatformNotSupportedException))]
  79. #endif
  80. public void FindServicePoint ()
  81. {
  82. ServicePointManager.MaxServicePoints = 0;
  83. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));
  84. Assert.AreEqual (apacheUri, sp.Address, "#1");
  85. #if MOBILE
  86. Assert.AreEqual (10, sp.ConnectionLimit, "#2");
  87. #else
  88. Assert.AreEqual (2, sp.ConnectionLimit, "#2");
  89. #endif
  90. Assert.AreEqual ("http", sp.ConnectionName, "#3");
  91. }
  92. private void DoWebRequest (Uri uri)
  93. {
  94. WebRequest.Create (uri).GetResponse ().Close ();
  95. }
  96. /* Unused code for now, but might be useful later for debugging
  97. private void WriteServicePoint (ServicePoint sp)
  98. {
  99. Console.WriteLine ("\nAddress: " + sp.Address);
  100. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  101. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  102. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  103. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  104. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  105. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  106. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  107. }
  108. */
  109. }
  110. }