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
39 [Ignore ("Unsupported - ServicePointManager.FindServicePoint")]
\r
43 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));
\r
44 //WriteServicePoint ("A servicepoint that isn't really", p);
\r
46 ServicePointManager.MaxServicePoints = 2;
\r
47 ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));
\r
49 ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));
\r
51 } catch (InvalidOperationException) { }
\r
52 ServicePointManager.MaxServicePoints = 0;
\r
54 //WriteServicePoint ("google before getting a webrequest", google);
\r
56 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
\r
57 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
\r
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
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
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
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
90 // unless of course some buffering is taking place.. let's check
\r
91 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
\r
92 ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);
\r
93 req2 = (HttpWebRequest) WebRequest.Create (uri2);
\r
94 async = req2.BeginGetResponse (null, null);
\r
95 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
\r
96 // CurrentConnections: 1
\r
97 res2 = req2.EndGetResponse (async);
\r
98 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
\r
99 // CurrentConnections: 1
\r
101 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
\r
105 // what's the limit of the cache?
\r
106 req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
\r
107 res2 = req2.GetResponse ();
\r
108 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
\r
109 //WriteServicePoint ("apache", sp2);
\r
110 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
\r
111 // CurrentConnections: 1
\r
113 // curious other effect: address is actually the full Uri of the previous request
\r
114 // anyways, buffer is probably 4096 bytes
\r
117 // try getting the stream to 5 web response objects
\r
118 // while ConnectionLimit equals 2
\r
121 [Category ("InetAccess")]
\r
123 [Ignore ("The System.Net.ServicePointManager.FindServicePoint(Uri) is not supported")]
\r
125 public void ConnectionLimit ()
\r
127 // the default is already 2, just in case it isn't..
\r
128 ServicePointManager.DefaultConnectionLimit = 5;
\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
141 for (int i = 0; i < 5; i++) {
\r
142 Stream stream = res [i].GetResponseStream();
\r
143 //Console.WriteLine ("Reading stream: " + i + " : " + stream);
\r
145 while (stream.ReadByte () != -1)
\r
147 //Console.WriteLine ("Finished reading: " + len + " bytes");
\r
150 for (int i = 0; i < 5; i++) {
\r
157 [Category ("InetAccess")]
\r
159 [Ignore ("The System.Net.ServicePointManager.FindServicePoint(Uri) is not supported")]
\r
161 public void EndPointBind ()
\r
163 Uri uri = new Uri ("http://www.go-mono.com/");
\r
164 ServicePoint sp = ServicePointManager.FindServicePoint (uri);
\r
166 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
\r
168 bool called = false;
\r
170 sp.BindIPEndPointDelegate = delegate {
\r
171 Assert.IsTrue (!called);
\r
176 req.GetResponse ().Close ();
\r
178 Assert.IsTrue (called);
\r
180 req = (HttpWebRequest) WebRequest.Create (uri);
\r
183 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {
\r
184 Assert.IsTrue (times < 5);
\r
186 return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);
\r
189 req.GetResponse ().Close ();
\r
191 Assert.IsTrue (called);
\r
195 // Debug code not used now, but could be useful later
\r
197 private void WriteServicePoint (string label, ServicePoint sp)
\r
199 Console.WriteLine ("\n" + label);
\r
200 Console.WriteLine ("Address: " + sp.Address);
\r
201 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
\r
202 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
\r
203 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
\r
204 Console.WriteLine ("IdleSince: " + sp.IdleSince);
\r
205 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
\r
206 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
\r
207 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
\r