2 // ServicePointTest.cs - NUnit Test Cases for System.Net.ServicePoint
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
\r
8 // (C) 2003 Martin Willemoes Hansen
\r
11 using NUnit.Framework;
\r
13 using System.Collections;
\r
16 using System.Threading;
\r
18 namespace MonoTests.System.Net
\r
22 public class ServicePointTest
\r
24 static private int max;
\r
26 public void SaveMax () {
\r
27 max = ServicePointManager.MaxServicePoints;
\r
28 ServicePointManager.MaxServicePoints = 0;
\r
32 public void RestoreMax () {
\r
33 ServicePointManager.MaxServicePoints = max;
\r
37 [Category ("InetAccess")]
\r
40 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));
\r
41 //WriteServicePoint ("A servicepoint that isn't really", p);
\r
43 ServicePointManager.MaxServicePoints = 2;
\r
44 ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));
\r
46 ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));
\r
48 } catch (InvalidOperationException) { }
\r
49 ServicePointManager.MaxServicePoints = 0;
\r
51 //WriteServicePoint ("google before getting a webrequest", google);
\r
53 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
\r
54 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
\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
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
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
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
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
98 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
\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
110 // curious other effect: address is actually the full Uri of the previous request
\r
111 // anyways, buffer is probably 4096 bytes
\r
114 // try getting the stream to 5 web response objects
\r
115 // while ConnectionLimit equals 2
\r
118 [Category ("InetAccess")]
\r
119 public void ConnectionLimit ()
\r
121 // the default is already 2, just in case it isn't..
\r
122 ServicePointManager.DefaultConnectionLimit = 5;
\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
135 for (int i = 0; i < 5; i++) {
\r
136 Stream stream = res [i].GetResponseStream();
\r
137 //Console.WriteLine ("Reading stream: " + i + " : " + stream);
\r
139 while (stream.ReadByte () != -1)
\r
141 //Console.WriteLine ("Finished reading: " + len + " bytes");
\r
144 for (int i = 0; i < 5; i++) {
\r
150 [Category ("InetAccess")]
\r
151 public void EndPointBind ()
\r
153 Uri uri = new Uri ("http://www.go-mono.com/");
\r
154 ServicePoint sp = ServicePointManager.FindServicePoint (uri);
\r
156 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
\r
158 bool called = false;
\r
159 sp.BindIPEndPointDelegate = delegate {
\r
160 Assert.IsTrue (!called);
\r
164 req.GetResponse ().Close ();
\r
166 Assert.IsTrue (called);
\r
168 req = (HttpWebRequest) WebRequest.Create (uri);
\r
170 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {
\r
171 Assert.IsTrue (times < 5);
\r
173 return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);
\r
175 req.GetResponse ().Close ();
\r
177 Assert.IsTrue (called);
\r
180 public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)
\r
184 [Test] //Covers #19823
\r
185 public void CloseConnectionGroupConcurency ()
\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
196 req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);
\r
198 req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);
\r
202 // Debug code not used now, but could be useful later
\r
204 private void WriteServicePoint (string label, ServicePoint sp)
\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