ServicePointManagerTest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. [SetUp]
  25. public void GetReady ()
  26. {
  27. googleUri = new Uri ("http://www.google.com");
  28. yahooUri = new Uri ("http://www.yahoo.com");
  29. apacheUri = new Uri ("http://www.apache.org");
  30. }
  31. [Test]
  32. public void MaxServicePointManagers ()
  33. {
  34. try {
  35. Assertion.AssertEquals ("#1", 0, ServicePointManager.MaxServicePoints);
  36. DoWebRequest (googleUri);
  37. Thread.Sleep (100);
  38. DoWebRequest (yahooUri);
  39. Thread.Sleep (100);
  40. DoWebRequest (apacheUri);
  41. Thread.Sleep (100);
  42. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);
  43. WriteServicePoint (sp);
  44. sp = ServicePointManager.FindServicePoint (yahooUri);
  45. WriteServicePoint (sp);
  46. sp = ServicePointManager.FindServicePoint (apacheUri);
  47. WriteServicePoint (sp);
  48. ServicePointManager.MaxServicePoints = 1;
  49. sp = ServicePointManager.FindServicePoint (googleUri);
  50. WriteServicePoint (sp);
  51. sp = ServicePointManager.FindServicePoint (yahooUri);
  52. WriteServicePoint (sp);
  53. sp = ServicePointManager.FindServicePoint (apacheUri);
  54. WriteServicePoint (sp);
  55. GC.Collect ();
  56. // hmm... aparently ms.net still has the service points even
  57. // though I set it to a max of 1.
  58. // this should force an exception then...
  59. sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));
  60. WriteServicePoint (sp);
  61. } catch (Exception e) {
  62. Assertion.Fail("The following unexpected Exception was thrown : " + e);
  63. }
  64. }
  65. [Test]
  66. public void FindServicePoint ()
  67. {
  68. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));
  69. Assertion.AssertEquals ("#1", apacheUri, sp.Address);
  70. Assertion.AssertEquals ("#2", 2, sp.ConnectionLimit);
  71. Assertion.AssertEquals ("#3", "http", sp.ConnectionName);
  72. }
  73. private void DoWebRequest (Uri uri)
  74. {
  75. WebRequest.Create (uri).GetResponse ().Close ();
  76. }
  77. private void WriteServicePoint (ServicePoint sp)
  78. {
  79. Console.WriteLine ("\nAddress: " + sp.Address);
  80. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  81. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  82. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  83. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  84. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  85. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  86. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  87. }
  88. }
  89. }