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