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.Reflection;
17 using System.Threading;
\r
19 namespace MonoTests.System.Net
\r
23 public class ServicePointTest
\r
25 static private int max;
\r
27 public void SaveMax () {
\r
28 max = ServicePointManager.MaxServicePoints;
\r
29 ServicePointManager.MaxServicePoints = 0;
\r
33 public void RestoreMax () {
\r
34 ServicePointManager.MaxServicePoints = max;
\r
38 [Category ("InetAccess")]
\r
41 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));
\r
42 //WriteServicePoint ("A servicepoint that isn't really", p);
\r
44 ServicePointManager.MaxServicePoints = 2;
\r
45 ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));
\r
47 ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));
\r
49 } catch (InvalidOperationException) { }
\r
50 ServicePointManager.MaxServicePoints = 0;
\r
52 //WriteServicePoint ("google before getting a webrequest", google);
\r
54 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
\r
55 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
\r
57 #if FOUND_SOME_OTHER_URL
58 // URL is no longer found, disabled the test until a more reliable URL is found :P
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
65 // in both instances property CurrentConnections is 0 according to ms.net.
\r
66 // let's see what it says when we do async operations...
\r
68 HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
\r
69 req2.Method = "PUT";
\r
70 IAsyncResult async = req2.BeginGetRequestStream (null, null);
\r
71 //WriteServicePoint ("after async BeginGetRequestStream", google);
\r
72 // CurrentConnections: 1
\r
73 Stream stream2 = req2.EndGetRequestStream (async);
\r
74 //WriteServicePoint ("after async EndGetRequestStream", google);
\r
75 // CurrentConnections: 1
\r
78 req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
\r
79 async = req2.BeginGetResponse (null, null);
\r
80 //WriteServicePoint ("after async BeginGetResponse", google);
\r
81 // CurrentConnections: 2
\r
82 WebResponse res2 = req2.EndGetResponse (async);
\r
83 //WriteServicePoint ("after async EndGetResponse", google);
\r
84 // CurrentConnections: 0
\r
85 // curious that after you get the webresponse object CurrentConnections is set to 0.
\r
86 // you'd think that you'd still be connected until you close the webresponse..
\r
87 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
\r
91 #if FOUND_SOME_OTHER_URL
92 // unless of course some buffering is taking place.. let's check
\r
93 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
\r
94 sp2 = ServicePointManager.FindServicePoint (uri2);
\r
95 req2 = (HttpWebRequest) WebRequest.Create (uri2);
\r
96 async = req2.BeginGetResponse (null, null);
\r
97 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
\r
98 // CurrentConnections: 1
\r
99 res2 = req2.EndGetResponse (async);
\r
100 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
\r
101 // CurrentConnections: 1
\r
103 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
\r
108 // what's the limit of the cache?
\r
109 req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");
\r
110 res2 = req2.GetResponse ();
\r
111 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));
\r
112 //WriteServicePoint ("apache", sp2);
\r
113 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
\r
114 // CurrentConnections: 1
\r
116 // curious other effect: address is actually the full Uri of the previous request
\r
117 // anyways, buffer is probably 4096 bytes
\r
120 // try getting the stream to 5 web response objects
\r
121 // while ConnectionLimit equals 2
\r
124 [Category ("InetAccess")]
\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
156 [Category ("InetAccess")]
\r
157 [Category ("AndroidNotWorking")] // #A1 fails
158 public void EndPointBind ()
\r
160 Uri uri = new Uri ("http://www.go-mono.com/");
\r
161 ServicePoint sp = ServicePointManager.FindServicePoint (uri);
\r
163 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
\r
165 bool called = false;
\r
166 sp.BindIPEndPointDelegate = delegate {
\r
167 Assert.IsTrue (!called);
\r
171 req.GetResponse ().Close ();
\r
173 Assert.IsTrue (called, "#A1");
\r
175 req = (HttpWebRequest) WebRequest.Create (uri);
\r
177 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {
\r
178 Assert.IsTrue (times < 5);
\r
180 return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);
\r
182 req.GetResponse ().Close ();
\r
184 Assert.IsTrue (called, "#A2");
\r
187 public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)
\r
191 [Test] //Covers #19823
\r
192 public void CloseConnectionGroupConcurency ()
\r
194 // Try with multiple service points
\r
195 for (var i = 0; i < 10; i++) {
\r
196 Uri targetUri = new Uri ("http://" + i + ".mono-project.com");
\r
197 var req = (HttpWebRequest) HttpWebRequest.Create (targetUri);
\r
198 req.ContentType = "application/x-www-form-urlencoded";
\r
199 req.Method = "POST";
\r
200 req.ConnectionGroupName = "" + i;
\r
201 req.ServicePoint.MaxIdleTime = 1;
\r
203 req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);
\r
205 req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);
\r
211 public void DnsRefreshTimeout ()
213 const int dnsRefreshTimeout = 200;
216 IPHostEntry host0, host1, host2;
218 PropertyInfo hostEntryProperty;
220 ServicePointManager.DnsRefreshTimeout = dnsRefreshTimeout;
222 uri = new Uri ("http://ww.google.com/");
223 sp = ServicePointManager.FindServicePoint (uri);
225 hostEntryProperty = typeof (ServicePoint).GetProperty ("HostEntry", BindingFlags.NonPublic | BindingFlags.Instance);
227 host0 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
228 host1 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
230 Assert.AreSame (host0, host1, "HostEntry should result in the same IPHostEntry object.");
232 Thread.Sleep (dnsRefreshTimeout * 2);
233 host2 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
235 Assert.AreNotSame(host0, host2, "HostEntry should result in a new IPHostEntry " +
236 "object when DnsRefreshTimeout is reached.");
239 // Debug code not used now, but could be useful later
\r
241 private void WriteServicePoint (string label, ServicePoint sp)
\r
243 Console.WriteLine ("\n" + label);
\r
244 Console.WriteLine ("Address: " + sp.Address);
\r
245 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
\r
246 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
\r
247 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
\r
248 Console.WriteLine ("IdleSince: " + sp.IdleSince);
\r
249 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
\r
250 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
\r
251 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
\r