ServicePointManagerTest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // ServicePointManagerTest.cs - NUnit Test Cases for System.Net.ServicePointManager
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Collections;
  10. using System.IO;
  11. using System.Net;
  12. using System.Threading;
  13. namespace MonoTests.System.Net
  14. {
  15. public class ServicePointManagerTest : TestCase
  16. {
  17. private Uri googleUri;
  18. private Uri yahooUri;
  19. private Uri apacheUri;
  20. public ServicePointManagerTest () :
  21. base ("[MonoTests.System.Net.ServicePointManagerTest]") {}
  22. public ServicePointManagerTest (string name) : base (name) {}
  23. protected override void SetUp () {
  24. googleUri = new Uri ("http://www.google.com");
  25. yahooUri = new Uri ("http://www.yahoo.com");
  26. apacheUri = new Uri ("http://www.apache.org");
  27. }
  28. protected override void TearDown () {}
  29. public static ITest Suite
  30. {
  31. get {
  32. return new TestSuite (typeof (ServicePointManagerTest));
  33. }
  34. }
  35. public void TestMaxServicePointManagers ()
  36. {
  37. try {
  38. AssertEquals ("#1", 0, ServicePointManager.MaxServicePoints);
  39. DoWebRequest (googleUri);
  40. Thread.Sleep (100);
  41. DoWebRequest (yahooUri);
  42. Thread.Sleep (100);
  43. DoWebRequest (apacheUri);
  44. Thread.Sleep (100);
  45. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);
  46. WriteServicePoint (sp);
  47. sp = ServicePointManager.FindServicePoint (yahooUri);
  48. WriteServicePoint (sp);
  49. sp = ServicePointManager.FindServicePoint (apacheUri);
  50. WriteServicePoint (sp);
  51. ServicePointManager.MaxServicePoints = 1;
  52. sp = ServicePointManager.FindServicePoint (googleUri);
  53. WriteServicePoint (sp);
  54. sp = ServicePointManager.FindServicePoint (yahooUri);
  55. WriteServicePoint (sp);
  56. sp = ServicePointManager.FindServicePoint (apacheUri);
  57. WriteServicePoint (sp);
  58. GC.Collect ();
  59. // hmm... aparently ms.net still has the service points even
  60. // though I set it to a max of 1.
  61. // this should force an exception then...
  62. sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));
  63. WriteServicePoint (sp);
  64. } catch (Exception e) {
  65. Fail("The following unexpected Exception was thrown : " + e);
  66. }
  67. }
  68. public void TestFindServicePoint ()
  69. {
  70. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));
  71. AssertEquals ("#1", apacheUri, sp.Address);
  72. AssertEquals ("#2", 2, sp.ConnectionLimit);
  73. AssertEquals ("#3", "http", sp.ConnectionName);
  74. }
  75. private void DoWebRequest (Uri uri)
  76. {
  77. WebRequest.Create (uri).GetResponse ().Close ();
  78. }
  79. private void WriteServicePoint (ServicePoint sp)
  80. {
  81. Console.WriteLine ("\nAddress: " + sp.Address);
  82. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  83. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  84. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  85. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  86. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  87. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  88. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  89. }
  90. }
  91. }