2004-01-02 Nick Drochak <ndrochak@gol.com>
[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         public void All ()\r
38         {\r
39                 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));\r
40                 //WriteServicePoint ("A servicepoint that isn't really", p);                    \r
41                 \r
42                 ServicePointManager.MaxServicePoints = 2;\r
43                 ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));\r
44                 try {                   \r
45                         ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));\r
46                         Assertion.Fail ("#1");\r
47                 } catch (InvalidOperationException) { }\r
48                 ServicePointManager.MaxServicePoints = 0;\r
49                 \r
50                 //WriteServicePoint ("google before getting a webrequest", google);\r
51                 \r
52                 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
53                 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();                     \r
54                 \r
55                 //WriteServicePoint ("google after getting a response", google);\r
56                 ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));\r
57                 Assertion.AssertEquals ("#equals", google, google2);\r
58                 res.Close ();\r
59                 \r
60                 // in both instances property CurrentConnections is 0 according to ms.net.\r
61                 // let's see what it says when we do async operations...\r
62                 \r
63                 HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
64                 req2.Method = "PUT";\r
65                 IAsyncResult async = req2.BeginGetRequestStream (null, null);\r
66                 //WriteServicePoint ("after async BeginGetRequestStream", google);\r
67                 // CurrentConnections: 1\r
68                 Stream stream2 = req2.EndGetRequestStream (async);\r
69                 //WriteServicePoint ("after async EndGetRequestStream", google);\r
70                 // CurrentConnections: 1\r
71                 stream2.Close ();\r
72                 \r
73                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
74                 async = req2.BeginGetResponse (null, null);\r
75                 //WriteServicePoint ("after async BeginGetResponse", google);\r
76                 // CurrentConnections: 2\r
77                 WebResponse res2 = req2.EndGetResponse (async);\r
78                 //WriteServicePoint ("after async EndGetResponse", google);\r
79                 // CurrentConnections: 0                        \r
80                 // curious that after you get the webresponse object CurrentConnections is set to 0.\r
81                 // you'd think that you'd still be connected until you close the webresponse..\r
82                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
83                 res2.Close ();\r
84                 \r
85                 \r
86                 // unless of course some buffering is taking place.. let's check\r
87                 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");\r
88                 ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);\r
89                 req2 = (HttpWebRequest) WebRequest.Create (uri2);\r
90                 async = req2.BeginGetResponse (null, null);\r
91                 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);\r
92                 // CurrentConnections: 1\r
93                 res2 = req2.EndGetResponse (async);\r
94                 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);\r
95                 // CurrentConnections: 1\r
96                 // and so it shows\r
97                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
98                 res2.Close ();\r
99                 \r
100                 \r
101                 // what's the limit of the cache?\r
102                 req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");\r
103                 res2 = req2.GetResponse ();\r
104                 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));\r
105                 //WriteServicePoint ("apache", sp2);\r
106                 //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
107                 // CurrentConnections: 1\r
108                 res2.Close ();\r
109                 // curious other effect: address is actually the full Uri of the previous request\r
110                 // anyways, buffer is probably 4096 bytes\r
111         }\r
112 \r
113         // try getting the stream to 5 web response objects     \r
114         // while ConnectionLimit equals 2\r
115 \r
116         [Test]\r
117         public void ConnectionLimit ()\r
118         {               \r
119                 // the default is already 2, just in case it isn't..\r
120                 ServicePointManager.DefaultConnectionLimit = 5;\r
121                 \r
122                 Uri uri = new Uri ("http://www.go-mono.com/");\r
123                 ServicePoint sp = ServicePointManager.FindServicePoint (uri);                   \r
124                 WebResponse [] res = new WebResponse [5];\r
125                 for (int i = 0; i < 5; i++) {\r
126                         //Console.WriteLine ("GOT1 : " + i);\r
127                         HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
128                         //Console.WriteLine ("GOT2 : " + i);\r
129                         res [i] = req.GetResponse ();\r
130                         //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);\r
131                 }\r
132                 \r
133                 for (int i = 0; i < 5; i++) {\r
134                         Stream stream = res [i].GetResponseStream();\r
135                         //Console.WriteLine ("Reading stream: " + i + " : " + stream);\r
136                         int len = 0;\r
137                         while (stream.ReadByte () != -1)\r
138                                 len++;\r
139                         //Console.WriteLine ("Finished reading: " + len + " bytes");\r
140                 }\r
141                 \r
142                 for (int i = 0; i < 5; i++) {\r
143                         res [i].Close ();\r
144                 }\r
145         }\r
146 \r
147 // Debug code not used now, but could be useful later\r
148 /*\r
149         private void WriteServicePoint (string label, ServicePoint sp)\r
150         {\r
151                 Console.WriteLine ("\n" + label);\r
152                 Console.WriteLine ("Address: " + sp.Address);\r
153                 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);\r
154                 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);\r
155                 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);\r
156                 Console.WriteLine ("IdleSince: " + sp.IdleSince);\r
157                 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);\r
158                 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
159                 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
160         }\r
161 */\r
162 }\r
163 }\r
164 \r