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