New test.
[mono.git] / mcs / class / System / Test / System.Net / ServicePointManagerTest.cs
1 //\r
2 // ServicePointManagerTest.cs - NUnit Test Cases for System.Net.ServicePointManager\r
3 //\r
4 // Authors:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)\r
7 //\r
8 // (C) 2003 Martin Willemoes Hansen\r
9 //\r
10 \r
11 using NUnit.Framework;\r
12 using System;\r
13 using System.Collections;\r
14 using System.IO;\r
15 using System.Net;\r
16 using System.Threading;\r
17 \r
18 namespace MonoTests.System.Net\r
19 {\r
20 \r
21 [TestFixture]\r
22 public class ServicePointManagerTest : Assertion\r
23 {\r
24         private Uri googleUri;\r
25         private Uri yahooUri;\r
26         private Uri apacheUri;\r
27         private int maxIdle;\r
28         \r
29         [SetUp]\r
30         public void GetReady () \r
31         {\r
32                 maxIdle = ServicePointManager.MaxServicePointIdleTime;\r
33                 ServicePointManager.MaxServicePointIdleTime = 10;\r
34                 googleUri = new Uri ("http://www.google.com");\r
35                 yahooUri = new Uri ("http://www.yahoo.com");\r
36                 apacheUri = new Uri ("http://www.apache.org");\r
37         }\r
38 \r
39         [TearDown]\r
40         public void Finish ()\r
41         {\r
42                 ServicePointManager.MaxServicePointIdleTime = maxIdle;\r
43         }\r
44 \r
45         [Test, ExpectedException (typeof (InvalidOperationException))]\r
46                 [Category ("InetAccess")]\r
47         public void MaxServicePointManagers ()\r
48         {\r
49                 AssertEquals ("#1", 0, ServicePointManager.MaxServicePoints);\r
50                 \r
51                 DoWebRequest (googleUri);\r
52                 Thread.Sleep (100);\r
53                 DoWebRequest (yahooUri);\r
54                 Thread.Sleep (100);\r
55                 DoWebRequest (apacheUri);\r
56                 Thread.Sleep (100);\r
57                 \r
58                 ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);\r
59                 //WriteServicePoint (sp);\r
60                 sp = ServicePointManager.FindServicePoint (yahooUri);\r
61                 //WriteServicePoint (sp);\r
62                 sp = ServicePointManager.FindServicePoint (apacheUri);\r
63                 //WriteServicePoint (sp);\r
64                 \r
65                 ServicePointManager.MaxServicePoints = 1;\r
66 \r
67                 sp = ServicePointManager.FindServicePoint (googleUri);\r
68                 //WriteServicePoint (sp);\r
69                 sp = ServicePointManager.FindServicePoint (yahooUri);\r
70                 //WriteServicePoint (sp);\r
71                 sp = ServicePointManager.FindServicePoint (apacheUri);\r
72                 //WriteServicePoint (sp);\r
73                 \r
74                 GC.Collect ();\r
75                 \r
76                 // hmm... aparently ms.net still has the service points even\r
77                 // though I set it to a max of 1.\r
78                 \r
79                 // this should force an exception then...               \r
80                 sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));\r
81                 //WriteServicePoint (sp);\r
82         }\r
83         \r
84         [Test]\r
85         public void FindServicePoint ()\r
86         {\r
87                 ServicePointManager.MaxServicePoints = 0;\r
88                 ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));\r
89                 AssertEquals ("#1", apacheUri, sp.Address);\r
90                 AssertEquals ("#2", 2, sp.ConnectionLimit);\r
91                 AssertEquals ("#3", "http", sp.ConnectionName);\r
92         }\r
93         \r
94         private void DoWebRequest (Uri uri)\r
95         {\r
96                 WebRequest.Create (uri).GetResponse ().Close ();\r
97         }\r
98 \r
99 /* Unused code for now, but might be useful later for debugging\r
100         private void WriteServicePoint (ServicePoint sp)\r
101         {\r
102                 Console.WriteLine ("\nAddress: " + sp.Address);\r
103                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
104                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
105                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
106                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
107                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
108                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
109                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
110         }\r
111 */\r
112 \r
113 }\r
114 }\r
115 \r