457dc5b29f1ec4e1daeb3553b813d97e0f23d5a9
[mono.git] / mcs / class / System / Test / System.Net / ServicePointTest.cs
1 //\r
2 // ServicePointTest.cs - NUnit Test Cases for System.Net.ServicePoint\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 ServicePointTest\r
23 {\r
24         static private int max;\r
25         [SetUp]\r
26         public void SaveMax () {\r
27                 max = ServicePointManager.MaxServicePoints;\r
28                 ServicePointManager.MaxServicePoints = 0;\r
29         }\r
30 \r
31         [TearDown]\r
32         public void RestoreMax () {\r
33                 ServicePointManager.MaxServicePoints = max;\r
34         }\r
35 \r
36         [Test]\r
37                 [Category ("InetAccess")]\r
38 #if TARGET_JVM\r
39         [Ignore ("Unsupported - ServicePointManager.FindServicePoint")]\r
40 #endif\r
41         public void All ()\r
42         {\r
43                 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));\r
44                 //WriteServicePoint ("A servicepoint that isn't really", p);                    \r
45                 \r
46                 ServicePointManager.MaxServicePoints = 2;\r
47                 ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));\r
48                 try {                   \r
49                         ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));\r
50                         Assert.Fail ("#1");\r
51                 } catch (InvalidOperationException) { }\r
52                 ServicePointManager.MaxServicePoints = 0;\r
53                 \r
54                 //WriteServicePoint ("google before getting a webrequest", google);\r
55                 \r
56                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
57                 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();                     \r
58                 \r
59                 //WriteServicePoint ("google after getting a response", google);\r
60                 ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));\r
61                 Assert.AreEqual (google, google2, "#equals");\r
62                 res.Close ();\r
63                 \r
64                 // in both instances property CurrentConnections is 0 according to ms.net.\r
65                 // let's see what it says when we do async operations...\r
66                 \r
67                 HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
68                 req2.Method = "PUT";\r
69                 IAsyncResult async = req2.BeginGetRequestStream (null, null);\r
70                 //WriteServicePoint ("after async BeginGetRequestStream", google);\r
71                 // CurrentConnections: 1\r
72                 Stream stream2 = req2.EndGetRequestStream (async);\r
73                 //WriteServicePoint ("after async EndGetRequestStream", google);\r
74                 // CurrentConnections: 1\r
75                 stream2.Close ();\r
76                 \r
77                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
78                 async = req2.BeginGetResponse (null, null);\r
79                 //WriteServicePoint ("after async BeginGetResponse", google);\r
80                 // CurrentConnections: 2\r
81                 WebResponse res2 = req2.EndGetResponse (async);\r
82                 //WriteServicePoint ("after async EndGetResponse", google);\r
83                 // CurrentConnections: 0                        \r
84                 // curious that after you get the webresponse object CurrentConnections is set to 0.\r
85                 // you'd think that you'd still be connected until you close the webresponse..\r
86                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
87                 res2.Close ();\r
88                 \r
89                 \r
90                 // unless of course some buffering is taking place.. let's check\r
91                 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");\r
92                 ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);\r
93                 req2 = (HttpWebRequest) WebRequest.Create (uri2);\r
94                 async = req2.BeginGetResponse (null, null);\r
95                 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);\r
96                 // CurrentConnections: 1\r
97                 res2 = req2.EndGetResponse (async);\r
98                 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);\r
99                 // CurrentConnections: 1\r
100                 // and so it shows\r
101                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
102                 res2.Close ();\r
103                 \r
104                 \r
105                 // what's the limit of the cache?\r
106                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");\r
107                 res2 = req2.GetResponse ();\r
108                 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));\r
109                 //WriteServicePoint ("apache", sp2);\r
110                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
111                 // CurrentConnections: 1\r
112                 res2.Close ();\r
113                 // curious other effect: address is actually the full Uri of the previous request\r
114                 // anyways, buffer is probably 4096 bytes\r
115         }\r
116 \r
117         // try getting the stream to 5 web response objects     \r
118         // while ConnectionLimit equals 2\r
119 \r
120         [Test]\r
121         [Category ("InetAccess")]\r
122 #if TARGET_JVM\r
123         [Ignore ("The System.Net.ServicePointManager.FindServicePoint(Uri) is not supported")]\r
124 #endif  \r
125         public void ConnectionLimit ()\r
126         {               \r
127                 // the default is already 2, just in case it isn't..\r
128                 ServicePointManager.DefaultConnectionLimit = 5;\r
129                 \r
130                 Uri uri = new Uri ("http://www.go-mono.com/");\r
131                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);                   \r
132                 WebResponse [] res = new WebResponse [5];\r
133                 for (int i = 0; i < 5; i++) {\r
134                         //Console.WriteLine ("GOT1 : " + i);\r
135                         HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
136                         //Console.WriteLine ("GOT2 : " + i);\r
137                         res [i] = req.GetResponse ();\r
138                         //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);\r
139                 }\r
140                 \r
141                 for (int i = 0; i < 5; i++) {\r
142                         Stream stream = res [i].GetResponseStream();\r
143                         //Console.WriteLine ("Reading stream: " + i + " : " + stream);\r
144                         int len = 0;\r
145                         while (stream.ReadByte () != -1)\r
146                                 len++;\r
147                         //Console.WriteLine ("Finished reading: " + len + " bytes");\r
148                 }\r
149                 \r
150                 for (int i = 0; i < 5; i++) {\r
151                         res [i].Close ();\r
152                 }\r
153         }\r
154 \r
155 #if NET_2_0\r
156         [Test]\r
157         [Category ("InetAccess")]\r
158 #if TARGET_JVM\r
159         [Ignore ("The System.Net.ServicePointManager.FindServicePoint(Uri) is not supported")]\r
160 #endif  \r
161         public void EndPointBind ()\r
162         {\r
163                 Uri uri = new Uri ("http://www.go-mono.com/");\r
164                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);\r
165 \r
166                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
167 \r
168                 bool called = false;\r
169 #if !TARGET_JVM\r
170                 sp.BindIPEndPointDelegate = delegate {\r
171                         Assert.IsTrue (!called);\r
172                         called = true;\r
173                         return null;\r
174                 };\r
175 #endif\r
176                 req.GetResponse ().Close ();\r
177 \r
178                 Assert.IsTrue (called);\r
179 \r
180                 req = (HttpWebRequest) WebRequest.Create (uri);\r
181                 called = false;\r
182 #if !TARGET_JVM\r
183                 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {\r
184                         Assert.IsTrue (times < 5);\r
185                         called = true;\r
186                         return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);\r
187                 };\r
188 #endif\r
189                 req.GetResponse ().Close ();\r
190 \r
191                 Assert.IsTrue (called);\r
192         }\r
193 #endif\r
194 \r
195 // Debug code not used now, but could be useful later\r
196 /*\r
197         private void WriteServicePoint (string label, ServicePoint sp)\r
198         {\r
199                 Console.WriteLine ("\n" + label);\r
200                 Console.WriteLine ("Address: " + sp.Address);\r
201                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
202                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
203                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
204                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
205                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
206                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
207                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
208         }\r
209 */\r
210 }\r
211 }\r
212 \r