[System*] Throw a PlatformNotSupported exception when using the managed networking...
[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 #if !FEATURE_NO_BSD_SOCKETS\r
33                 maxIdle = ServicePointManager.MaxServicePointIdleTime;\r
34                 ServicePointManager.MaxServicePointIdleTime = 10;\r
35 #endif\r
36                 googleUri = new Uri ("http://www.google.com");\r
37                 yahooUri = new Uri ("http://www.yahoo.com");\r
38                 apacheUri = new Uri ("http://www.apache.org");\r
39         }\r
40 \r
41         [TearDown]\r
42         public void Finish ()\r
43         {\r
44 #if !FEATURE_NO_BSD_SOCKETS\r
45                 ServicePointManager.MaxServicePointIdleTime = maxIdle;\r
46 #endif\r
47         }\r
48 \r
49         [Test, ExpectedException (typeof (InvalidOperationException))]\r
50                 [Category ("InetAccess")]\r
51         public void MaxServicePointManagers ()\r
52         {\r
53                 Assert.AreEqual (0, ServicePointManager.MaxServicePoints, "#1");\r
54                 \r
55                 DoWebRequest (googleUri);\r
56                 Thread.Sleep (100);\r
57                 DoWebRequest (yahooUri);\r
58                 Thread.Sleep (100);\r
59                 DoWebRequest (apacheUri);\r
60                 Thread.Sleep (100);\r
61                 \r
62                 ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);\r
63                 //WriteServicePoint (sp);\r
64                 sp = ServicePointManager.FindServicePoint (yahooUri);\r
65                 //WriteServicePoint (sp);\r
66                 sp = ServicePointManager.FindServicePoint (apacheUri);\r
67                 //WriteServicePoint (sp);\r
68                 \r
69                 ServicePointManager.MaxServicePoints = 1;\r
70 \r
71                 sp = ServicePointManager.FindServicePoint (googleUri);\r
72                 //WriteServicePoint (sp);\r
73                 sp = ServicePointManager.FindServicePoint (yahooUri);\r
74                 //WriteServicePoint (sp);\r
75                 sp = ServicePointManager.FindServicePoint (apacheUri);\r
76                 //WriteServicePoint (sp);\r
77                 \r
78                 GC.Collect ();\r
79                 \r
80                 // hmm... aparently ms.net still has the service points even\r
81                 // though I set it to a max of 1.\r
82                 \r
83                 // this should force an exception then...               \r
84                 sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));\r
85                 //WriteServicePoint (sp);\r
86         }\r
87         \r
88         [Test]\r
89 #if FEATURE_NO_BSD_SOCKETS\r
90         [ExpectedException (typeof (PlatformNotSupportedException))]\r
91 #endif\r
92         public void FindServicePoint ()\r
93         {\r
94                 ServicePointManager.MaxServicePoints = 0;\r
95                 ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));\r
96                 Assert.AreEqual (apacheUri, sp.Address, "#1");\r
97 #if MOBILE && !MONODROID\r
98                 Assert.AreEqual (10, sp.ConnectionLimit, "#2");\r
99 #else\r
100                 Assert.AreEqual (2, sp.ConnectionLimit, "#2");\r
101 #endif\r
102                 Assert.AreEqual ("http", sp.ConnectionName, "#3");\r
103         }\r
104         \r
105         private void DoWebRequest (Uri uri)\r
106         {\r
107                 WebRequest.Create (uri).GetResponse ().Close ();\r
108         }\r
109 \r
110 /* Unused code for now, but might be useful later for debugging\r
111         private void WriteServicePoint (ServicePoint sp)\r
112         {\r
113                 Console.WriteLine ("\nAddress: " + sp.Address);\r
114                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
115                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
116                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
117                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
118                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
119                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
120                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
121         }\r
122 */\r
123 \r
124 }\r
125 }\r
126 \r