346f35bdeacd358202f49bad9a130cef91bd0066
[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.Reflection;
17 using System.Threading;\r
18 \r
19 namespace MonoTests.System.Net\r
20 {\r
21 \r
22 [TestFixture]\r
23 public class ServicePointTest\r
24 {\r
25         static private int max;\r
26         [SetUp]\r
27         public void SaveMax () {\r
28                 max = ServicePointManager.MaxServicePoints;\r
29                 ServicePointManager.MaxServicePoints = 0;\r
30         }\r
31 \r
32         [TearDown]\r
33         public void RestoreMax () {\r
34                 ServicePointManager.MaxServicePoints = max;\r
35         }\r
36 \r
37         [Test]\r
38                 [Category ("InetAccess")]\r
39         public void All ()\r
40         {\r
41                 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));\r
42                 //WriteServicePoint ("A servicepoint that isn't really", p);                    \r
43                 \r
44                 ServicePointManager.MaxServicePoints = 2;\r
45                 ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));\r
46                 try {                   \r
47                         ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));\r
48                         Assert.Fail ("#1");\r
49                 } catch (InvalidOperationException) { }\r
50                 ServicePointManager.MaxServicePoints = 0;\r
51                 \r
52                 //WriteServicePoint ("google before getting a webrequest", google);\r
53                 \r
54                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
55                 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();                     \r
56                 \r
57 #if FOUND_SOME_OTHER_URL
58                 // URL is no longer found, disabled the test until a more reliable URL is found :P
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 #endif
64                 \r
65                 // in both instances property CurrentConnections is 0 according to ms.net.\r
66                 // let's see what it says when we do async operations...\r
67                 \r
68                 HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
69                 req2.Method = "PUT";\r
70                 IAsyncResult async = req2.BeginGetRequestStream (null, null);\r
71                 //WriteServicePoint ("after async BeginGetRequestStream", google);\r
72                 // CurrentConnections: 1\r
73                 Stream stream2 = req2.EndGetRequestStream (async);\r
74                 //WriteServicePoint ("after async EndGetRequestStream", google);\r
75                 // CurrentConnections: 1\r
76                 stream2.Close ();\r
77                 \r
78                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
79                 async = req2.BeginGetResponse (null, null);\r
80                 //WriteServicePoint ("after async BeginGetResponse", google);\r
81                 // CurrentConnections: 2\r
82                 WebResponse res2 = req2.EndGetResponse (async);\r
83                 //WriteServicePoint ("after async EndGetResponse", google);\r
84                 // CurrentConnections: 0                        \r
85                 // curious that after you get the webresponse object CurrentConnections is set to 0.\r
86                 // you'd think that you'd still be connected until you close the webresponse..\r
87                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
88                 res2.Close ();\r
89                 \r
90                 ServicePoint sp2;
91 #if FOUND_SOME_OTHER_URL
92                 // unless of course some buffering is taking place.. let's check\r
93                 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");\r
94                 sp2 = ServicePointManager.FindServicePoint (uri2);\r
95                 req2 = (HttpWebRequest) WebRequest.Create (uri2);\r
96                 async = req2.BeginGetResponse (null, null);\r
97                 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);\r
98                 // CurrentConnections: 1\r
99                 res2 = req2.EndGetResponse (async);\r
100                 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);\r
101                 // CurrentConnections: 1\r
102                 // and so it shows\r
103                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
104                 res2.Close ();\r
105 #endif
106                 \r
107                 \r
108                 // what's the limit of the cache?\r
109                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");\r
110                 res2 = req2.GetResponse ();\r
111                 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));\r
112                 //WriteServicePoint ("apache", sp2);\r
113                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
114                 // CurrentConnections: 1\r
115                 res2.Close ();\r
116                 // curious other effect: address is actually the full Uri of the previous request\r
117                 // anyways, buffer is probably 4096 bytes\r
118         }\r
119 \r
120         // try getting the stream to 5 web response objects     \r
121         // while ConnectionLimit equals 2\r
122 \r
123         [Test]\r
124         [Category ("InetAccess")]\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         [Test]\r
156         [Category ("InetAccess")]\r
157         [Category ("AndroidNotWorking")] // #A1 fails
158         public void EndPointBind ()\r
159         {\r
160                 Uri uri = new Uri ("http://www.go-mono.com/");\r
161                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);\r
162 \r
163                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
164 \r
165                 bool called = false;\r
166                 sp.BindIPEndPointDelegate = delegate {\r
167                         Assert.IsTrue (!called);\r
168                         called = true;\r
169                         return null;\r
170                 };\r
171                 req.GetResponse ().Close ();\r
172 \r
173                 Assert.IsTrue (called, "#A1");\r
174 \r
175                 req = (HttpWebRequest) WebRequest.Create (uri);\r
176                 called = false;\r
177                 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {\r
178                         Assert.IsTrue (times < 5);\r
179                         called = true;\r
180                         return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);\r
181                 };\r
182                 req.GetResponse ().Close ();\r
183 \r
184                 Assert.IsTrue (called, "#A2");\r
185         }\r
186 \r
187         public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)\r
188         {\r
189         }\r
190 \r
191         [Test] //Covers #19823\r
192         public void CloseConnectionGroupConcurency ()\r
193         {\r
194                 // Try with multiple service points\r
195                 for (var i = 0; i < 10; i++) {\r
196                         Uri targetUri = new Uri ("http://" + i + ".mono-project.com");\r
197                         var req = (HttpWebRequest) HttpWebRequest.Create (targetUri);\r
198                         req.ContentType = "application/x-www-form-urlencoded";\r
199                         req.Method = "POST";\r
200                         req.ConnectionGroupName = "" + i;\r
201                         req.ServicePoint.MaxIdleTime = 1;\r
202 \r
203                         req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);\r
204                         Thread.Sleep (1);\r
205                         req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);\r
206                 }\r
207         }\r
208 \r
209
210         [Test]
211         public void DnsRefreshTimeout ()
212         {
213                 const int dnsRefreshTimeout = 2000;
214
215                 ServicePoint sp;
216                 IPHostEntry host0, host1, host2;
217                 Uri uri;
218                 PropertyInfo hostEntryProperty;
219
220                 ServicePointManager.DnsRefreshTimeout = dnsRefreshTimeout;
221
222                 uri = new Uri ("http://localhost/");
223                 sp = ServicePointManager.FindServicePoint (uri);
224
225                 hostEntryProperty = typeof (ServicePoint).GetProperty ("HostEntry", BindingFlags.NonPublic | BindingFlags.Instance);
226
227                 host0 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
228                 host1 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
229
230                 Assert.AreSame (host0, host1, "HostEntry should result in the same IPHostEntry object.");
231
232                 Thread.Sleep (dnsRefreshTimeout * 2);
233                 host2 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
234
235                 Assert.AreNotSame(host0, host2, "HostEntry should result in a new IPHostEntry " +
236                                 "object when DnsRefreshTimeout is reached.");
237         }
238
239 // Debug code not used now, but could be useful later\r
240 /*\r
241         private void WriteServicePoint (string label, ServicePoint sp)\r
242         {\r
243                 Console.WriteLine ("\n" + label);\r
244                 Console.WriteLine ("Address: " + sp.Address);\r
245                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
246                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
247                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
248                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
249                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
250                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
251                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
252         }\r
253 */\r
254 }\r
255 }\r
256 \r