8922c14f00e65e1afe983af6cd8aa7b6d1a0685c
[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         public void MaxServicePointManagers ()\r
48         {\r
49                 Assert.AreEqual (0, ServicePointManager.MaxServicePoints, "#1");\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                 Assert.AreEqual (apacheUri, sp.Address, "#1");\r
90 #if MOBILE && !MONODROID\r
91                 Assert.AreEqual (10, sp.ConnectionLimit, "#2");\r
92 #else\r
93                 Assert.AreEqual (2, sp.ConnectionLimit, "#2");\r
94 #endif\r
95                 Assert.AreEqual ("http", sp.ConnectionName, "#3");\r
96         }\r
97         \r
98         private void DoWebRequest (Uri uri)\r
99         {\r
100                 WebRequest.Create (uri).GetResponse ().Close ();\r
101         }\r
102 \r
103 /* Unused code for now, but might be useful later for debugging\r
104         private void WriteServicePoint (ServicePoint sp)\r
105         {\r
106                 Console.WriteLine ("\nAddress: " + sp.Address);\r
107                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
108                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
109                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
110                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
111                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
112                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
113                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
114         }\r
115 */\r
116 \r
117 }\r
118 }\r
119 \r