2 // ServicePointTest.cs - NUnit Test Cases for System.Net.ServicePoint
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2003 Martin Willemoes Hansen
11 using NUnit
.Framework
;
13 using System
.Collections
;
16 using System
.Reflection
;
17 using System
.Threading
;
19 namespace MonoTests
.System
.Net
23 public class ServicePointTest
25 static private int max
;
27 #if !FEATURE_NO_BSD_SOCKETS
29 public void SaveMax () {
30 max
= ServicePointManager
.MaxServicePoints
;
31 ServicePointManager
.MaxServicePoints
= 0;
35 public void RestoreMax () {
36 ServicePointManager
.MaxServicePoints
= max
;
41 [Category ("NotWorking")]
44 ServicePoint p
= ServicePointManager
.FindServicePoint (new Uri ("mailto:xx@yyy.com"));
45 //WriteServicePoint ("A servicepoint that isn't really", p);
47 ServicePointManager
.MaxServicePoints
= 2;
48 ServicePoint google
= ServicePointManager
.FindServicePoint (new Uri ("http://www.google.com"));
50 ServicePoint slashdot
= ServicePointManager
.FindServicePoint (new Uri ("http://www.slashdot.org"));
52 } catch (InvalidOperationException
) { }
53 ServicePointManager
.MaxServicePoints
= 0;
55 //WriteServicePoint ("google before getting a webrequest", google);
57 HttpWebRequest req
= (HttpWebRequest
) WebRequest
.Create ("http://www.google.com");
58 HttpWebResponse res
= (HttpWebResponse
) req
.GetResponse ();
60 #if FOUND_SOME_OTHER_URL
61 // URL is no longer found, disabled the test until a more reliable URL is found :P
62 //WriteServicePoint ("google after getting a response", google);
63 ServicePoint google2
= ServicePointManager
.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));
64 Assert
.AreEqual (google
, google2
, "#equals");
68 // in both instances property CurrentConnections is 0 according to ms.net.
69 // let's see what it says when we do async operations...
71 HttpWebRequest req2
= (HttpWebRequest
) WebRequest
.Create ("http://www.google.com");
73 IAsyncResult
async = req2
.BeginGetRequestStream (null, null);
74 //WriteServicePoint ("after async BeginGetRequestStream", google);
75 // CurrentConnections: 1
76 Stream stream2
= req2
.EndGetRequestStream (async);
77 //WriteServicePoint ("after async EndGetRequestStream", google);
78 // CurrentConnections: 1
81 req2
= (HttpWebRequest
) WebRequest
.Create ("http://www.google.com");
82 async = req2
.BeginGetResponse (null, null);
83 //WriteServicePoint ("after async BeginGetResponse", google);
84 // CurrentConnections: 2
85 WebResponse res2
= req2
.EndGetResponse (async);
86 //WriteServicePoint ("after async EndGetResponse", google);
87 // CurrentConnections: 0
88 // curious that after you get the webresponse object CurrentConnections is set to 0.
89 // you'd think that you'd still be connected until you close the webresponse..
90 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
94 #if FOUND_SOME_OTHER_URL
95 // unless of course some buffering is taking place.. let's check
96 Uri uri2
= new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
97 sp2
= ServicePointManager
.FindServicePoint (uri2
);
98 req2
= (HttpWebRequest
) WebRequest
.Create (uri2
);
99 async = req2
.BeginGetResponse (null, null);
100 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
101 // CurrentConnections: 1
102 res2
= req2
.EndGetResponse (async);
103 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
104 // CurrentConnections: 1
106 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
111 // what's the limit of the cache?
112 req2
= (HttpWebRequest
) WebRequest
.Create ("http://www.apache.org/");
113 res2
= req2
.GetResponse ();
114 sp2
= ServicePointManager
.FindServicePoint (new Uri("http://www.apache.org/"));
115 //WriteServicePoint ("apache", sp2);
116 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
117 // CurrentConnections: 1
119 // curious other effect: address is actually the full Uri of the previous request
120 // anyways, buffer is probably 4096 bytes
123 // try getting the stream to 5 web response objects
124 // while ConnectionLimit equals 2
127 [Category ("NotWorking")]
128 public void ConnectionLimit ()
130 // the default is already 2, just in case it isn't..
131 ServicePointManager
.DefaultConnectionLimit
= 5;
133 Uri uri
= new Uri ("http://www.go-mono.com/");
134 ServicePoint sp
= ServicePointManager
.FindServicePoint (uri
);
135 WebResponse
[] res
= new WebResponse
[5];
136 for (int i
= 0; i
< 5; i
++) {
137 //Console.WriteLine ("GOT1 : " + i);
138 HttpWebRequest req
= (HttpWebRequest
) WebRequest
.Create (uri
);
139 //Console.WriteLine ("GOT2 : " + i);
140 res
[i
] = req
.GetResponse ();
141 //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);
144 for (int i
= 0; i
< 5; i
++) {
145 Stream stream
= res
[i
].GetResponseStream();
146 //Console.WriteLine ("Reading stream: " + i + " : " + stream);
148 while (stream
.ReadByte () != -1)
150 //Console.WriteLine ("Finished reading: " + len + " bytes");
153 for (int i
= 0; i
< 5; i
++) {
159 [Category ("NotWorking")] // #A1 fails
160 public void EndPointBind ()
162 Uri uri
= new Uri ("http://www.go-mono.com/");
163 ServicePoint sp
= ServicePointManager
.FindServicePoint (uri
);
165 HttpWebRequest req
= (HttpWebRequest
) WebRequest
.Create (uri
);
168 sp
.BindIPEndPointDelegate
= delegate {
169 Assert
.IsTrue (!called
);
173 req
.GetResponse ().Close ();
175 Assert
.IsTrue (called
, "#A1");
177 req
= (HttpWebRequest
) WebRequest
.Create (uri
);
179 sp
.BindIPEndPointDelegate
= delegate(ServicePoint point
, IPEndPoint remote
, int times
) {
180 Assert
.IsTrue (times
< 5);
182 return new IPEndPoint(IPAddress
.Parse("0.0.0.0"), 12345 + times
);
184 req
.GetResponse ().Close ();
186 Assert
.IsTrue (called
, "#A2");
189 public static void GetRequestStreamCallback (IAsyncResult asynchronousResult
)
193 [Test
] //Covers #19823
194 #if FEATURE_NO_BSD_SOCKETS
195 // This test uses HttpWebRequest
196 [ExpectedException (typeof (PlatformNotSupportedException
))]
198 public void CloseConnectionGroupConcurency ()
200 // Try with multiple service points
201 for (var i
= 0; i
< 10; i
++) {
202 Uri targetUri
= new Uri ("http://" + i
+ ".mono-project.com");
203 var req
= (HttpWebRequest
) HttpWebRequest
.Create (targetUri
);
204 req
.ContentType
= "application/x-www-form-urlencoded";
206 req
.ConnectionGroupName
= "" + i
;
207 req
.ServicePoint
.MaxIdleTime
= 1;
209 req
.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback
), req
);
211 req
.ServicePoint
.CloseConnectionGroup (req
.ConnectionGroupName
);
217 [Category ("RequiresBSDSockets")] // Tests internals, so it doesn't make sense to assert that PlatformNotSupportedExceptions are thrown.
218 public void DnsRefreshTimeout ()
220 const int dnsRefreshTimeout
= 2000;
223 IPHostEntry host0
, host1
, host2
;
225 PropertyInfo hostEntryProperty
;
227 ServicePointManager
.DnsRefreshTimeout
= dnsRefreshTimeout
;
229 uri
= new Uri ("http://localhost/");
230 sp
= ServicePointManager
.FindServicePoint (uri
);
232 hostEntryProperty
= typeof (ServicePoint
).GetProperty ("HostEntry", BindingFlags
.NonPublic
| BindingFlags
.Instance
);
234 host0
= hostEntryProperty
.GetValue (sp
, null) as IPHostEntry
;
235 host1
= hostEntryProperty
.GetValue (sp
, null) as IPHostEntry
;
237 Assert
.AreSame (host0
, host1
, "HostEntry should result in the same IPHostEntry object.");
239 Thread
.Sleep (dnsRefreshTimeout
* 2);
240 host2
= hostEntryProperty
.GetValue (sp
, null) as IPHostEntry
;
242 Assert
.AreNotSame(host0
, host2
, "HostEntry should result in a new IPHostEntry " +
243 "object when DnsRefreshTimeout is reached.");
246 // Debug code not used now, but could be useful later
248 private void WriteServicePoint (string label, ServicePoint sp)
250 Console.WriteLine ("\n" + label);
251 Console.WriteLine ("Address: " + sp.Address);
252 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
253 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
254 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
255 Console.WriteLine ("IdleSince: " + sp.IdleSince);
256 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
257 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
258 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);