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
.Threading
;
18 namespace MonoTests
.System
.Net
22 public class ServicePointTest
24 static private int max
;
26 public void SaveMax () {
27 max
= ServicePointManager
.MaxServicePoints
;
28 ServicePointManager
.MaxServicePoints
= 0;
32 public void RestoreMax () {
33 ServicePointManager
.MaxServicePoints
= max
;
39 ServicePoint p
= ServicePointManager
.FindServicePoint (new Uri ("mailto:xx@yyy.com"));
40 //WriteServicePoint ("A servicepoint that isn't really", p);
42 ServicePointManager
.MaxServicePoints
= 2;
43 ServicePoint google
= ServicePointManager
.FindServicePoint (new Uri ("http://www.google.com"));
45 ServicePoint slashdot
= ServicePointManager
.FindServicePoint (new Uri ("http://www.slashdot.org"));
46 Assertion
.Fail ("#1");
47 } catch (InvalidOperationException
) { }
48 ServicePointManager
.MaxServicePoints
= 0;
50 //WriteServicePoint ("google before getting a webrequest", google);
52 HttpWebRequest req
= (HttpWebRequest
) WebRequest
.Create ("http://www.google.com");
53 HttpWebResponse res
= (HttpWebResponse
) req
.GetResponse ();
55 //WriteServicePoint ("google after getting a response", google);
56 ServicePoint google2
= ServicePointManager
.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));
57 Assertion
.AssertEquals ("#equals", google
, google2
);
60 // in both instances property CurrentConnections is 0 according to ms.net.
61 // let's see what it says when we do async operations...
63 HttpWebRequest req2
= (HttpWebRequest
) WebRequest
.Create ("http://www.google.com");
65 IAsyncResult
async = req2
.BeginGetRequestStream (null, null);
66 //WriteServicePoint ("after async BeginGetRequestStream", google);
67 // CurrentConnections: 1
68 Stream stream2
= req2
.EndGetRequestStream (async);
69 //WriteServicePoint ("after async EndGetRequestStream", google);
70 // CurrentConnections: 1
73 req2
= (HttpWebRequest
) WebRequest
.Create ("http://www.google.com");
74 async = req2
.BeginGetResponse (null, null);
75 //WriteServicePoint ("after async BeginGetResponse", google);
76 // CurrentConnections: 2
77 WebResponse res2
= req2
.EndGetResponse (async);
78 //WriteServicePoint ("after async EndGetResponse", google);
79 // CurrentConnections: 0
80 // curious that after you get the webresponse object CurrentConnections is set to 0.
81 // you'd think that you'd still be connected until you close the webresponse..
82 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
86 // unless of course some buffering is taking place.. let's check
87 Uri uri2
= new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
88 ServicePoint sp2
= ServicePointManager
.FindServicePoint (uri2
);
89 req2
= (HttpWebRequest
) WebRequest
.Create (uri2
);
90 async = req2
.BeginGetResponse (null, null);
91 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
92 // CurrentConnections: 1
93 res2
= req2
.EndGetResponse (async);
94 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
95 // CurrentConnections: 1
97 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
101 // what's the limit of the cache?
102 req2
= (HttpWebRequest
) WebRequest
.Create ("http://www.apache.org/");
103 res2
= req2
.GetResponse ();
104 sp2
= ServicePointManager
.FindServicePoint (new Uri("http://www.apache.org/"));
105 //WriteServicePoint ("apache", sp2);
106 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
107 // CurrentConnections: 1
109 // curious other effect: address is actually the full Uri of the previous request
110 // anyways, buffer is probably 4096 bytes
113 // try getting the stream to 5 web response objects
114 // while ConnectionLimit equals 2
117 public void ConnectionLimit ()
119 // the default is already 2, just in case it isn't..
120 ServicePointManager
.DefaultConnectionLimit
= 5;
122 Uri uri
= new Uri ("http://www.go-mono.com/");
123 ServicePoint sp
= ServicePointManager
.FindServicePoint (uri
);
124 WebResponse
[] res
= new WebResponse
[5];
125 for (int i
= 0; i
< 5; i
++) {
126 //Console.WriteLine ("GOT1 : " + i);
127 HttpWebRequest req
= (HttpWebRequest
) WebRequest
.Create (uri
);
128 //Console.WriteLine ("GOT2 : " + i);
129 res
[i
] = req
.GetResponse ();
130 //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);
133 for (int i
= 0; i
< 5; i
++) {
134 Stream stream
= res
[i
].GetResponseStream();
135 //Console.WriteLine ("Reading stream: " + i + " : " + stream);
137 while (stream
.ReadByte () != -1)
139 //Console.WriteLine ("Finished reading: " + len + " bytes");
142 for (int i
= 0; i
< 5; i
++) {
147 // Debug code not used now, but could be useful later
149 private void WriteServicePoint (string label, ServicePoint sp)
151 Console.WriteLine ("\n" + label);
152 Console.WriteLine ("Address: " + sp.Address);
153 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
154 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
155 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
156 Console.WriteLine ("IdleSince: " + sp.IdleSince);
157 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
158 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
159 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);