Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[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                 //WriteServicePoint ("google after getting a response", google);\r
57                 ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));\r
58                 Assert.AreEqual (google, google2, "#equals");\r
59                 res.Close ();\r
60                 \r
61                 // in both instances property CurrentConnections is 0 according to ms.net.\r
62                 // let's see what it says when we do async operations...\r
63                 \r
64                 HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
65                 req2.Method = "PUT";\r
66                 IAsyncResult async = req2.BeginGetRequestStream (null, null);\r
67                 //WriteServicePoint ("after async BeginGetRequestStream", google);\r
68                 // CurrentConnections: 1\r
69                 Stream stream2 = req2.EndGetRequestStream (async);\r
70                 //WriteServicePoint ("after async EndGetRequestStream", google);\r
71                 // CurrentConnections: 1\r
72                 stream2.Close ();\r
73                 \r
74                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
75                 async = req2.BeginGetResponse (null, null);\r
76                 //WriteServicePoint ("after async BeginGetResponse", google);\r
77                 // CurrentConnections: 2\r
78                 WebResponse res2 = req2.EndGetResponse (async);\r
79                 //WriteServicePoint ("after async EndGetResponse", google);\r
80                 // CurrentConnections: 0                        \r
81                 // curious that after you get the webresponse object CurrentConnections is set to 0.\r
82                 // you'd think that you'd still be connected until you close the webresponse..\r
83                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
84                 res2.Close ();\r
85                 \r
86                 \r
87                 // unless of course some buffering is taking place.. let's check\r
88                 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");\r
89                 ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);\r
90                 req2 = (HttpWebRequest) WebRequest.Create (uri2);\r
91                 async = req2.BeginGetResponse (null, null);\r
92                 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);\r
93                 // CurrentConnections: 1\r
94                 res2 = req2.EndGetResponse (async);\r
95                 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);\r
96                 // CurrentConnections: 1\r
97                 // and so it shows\r
98                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
99                 res2.Close ();\r
100                 \r
101                 \r
102                 // what's the limit of the cache?\r
103                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");\r
104                 res2 = req2.GetResponse ();\r
105                 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));\r
106                 //WriteServicePoint ("apache", sp2);\r
107                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
108                 // CurrentConnections: 1\r
109                 res2.Close ();\r
110                 // curious other effect: address is actually the full Uri of the previous request\r
111                 // anyways, buffer is probably 4096 bytes\r
112         }\r
113 \r
114         // try getting the stream to 5 web response objects     \r
115         // while ConnectionLimit equals 2\r
116 \r
117         [Test]\r
118         [Category ("InetAccess")]\r
119         public void ConnectionLimit ()\r
120         {               \r
121                 // the default is already 2, just in case it isn't..\r
122                 ServicePointManager.DefaultConnectionLimit = 5;\r
123                 \r
124                 Uri uri = new Uri ("http://www.go-mono.com/");\r
125                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);                   \r
126                 WebResponse [] res = new WebResponse [5];\r
127                 for (int i = 0; i < 5; i++) {\r
128                         //Console.WriteLine ("GOT1 : " + i);\r
129                         HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
130                         //Console.WriteLine ("GOT2 : " + i);\r
131                         res [i] = req.GetResponse ();\r
132                         //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);\r
133                 }\r
134                 \r
135                 for (int i = 0; i < 5; i++) {\r
136                         Stream stream = res [i].GetResponseStream();\r
137                         //Console.WriteLine ("Reading stream: " + i + " : " + stream);\r
138                         int len = 0;\r
139                         while (stream.ReadByte () != -1)\r
140                                 len++;\r
141                         //Console.WriteLine ("Finished reading: " + len + " bytes");\r
142                 }\r
143                 \r
144                 for (int i = 0; i < 5; i++) {\r
145                         res [i].Close ();\r
146                 }\r
147         }\r
148 \r
149         [Test]\r
150         [Category ("InetAccess")]\r
151         public void EndPointBind ()\r
152         {\r
153                 Uri uri = new Uri ("http://www.go-mono.com/");\r
154                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);\r
155 \r
156                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
157 \r
158                 bool called = false;\r
159                 sp.BindIPEndPointDelegate = delegate {\r
160                         Assert.IsTrue (!called);\r
161                         called = true;\r
162                         return null;\r
163                 };\r
164                 req.GetResponse ().Close ();\r
165 \r
166                 Assert.IsTrue (called);\r
167 \r
168                 req = (HttpWebRequest) WebRequest.Create (uri);\r
169                 called = false;\r
170                 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {\r
171                         Assert.IsTrue (times < 5);\r
172                         called = true;\r
173                         return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);\r
174                 };\r
175                 req.GetResponse ().Close ();\r
176 \r
177                 Assert.IsTrue (called);\r
178         }\r
179 \r
180         public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)\r
181         {\r
182         }\r
183 \r
184         [Test] //Covers #19823\r
185         public void CloseConnectionGroupConcurency ()\r
186         {\r
187                 // Try with multiple service points\r
188                 for (var i = 0; i < 10; i++) {\r
189                         Uri targetUri = new Uri ("http://" + i + ".mono-project.com");\r
190                         var req = (HttpWebRequest) HttpWebRequest.Create (targetUri);\r
191                         req.ContentType = "application/x-www-form-urlencoded";\r
192                         req.Method = "POST";\r
193                         req.ConnectionGroupName = "" + i;\r
194                         req.ServicePoint.MaxIdleTime = 1;\r
195 \r
196                         req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);\r
197                         Thread.Sleep (1);\r
198                         req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);\r
199                 }\r
200         }\r
201 \r
202 // Debug code not used now, but could be useful later\r
203 /*\r
204         private void WriteServicePoint (string label, ServicePoint sp)\r
205         {\r
206                 Console.WriteLine ("\n" + label);\r
207                 Console.WriteLine ("Address: " + sp.Address);\r
208                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
209                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
210                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
211                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
212                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
213                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
214                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
215         }\r
216 */\r
217 }\r
218 }\r
219 \r