Merge pull request #1909 from esdrubal/reflection
[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         public void All ()\r
39         {\r
40                 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));\r
41                 //WriteServicePoint ("A servicepoint that isn't really", p);                    \r
42                 \r
43                 ServicePointManager.MaxServicePoints = 2;\r
44                 ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));\r
45                 try {                   \r
46                         ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));\r
47                         Assert.Fail ("#1");\r
48                 } catch (InvalidOperationException) { }\r
49                 ServicePointManager.MaxServicePoints = 0;\r
50                 \r
51                 //WriteServicePoint ("google before getting a webrequest", google);\r
52                 \r
53                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
54                 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();                     \r
55                 \r
56 #if FOUND_SOME_OTHER_URL
57                 // URL is no longer found, disabled the test until a more reliable URL is found :P
58                 //WriteServicePoint ("google after getting a response", google);\r
59                 ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));\r
60                 Assert.AreEqual (google, google2, "#equals");\r
61                 res.Close ();\r
62 #endif
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                 ServicePoint sp2;
90 #if FOUND_SOME_OTHER_URL
91                 // unless of course some buffering is taking place.. let's check\r
92                 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");\r
93                 sp2 = ServicePointManager.FindServicePoint (uri2);\r
94                 req2 = (HttpWebRequest) WebRequest.Create (uri2);\r
95                 async = req2.BeginGetResponse (null, null);\r
96                 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);\r
97                 // CurrentConnections: 1\r
98                 res2 = req2.EndGetResponse (async);\r
99                 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);\r
100                 // CurrentConnections: 1\r
101                 // and so it shows\r
102                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
103                 res2.Close ();\r
104 #endif
105                 \r
106                 \r
107                 // what's the limit of the cache?\r
108                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");\r
109                 res2 = req2.GetResponse ();\r
110                 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));\r
111                 //WriteServicePoint ("apache", sp2);\r
112                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
113                 // CurrentConnections: 1\r
114                 res2.Close ();\r
115                 // curious other effect: address is actually the full Uri of the previous request\r
116                 // anyways, buffer is probably 4096 bytes\r
117         }\r
118 \r
119         // try getting the stream to 5 web response objects     \r
120         // while ConnectionLimit equals 2\r
121 \r
122         [Test]\r
123         [Category ("InetAccess")]\r
124         public void ConnectionLimit ()\r
125         {               \r
126                 // the default is already 2, just in case it isn't..\r
127                 ServicePointManager.DefaultConnectionLimit = 5;\r
128                 \r
129                 Uri uri = new Uri ("http://www.go-mono.com/");\r
130                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);                   \r
131                 WebResponse [] res = new WebResponse [5];\r
132                 for (int i = 0; i < 5; i++) {\r
133                         //Console.WriteLine ("GOT1 : " + i);\r
134                         HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
135                         //Console.WriteLine ("GOT2 : " + i);\r
136                         res [i] = req.GetResponse ();\r
137                         //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);\r
138                 }\r
139                 \r
140                 for (int i = 0; i < 5; i++) {\r
141                         Stream stream = res [i].GetResponseStream();\r
142                         //Console.WriteLine ("Reading stream: " + i + " : " + stream);\r
143                         int len = 0;\r
144                         while (stream.ReadByte () != -1)\r
145                                 len++;\r
146                         //Console.WriteLine ("Finished reading: " + len + " bytes");\r
147                 }\r
148                 \r
149                 for (int i = 0; i < 5; i++) {\r
150                         res [i].Close ();\r
151                 }\r
152         }\r
153 \r
154         [Test]\r
155         [Category ("InetAccess")]\r
156         [Category ("AndroidNotWorking")] // #A1 fails
157         public void EndPointBind ()\r
158         {\r
159                 Uri uri = new Uri ("http://www.go-mono.com/");\r
160                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);\r
161 \r
162                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
163 \r
164                 bool called = false;\r
165                 sp.BindIPEndPointDelegate = delegate {\r
166                         Assert.IsTrue (!called);\r
167                         called = true;\r
168                         return null;\r
169                 };\r
170                 req.GetResponse ().Close ();\r
171 \r
172                 Assert.IsTrue (called, "#A1");\r
173 \r
174                 req = (HttpWebRequest) WebRequest.Create (uri);\r
175                 called = false;\r
176                 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {\r
177                         Assert.IsTrue (times < 5);\r
178                         called = true;\r
179                         return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);\r
180                 };\r
181                 req.GetResponse ().Close ();\r
182 \r
183                 Assert.IsTrue (called, "#A2");\r
184         }\r
185 \r
186         public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)\r
187         {\r
188         }\r
189 \r
190         [Test] //Covers #19823\r
191         public void CloseConnectionGroupConcurency ()\r
192         {\r
193                 // Try with multiple service points\r
194                 for (var i = 0; i < 10; i++) {\r
195                         Uri targetUri = new Uri ("http://" + i + ".mono-project.com");\r
196                         var req = (HttpWebRequest) HttpWebRequest.Create (targetUri);\r
197                         req.ContentType = "application/x-www-form-urlencoded";\r
198                         req.Method = "POST";\r
199                         req.ConnectionGroupName = "" + i;\r
200                         req.ServicePoint.MaxIdleTime = 1;\r
201 \r
202                         req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);\r
203                         Thread.Sleep (1);\r
204                         req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);\r
205                 }\r
206         }\r
207 \r
208 // Debug code not used now, but could be useful later\r
209 /*\r
210         private void WriteServicePoint (string label, ServicePoint sp)\r
211         {\r
212                 Console.WriteLine ("\n" + label);\r
213                 Console.WriteLine ("Address: " + sp.Address);\r
214                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
215                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
216                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
217                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
218                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
219                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
220                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
221         }\r
222 */\r
223 }\r
224 }\r
225 \r