2 // HttpWebRequestTest.cs - NUnit Test Cases for System.Net.HttpWebRequest
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // (C) 2003 Martin Willemoes Hansen
10 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com
13 using NUnit.Framework;
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Globalization;
20 using System.Net.Sockets;
21 using System.Security.Cryptography;
22 using System.Security.Cryptography.X509Certificates;
24 using System.Threading;
26 using Mono.Security.Authenticode;
27 using Mono.Security.Protocol.Tls;
30 namespace MonoTests.System.Net
33 public class HttpWebRequestTest
35 private Random rand = new Random ();
36 private byte [] data64KB = new byte [64 * 1024];
41 ServicePointManager.Expect100Continue = false;
42 rand.NextBytes (data64KB);
47 [Ignore ("Ignore failures in Sys.Net")]
49 public void Proxy_Null ()
51 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
52 Assert.IsNotNull (req.Proxy, "#1");
54 Assert.IsNull (req.Proxy, "#2");
58 [Category("InetAccess")]
60 [Ignore ("NMA - wrong cookies number returned")]
64 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
65 Assert.IsNotNull (req.IfModifiedSince, "req:If Modified Since: ");
67 req.UserAgent = "MonoClient v1.0";
68 Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
69 Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
71 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
72 Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
73 Assert.AreEqual ("OK", res.StatusDescription, "#B2");
75 Assert.AreEqual ("text/html; charset=ISO-8859-1", res.Headers.Get ("Content-Type"), "#C1");
76 Assert.IsNotNull (res.LastModified, "#C2");
77 Assert.AreEqual (0, res.Cookies.Count, "#C3");
83 public void AddRange ()
85 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
87 req.AddRange (50, 90);
88 req.AddRange ("bytes", 100);
89 req.AddRange ("bytes", 100, 120);
90 Assert.AreEqual ("bytes=10-,50-90,100-,100-120", req.Headers ["Range"], "#1");
92 req.AddRange ("bits", 2000);
94 } catch (InvalidOperationException) {}
98 public void CloseRequestStreamAfterReadingResponse ()
100 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9152);
101 string url = "http://" + ep.ToString () + "/test/";
103 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
106 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
109 req.ReadWriteTimeout = 2000;
111 byte [] data = new byte [128];
112 req.ContentLength = data.Length;
114 Stream rs = req.GetRequestStream ();
115 rs.Write (data, 0, data.Length);
118 HttpWebResponse response = (HttpWebResponse) req.GetResponse ();
128 [Category("InetAccess")]
129 public void Cookies1 ()
131 // The purpose of this test is to ensure that the cookies we get from a request
132 // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer
133 // in HttpWebRequest.
134 // If this URL stops sending *one* and only one cookie, replace it.
135 string url = "http://www.elmundo.es";
136 CookieContainer cookies = new CookieContainer ();
137 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
138 req.KeepAlive = false;
139 req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2";
140 req.CookieContainer = cookies;
141 Assert.AreEqual (0, cookies.Count, "#01");
142 using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) {
143 CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url));
144 Assert.AreEqual (1, coll.Count, "#02");
145 Assert.AreEqual (1, res.Cookies.Count, "#03");
146 Cookie one = coll [0];
147 Cookie two = res.Cookies [0];
148 Assert.AreEqual (true, object.ReferenceEquals (one, two), "#04");
152 #if !TARGET_JVM //NotWorking
154 [Ignore ("Fails on MS.NET")]
155 public void SslClientBlock ()
157 // This tests that the write request/initread/write body sequence does not hang
159 // If there's a regression for this, the test will hang.
160 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
162 SslHttpServer server = new SslHttpServer ();
165 string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port);
166 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
167 request.Method = "POST";
168 Stream stream = request.GetRequestStream ();
169 byte [] bytes = new byte [100];
170 stream.Write (bytes, 0, bytes.Length);
172 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
173 Assert.AreEqual (200, (int) resp.StatusCode, "StatusCode");
174 StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
179 if (server.Error != null)
182 ServicePointManager.CertificatePolicy = null;
188 [Category("NotWorking")]
190 public void Missing_ContentEncoding ()
192 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
194 BadChunkedServer server = new BadChunkedServer ();
197 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
198 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
199 request.Method = "GET";
200 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
201 Assert.AreEqual ("", resp.ContentEncoding);
204 if (server.Error != null)
207 ServicePointManager.CertificatePolicy = null;
213 [Category ("NotWorking")]
215 public void BadServer_ChunkedClose ()
217 // The server will send a chunked response without a 'last-chunked' mark
218 // and then shutdown the socket for sending.
219 BadChunkedServer server = new BadChunkedServer ();
221 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
222 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
223 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
226 byte [] bytes = new byte [32];
227 // Using StreamReader+UTF8Encoding here fails on MS runtime
228 Stream stream = resp.GetResponseStream ();
229 int nread = stream.Read (bytes, 0, 32);
230 Assert.AreEqual (16, nread, "#01");
231 x = Encoding.ASCII.GetString (bytes, 0, 16);
237 if (server.Error != null)
240 Assert.AreEqual ("1234567890123456", x);
244 [Ignore ("This test asserts that our code violates RFC 2616")]
245 public void MethodCase ()
247 ListDictionary methods = new ListDictionary ();
248 methods.Add ("post", "POST");
249 methods.Add ("puT", "PUT");
250 methods.Add ("POST", "POST");
251 methods.Add ("whatever", "whatever");
252 methods.Add ("PUT", "PUT");
254 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9153);
255 string url = "http://" + ep.ToString () + "/test/";
257 foreach (DictionaryEntry de in methods) {
258 SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler));
261 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
262 req.Method = (string) de.Key;
264 req.ReadWriteTimeout = 2000;
265 req.KeepAlive = false;
266 Stream rs = req.GetRequestStream ();
268 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
269 StreamReader sr = new StreamReader (resp.GetResponseStream (),
271 string line = sr.ReadLine ();
273 Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
282 public void BeginGetRequestStream_Body_NotAllowed ()
284 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9154);
285 string url = "http://" + ep.ToString () + "/test/";
287 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
290 HttpWebRequest request;
292 request = (HttpWebRequest) WebRequest.Create (url);
293 request.Method = "GET";
296 request.BeginGetRequestStream (null, null);
298 } catch (ProtocolViolationException ex) {
299 // Cannot send a content-body with this
301 Assert.IsNull (ex.InnerException, "#A2");
302 Assert.IsNotNull (ex.Message, "#A3");
305 request = (HttpWebRequest) WebRequest.Create (url);
306 request.Method = "HEAD";
309 request.BeginGetRequestStream (null, null);
311 } catch (ProtocolViolationException ex) {
312 // Cannot send a content-body with this
314 Assert.IsNull (ex.InnerException, "#B2");
315 Assert.IsNotNull (ex.Message, "#B3");
320 [Test] // bug #465613
321 public void BeginGetRequestStream_NoBuffering ()
323 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 11001);
324 string url = "http://" + ep.ToString () + "/test/";
326 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
333 req = (HttpWebRequest) WebRequest.Create (url);
335 req.SendChunked = false;
336 req.KeepAlive = false;
337 req.AllowWriteStreamBuffering = false;
339 ar = req.BeginGetRequestStream (null, null);
340 rs = req.EndGetRequestStream (ar);
343 req = (HttpWebRequest) WebRequest.Create (url);
345 req.SendChunked = false;
346 req.KeepAlive = true;
347 req.AllowWriteStreamBuffering = false;
350 req.BeginGetRequestStream (null, null);
352 } catch (ProtocolViolationException ex) {
353 // When performing a write operation with
354 // AllowWriteStreamBuffering set to false,
355 // you must either set ContentLength to a
356 // non-negative number or set SendChunked
358 Assert.IsNull (ex.InnerException, "#A2");
359 Assert.IsNotNull (ex.Message, "#A3");
362 req = (HttpWebRequest) WebRequest.Create (url);
364 req.SendChunked = false;
365 req.KeepAlive = true;
366 req.AllowWriteStreamBuffering = false;
367 req.ContentLength = 0;
369 ar = req.BeginGetRequestStream (null, null);
370 rs = req.EndGetRequestStream (ar);
375 [Test] // bug #508027
376 public void BeginGetResponse ()
378 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8001);
379 string url = "http://" + ep.ToString () + "/test/";
381 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
386 req = (HttpWebRequest) WebRequest.Create (url);
388 req.SendChunked = false;
389 req.KeepAlive = false;
390 req.AllowWriteStreamBuffering = false;
391 req.BeginGetResponse (null, null);
394 req = (HttpWebRequest) WebRequest.Create (url);
396 req.SendChunked = true;
397 req.KeepAlive = false;
398 req.AllowWriteStreamBuffering = false;
399 req.GetRequestStream ().WriteByte (1);
400 req.BeginGetResponse (null, null);
403 req = (HttpWebRequest) WebRequest.Create (url);
405 req.ContentLength = 5;
406 req.SendChunked = false;
407 req.KeepAlive = false;
408 req.AllowWriteStreamBuffering = false;
409 req.GetRequestStream ().WriteByte (5);
410 req.BeginGetResponse (null, null);
413 req = (HttpWebRequest) WebRequest.Create (url);
415 req.SendChunked = false;
416 req.KeepAlive = true;
417 req.AllowWriteStreamBuffering = false;
419 req.BeginGetResponse (null, null);
422 req = (HttpWebRequest) WebRequest.Create (url);
424 req.SendChunked = false;
425 req.KeepAlive = false;
426 req.AllowWriteStreamBuffering = false;
427 req.ContentLength = 5;
428 req.BeginGetResponse (null, null);
431 req = (HttpWebRequest) WebRequest.Create (url);
433 req.SendChunked = false;
434 req.KeepAlive = true;
435 req.AllowWriteStreamBuffering = false;
436 req.ContentLength = 5;
437 req.BeginGetResponse (null, null);
440 req = (HttpWebRequest) WebRequest.Create (url);
442 req.SendChunked = true;
444 req.BeginGetResponse (null, null);
447 req = (HttpWebRequest) WebRequest.Create (url);
449 req.ContentLength = 5;
451 req.BeginGetResponse (null, null);
454 req = (HttpWebRequest) WebRequest.Create (url);
456 req.ContentLength = 0;
458 req.BeginGetResponse (null, null);
463 [Test] // bug #511851
464 public void BeginGetRequestStream_Request_Aborted ()
466 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8002);
467 string url = "http://" + ep.ToString () + "/test/";
469 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
472 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
477 req.BeginGetRequestStream (null, null);
479 } catch (WebException ex) {
480 // The request was aborted: The request was canceled
481 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
482 Assert.IsNull (ex.InnerException, "#3");
483 Assert.IsNotNull (ex.Message, "#4");
484 Assert.IsNull (ex.Response, "#5");
485 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
490 [Test] // bug #511851
491 public void BeginGetResponse_Request_Aborted ()
493 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9155);
494 string url = "http://" + ep.ToString () + "/test/";
496 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
499 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
504 req.BeginGetResponse (null, null);
506 } catch (WebException ex) {
507 // The request was aborted: The request was canceled
508 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
509 Assert.IsNull (ex.InnerException, "#3");
510 Assert.IsNotNull (ex.Message, "#4");
511 Assert.IsNull (ex.Response, "#5");
512 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
518 public void EndGetRequestStream_AsyncResult_Null ()
520 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9156);
521 string url = "http://" + ep.ToString () + "/test/";
523 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
526 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
528 req.BeginGetRequestStream (null, null);
531 req.EndGetRequestStream (null);
533 } catch (ArgumentNullException ex) {
534 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
535 Assert.IsNull (ex.InnerException, "#3");
536 Assert.IsNotNull (ex.Message, "#4");
537 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
545 [Category ("NotWorking")] // do not get consistent result on MS
546 public void EndGetRequestStream_Request_Aborted ()
548 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8003);
549 string url = "http://" + ep.ToString () + "/test/";
551 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
554 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
556 IAsyncResult ar = req.BeginGetRequestStream (null, null);
561 req.EndGetRequestStream (ar);
563 } catch (WebException ex) {
564 // The request was aborted: The request was canceled
565 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
566 Assert.IsNull (ex.InnerException, "#3");
567 Assert.IsNotNull (ex.Message, "#4");
568 Assert.IsNull (ex.Response, "#5");
569 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
574 [Test] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=471522
575 [Category ("NotWorking")]
576 public void EndGetResponse_AsyncResult_Invalid ()
578 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9157);
579 string url = "http://" + ep.ToString () + "/test/";
581 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
584 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
587 req.ReadWriteTimeout = 2000;
588 IAsyncResult ar = req.BeginGetRequestStream (null, null);
590 // AsyncResult was not returned from call to BeginGetResponse
592 req.EndGetResponse (ar);
594 } catch (InvalidCastException) {
602 public void EndGetResponse_AsyncResult_Null ()
604 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9158);
605 string url = "http://" + ep.ToString () + "/test/";
607 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
610 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
612 req.ReadWriteTimeout = 2000;
614 IAsyncResult ar = req.BeginGetResponse (null, null);
617 req.EndGetResponse (null);
619 } catch (ArgumentNullException ex) {
620 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
621 Assert.IsNull (ex.InnerException, "#3");
622 Assert.IsNotNull (ex.Message, "#4");
623 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
627 using (HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse (ar)) {
634 [Test] // bug #429200
635 public void GetRequestStream ()
637 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10000);
638 string url = "http://" + ep.ToString () + "/test/";
640 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
643 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
646 req.ReadWriteTimeout = 2000;
648 Stream rs1 = req.GetRequestStream ();
649 Stream rs2 = req.GetRequestStream ();
651 Assert.IsNotNull (rs1, "#1");
652 Assert.AreSame (rs1, rs2, "#2");
658 [Test] // bug #511851
659 public void GetRequestStream_Request_Aborted ()
661 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10001);
662 string url = "http://" + ep.ToString () + "/test/";
664 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
667 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
672 req.GetRequestStream ();
674 } catch (WebException ex) {
675 // The request was aborted: The request was canceled
676 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
677 Assert.IsNull (ex.InnerException, "#3");
678 Assert.IsNotNull (ex.Message, "#4");
679 Assert.IsNull (ex.Response, "#5");
680 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
685 [Test] // bug #510661
686 public void GetRequestStream_Close_NotAllBytesWritten ()
688 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10002);
689 string url = "http://" + ep.ToString () + "/test/";
691 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
697 req = (HttpWebRequest) WebRequest.Create (url);
699 req.ContentLength = 2;
700 rs = req.GetRequestStream ();
704 } catch (WebException ex) {
705 // The request was aborted: The request was canceled
706 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
707 Assert.IsNotNull (ex.Message, "#A3");
708 Assert.IsNull (ex.Response, "#A4");
709 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#A5");
711 // Cannot close stream until all bytes are written
712 Exception inner = ex.InnerException;
713 Assert.IsNotNull (inner, "#A6");
714 Assert.AreEqual (typeof (IOException), inner.GetType (), "#A7");
715 Assert.IsNull (inner.InnerException, "#A8");
716 Assert.IsNotNull (inner.Message, "#A9");
719 req = (HttpWebRequest) WebRequest.Create (url);
721 req.ContentLength = 2;
722 rs = req.GetRequestStream ();
727 } catch (WebException ex) {
728 // The request was aborted: The request was canceled
729 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
730 Assert.IsNotNull (ex.Message, "#B3");
731 Assert.IsNull (ex.Response, "#B4");
732 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#B5");
734 // Cannot close stream until all bytes are written
735 Exception inner = ex.InnerException;
736 Assert.IsNotNull (inner, "#B6");
737 Assert.AreEqual (typeof (IOException), inner.GetType (), "#B7");
738 Assert.IsNull (inner.InnerException, "#B8");
739 Assert.IsNotNull (inner.Message, "#B9");
742 req = (HttpWebRequest) WebRequest.Create (url);
744 req.ContentLength = 2;
745 rs = req.GetRequestStream ();
752 [Test] // bug #510642
753 public void GetRequestStream_Write_Overflow ()
755 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8010);
756 string url = "http://" + ep.ToString () + "/test/";
758 // buffered, non-chunked
759 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
766 req = (HttpWebRequest) WebRequest.Create (url);
769 req.ReadWriteTimeout = 2000;
770 req.ContentLength = 2;
772 rs = req.GetRequestStream ();
775 buffer = new byte [] { 0x2a, 0x1d };
777 rs.Write (buffer, 0, buffer.Length);
779 } catch (ProtocolViolationException ex) {
780 // Bytes to be written to the stream exceed
781 // Content-Length bytes size specified
782 Assert.IsNull (ex.InnerException, "#A2");
783 Assert.IsNotNull (ex.Message, "#A3");
788 req = (HttpWebRequest) WebRequest.Create (url);
791 req.ReadWriteTimeout = 2000;
792 req.ContentLength = 2;
794 rs = req.GetRequestStream ();
796 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
798 rs.Write (buffer, 0, buffer.Length);
800 } catch (ProtocolViolationException ex) {
801 // Bytes to be written to the stream exceed
802 // Content-Length bytes size specified
803 Assert.IsNull (ex.InnerException, "#B2");
804 Assert.IsNotNull (ex.Message, "#B3");
811 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
819 req = (HttpWebRequest) WebRequest.Create (url);
821 req.SendChunked = true;
823 req.ReadWriteTimeout = 2000;
824 req.ContentLength = 2;
826 rs = req.GetRequestStream ();
829 buffer = new byte [] { 0x2a, 0x1d };
830 rs.Write (buffer, 0, buffer.Length);
834 req = (HttpWebRequest) WebRequest.Create (url);
836 req.SendChunked = true;
838 req.ReadWriteTimeout = 2000;
839 req.ContentLength = 2;
841 rs = req.GetRequestStream ();
843 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
844 rs.Write (buffer, 0, buffer.Length);
848 // non-buffered, non-chunked
849 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
856 req = (HttpWebRequest) WebRequest.Create (url);
857 req.AllowWriteStreamBuffering = false;
860 req.ReadWriteTimeout = 2000;
861 req.ContentLength = 2;
863 rs = req.GetRequestStream ();
866 buffer = new byte [] { 0x2a, 0x1d };
868 rs.Write (buffer, 0, buffer.Length);
870 } catch (ProtocolViolationException ex) {
871 // Bytes to be written to the stream exceed
872 // Content-Length bytes size specified
873 Assert.IsNull (ex.InnerException, "#C2");
874 Assert.IsNotNull (ex.Message, "#3");
879 req = (HttpWebRequest) WebRequest.Create (url);
880 req.AllowWriteStreamBuffering = false;
883 req.ReadWriteTimeout = 2000;
884 req.ContentLength = 2;
886 rs = req.GetRequestStream ();
888 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
890 rs.Write (buffer, 0, buffer.Length);
892 } catch (ProtocolViolationException ex) {
893 // Bytes to be written to the stream exceed
894 // Content-Length bytes size specified
895 Assert.IsNull (ex.InnerException, "#D2");
896 Assert.IsNotNull (ex.Message, "#D3");
902 // non-buffered, chunked
903 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
910 req = (HttpWebRequest) WebRequest.Create (url);
911 req.AllowWriteStreamBuffering = false;
913 req.SendChunked = true;
915 req.ReadWriteTimeout = 2000;
916 req.ContentLength = 2;
918 rs = req.GetRequestStream ();
921 buffer = new byte [] { 0x2a, 0x1d };
922 rs.Write (buffer, 0, buffer.Length);
925 req = (HttpWebRequest) WebRequest.Create (url);
926 req.AllowWriteStreamBuffering = false;
928 req.SendChunked = true;
930 req.ReadWriteTimeout = 2000;
931 req.ContentLength = 2;
933 rs = req.GetRequestStream ();
935 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
936 rs.Write (buffer, 0, buffer.Length);
942 [Ignore ("This test asserts that our code violates RFC 2616")]
943 public void GetRequestStream_Body_NotAllowed ()
945 string [] methods = new string [] { "GET", "HEAD", "CONNECT",
946 "get", "HeAd", "ConNect" };
948 foreach (string method in methods) {
949 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
950 "http://localhost:8000");
953 req.GetRequestStream ();
954 Assert.Fail ("#1:" + method);
955 } catch (ProtocolViolationException ex) {
956 Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
957 Assert.IsNull (ex.InnerException, "#3:" + method);
958 Assert.IsNotNull (ex.Message, "#4:" + method);
963 [Test] // bug #511851
964 public void GetResponse_Request_Aborted ()
966 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10100);
967 string url = "http://" + ep.ToString () + "/test/";
969 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
972 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
979 } catch (WebException ex) {
980 // The request was aborted: The request was canceled
981 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
982 Assert.IsNull (ex.InnerException, "#3");
983 Assert.IsNotNull (ex.Message, "#4");
984 Assert.IsNull (ex.Response, "#5");
985 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
992 [Category("NotWorking")]
994 [Ignore ("This does not timeout any more. That's how MS works when reading small responses")]
995 public void ReadTimeout ()
997 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
998 string url = "http://" + localEP.ToString () + "/original/";
1000 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1003 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1004 req.Method = "POST";
1005 req.AllowAutoRedirect = false;
1007 req.ReadWriteTimeout = 2000;
1008 req.KeepAlive = false;
1009 Stream rs = req.GetRequestStream ();
1011 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1013 Stream s = resp.GetResponseStream ();
1016 } catch (WebException ex) {
1017 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
1018 Assert.IsNull (ex.InnerException, "#3");
1019 Assert.IsNull (ex.Response, "#4");
1020 Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
1027 [Test] // bug #324300
1029 [Category("NotWorking")]
1031 public void AllowAutoRedirect ()
1033 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
1034 string url = "http://" + localEP.ToString () + "/original/";
1036 // allow autoredirect
1037 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1040 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1041 req.Method = "POST";
1043 req.ReadWriteTimeout = 2000;
1044 req.KeepAlive = false;
1045 Stream rs = req.GetRequestStream ();
1047 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1048 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1050 string body = sr.ReadToEnd ();
1052 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1053 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1054 localEP.ToString () + "/moved/", "#A2");
1055 Assert.AreEqual ("GET", resp.Method, "#A3");
1056 Assert.AreEqual ("LOOKS OK", body, "#A4");
1061 // do not allow autoredirect
1062 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1065 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1066 req.Method = "POST";
1067 req.AllowAutoRedirect = false;
1069 req.ReadWriteTimeout = 1000;
1070 req.KeepAlive = false;
1071 Stream rs = req.GetRequestStream ();
1073 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1074 Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
1075 Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
1076 Assert.AreEqual ("POST", resp.Method, "#B3");
1083 public void PostAndRedirect_NoCL ()
1085 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8769);
1086 string url = "http://" + localEP.ToString () + "/original/";
1088 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1091 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1092 req.Method = "POST";
1094 req.ReadWriteTimeout = 2000;
1095 Stream rs = req.GetRequestStream ();
1098 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1099 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1101 string body = sr.ReadToEnd ();
1103 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1104 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1105 localEP.ToString () + "/moved/", "#A2");
1106 Assert.AreEqual ("GET", resp.Method, "#A3");
1107 Assert.AreEqual ("LOOKS OK", body, "#A4");
1114 public void PostAndRedirect_CL ()
1116 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8770);
1117 string url = "http://" + localEP.ToString () + "/original/";
1119 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1122 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1123 req.Method = "POST";
1125 req.ReadWriteTimeout = 2000;
1126 req.ContentLength = 1;
1127 Stream rs = req.GetRequestStream ();
1129 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1130 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1132 string body = sr.ReadToEnd ();
1134 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1135 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1136 localEP.ToString () + "/moved/", "#A2");
1137 Assert.AreEqual ("GET", resp.Method, "#A3");
1138 Assert.AreEqual ("LOOKS OK", body, "#A4");
1145 public void PostAnd401 ()
1147 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8771);
1148 string url = "http://" + localEP.ToString () + "/original/";
1150 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1153 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1154 req.Method = "POST";
1156 req.ReadWriteTimeout = 2000;
1157 req.ContentLength = 1;
1158 Stream rs = req.GetRequestStream ();
1160 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1161 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1163 string body = sr.ReadToEnd ();
1165 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1166 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1167 localEP.ToString () + "/moved/", "#A2");
1168 Assert.AreEqual ("GET", resp.Method, "#A3");
1169 Assert.AreEqual ("LOOKS OK", body, "#A4");
1175 [Test] // bug #324347
1176 [Category ("NotWorking")]
1177 public void InternalServerError ()
1179 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8766);
1180 string url = "http://" + localEP.ToString () + "/original/";
1183 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
1186 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1187 req.Method = "POST";
1189 req.ReadWriteTimeout = 2000;
1190 req.KeepAlive = false;
1191 Stream rs = req.GetRequestStream ();
1196 Assert.Fail ("#A1");
1197 } catch (WebException ex) {
1198 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1199 Assert.IsNull (ex.InnerException, "#A3");
1200 Assert.IsNotNull (ex.Message, "#A4");
1201 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
1203 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1204 Assert.IsNotNull (webResponse, "#A6");
1205 Assert.AreEqual ("POST", webResponse.Method, "#A7");
1206 webResponse.Close ();
1213 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
1216 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1219 req.ReadWriteTimeout = 2000;
1220 req.KeepAlive = false;
1224 Assert.Fail ("#B1");
1225 } catch (WebException ex) {
1226 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1227 Assert.IsNull (ex.InnerException, "#B3");
1228 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1230 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1231 Assert.IsNotNull (webResponse, "#B5");
1232 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1233 webResponse.Close ();
1241 [Category ("NotWorking")] // #B3 fails; we get a SocketException: An existing connection was forcibly closed by the remote host
1242 public void NoContentLength ()
1244 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8767);
1245 string url = "http://" + localEP.ToString () + "/original/";
1248 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
1251 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1252 req.Method = "POST";
1254 req.ReadWriteTimeout = 2000;
1255 req.KeepAlive = false;
1256 Stream rs = req.GetRequestStream ();
1261 Assert.Fail ("#A1");
1262 } catch (WebException ex) {
1263 // The underlying connection was closed:
1264 // An unexpected error occurred on a
1266 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1267 Assert.IsNotNull (ex.InnerException, "#A3");
1268 Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
1269 Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
1271 // Unable to read data from the transport connection:
1272 // A connection attempt failed because the connected party
1273 // did not properly respond after a period of time, or
1274 // established connection failed because connected host has
1275 // failed to respond
1276 IOException ioe = (IOException) ex.InnerException;
1277 Assert.IsNotNull (ioe.InnerException, "#A6");
1278 Assert.IsNotNull (ioe.Message, "#A7");
1279 Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
1281 // An existing connection was forcibly
1282 // closed by the remote host
1283 SocketException soe = (SocketException) ioe.InnerException;
1284 Assert.IsNull (soe.InnerException, "#A9");
1285 Assert.IsNotNull (soe.Message, "#A10");
1287 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1288 Assert.IsNull (webResponse, "#A11");
1295 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
1298 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1301 req.ReadWriteTimeout = 2000;
1302 req.KeepAlive = false;
1306 Assert.Fail ("#B1");
1307 } catch (WebException ex) {
1308 // The remote server returned an error:
1309 // (500) Internal Server Error
1310 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1311 Assert.IsNull (ex.InnerException, "#B3");
1312 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1314 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1315 Assert.IsNotNull (webResponse, "#B5");
1316 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1317 webResponse.Close ();
1324 [Test] // bug #513087
1325 public void NonStandardVerb ()
1327 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8123);
1328 string url = "http://" + ep.ToString () + "/moved/";
1330 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (VerbEchoHandler))) {
1333 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1334 req.Method = "WhatEver";
1335 req.KeepAlive = false;
1336 req.Timeout = 20000;
1337 req.ReadWriteTimeout = 20000;
1339 Stream rs = req.GetRequestStream ();
1342 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1343 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1345 string body = sr.ReadToEnd ();
1347 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#1");
1348 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1349 ep.ToString () + "/moved/", "#2");
1350 Assert.AreEqual ("WhatEver", resp.Method, "#3");
1351 Assert.AreEqual ("WhatEver", body, "#4");
1359 [Category ("NotWorking")] // Assert #2 fails
1360 public void NotModifiedSince ()
1362 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9123);
1363 string url = "http://" + ep.ToString () + "/test/";
1365 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (NotModifiedSinceHandler))) {
1368 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1370 req.KeepAlive = false;
1371 req.Timeout = 20000;
1372 req.ReadWriteTimeout = 20000;
1373 req.Headers.Add (HttpRequestHeader.IfNoneMatch, "898bbr2347056cc2e096afc66e104653");
1374 req.IfModifiedSince = new DateTime (2010, 01, 04);
1376 DateTime start = DateTime.Now;
1377 HttpWebResponse response = null;
1382 } catch (WebException e) {
1383 response = (HttpWebResponse) e.Response;
1386 Assert.IsNotNull (response, "#2");
1387 using (Stream stream = response.GetResponseStream ()) {
1388 byte [] buffer = new byte [4096];
1389 int bytesRead = stream.Read (buffer, 0, buffer.Length);
1390 Assert.AreEqual (0, bytesRead, "#3");
1393 TimeSpan elapsed = DateTime.Now - start;
1394 Assert.IsTrue (elapsed.TotalMilliseconds < 2000, "#4");
1400 [Test] // bug #353495
1401 [Category ("NotWorking")]
1402 public void LastModifiedKind ()
1404 const string reqURL = "http://coffeefaq.com/site/node/25";
1405 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
1406 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
1407 DateTime lastMod = resp.LastModified;
1408 //string rawLastMod = resp.Headers ["Last-Modified"];
1410 //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
1411 Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
1412 req = (HttpWebRequest) WebRequest.Create (reqURL);
1413 req.IfModifiedSince = lastMod;
1415 resp = (HttpWebResponse) req.GetResponse ();
1417 Assert.Fail ("Should result in 304");
1418 } catch (WebException ex) {
1419 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
1420 Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
1424 internal static byte [] EchoRequestHandler (Socket socket)
1426 MemoryStream ms = new MemoryStream ();
1427 byte [] buffer = new byte [4096];
1428 int bytesReceived = socket.Receive (buffer);
1429 while (bytesReceived > 0) {
1430 ms.Write (buffer, 0, bytesReceived);
1431 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1432 // after sending the headers
1434 if (socket.Available > 0) {
1435 bytesReceived = socket.Receive (buffer);
1442 StreamReader sr = new StreamReader (ms, Encoding.UTF8);
1443 string request = sr.ReadToEnd ();
1445 StringWriter sw = new StringWriter ();
1446 sw.WriteLine ("HTTP/1.1 200 OK");
1447 sw.WriteLine ("Content-Type: text/xml");
1448 sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
1453 return Encoding.UTF8.GetBytes (sw.ToString ());
1456 static byte [] RedirectRequestHandler (Socket socket)
1458 MemoryStream ms = new MemoryStream ();
1459 byte [] buffer = new byte [4096];
1460 int bytesReceived = socket.Receive (buffer);
1461 while (bytesReceived > 0) {
1462 ms.Write (buffer, 0, bytesReceived);
1463 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1464 // after sending the headers
1466 if (socket.Available > 0) {
1467 bytesReceived = socket.Receive (buffer);
1474 string statusLine = null;
1475 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1476 statusLine = sr.ReadLine ();
1479 StringWriter sw = new StringWriter ();
1480 if (statusLine.StartsWith ("POST /original/")) {
1481 sw.WriteLine ("HTTP/1.0 302 Found");
1482 EndPoint ep = socket.LocalEndPoint;
1483 sw.WriteLine ("Location: " + "http://" + ep.ToString () + "/moved/");
1486 } else if (statusLine.StartsWith ("GET /moved/")) {
1487 sw.WriteLine ("HTTP/1.0 200 OK");
1488 sw.WriteLine ("Content-Type: text/plain");
1489 sw.WriteLine ("Content-Length: 8");
1491 sw.Write ("LOOKS OK");
1494 sw.WriteLine ("HTTP/1.0 500 Too Lazy");
1499 return Encoding.UTF8.GetBytes (sw.ToString ());
1502 static byte [] InternalErrorHandler (Socket socket)
1504 byte [] buffer = new byte [4096];
1505 int bytesReceived = socket.Receive (buffer);
1506 while (bytesReceived > 0) {
1507 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1508 // after sending the headers
1510 if (socket.Available > 0) {
1511 bytesReceived = socket.Receive (buffer);
1516 StringWriter sw = new StringWriter ();
1517 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1518 sw.WriteLine ("Content-Length: 0");
1522 return Encoding.UTF8.GetBytes (sw.ToString ());
1525 static byte [] NoContentLengthHandler (Socket socket)
1527 StringWriter sw = new StringWriter ();
1528 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1532 return Encoding.UTF8.GetBytes (sw.ToString ());
1535 static byte [] NotModifiedSinceHandler (Socket socket)
1537 StringWriter sw = new StringWriter ();
1538 sw.WriteLine ("HTTP/1.1 304 Not Modified");
1539 sw.WriteLine ("Date: Fri, 06 Feb 2009 12:50:26 GMT");
1540 sw.WriteLine ("Server: Apache/2.2.6 (Debian) PHP/5.2.6-2+b1 with Suhosin-Patch mod_ssl/2.2.6 OpenSSL/0.9.8g");
1541 sw.WriteLine ("Not-Modified-Since: Sun, 08 Feb 2009 08:49:26 GMT");
1542 sw.WriteLine ("ETag: 898bbr2347056cc2e096afc66e104653");
1543 sw.WriteLine ("Connection: close");
1547 return Encoding.UTF8.GetBytes (sw.ToString ());
1550 static byte [] VerbEchoHandler (Socket socket)
1552 MemoryStream ms = new MemoryStream ();
1553 byte [] buffer = new byte [4096];
1554 int bytesReceived = socket.Receive (buffer);
1555 while (bytesReceived > 0) {
1556 ms.Write (buffer, 0, bytesReceived);
1557 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1558 // after sending the headers
1560 if (socket.Available > 0) {
1561 bytesReceived = socket.Receive (buffer);
1568 string statusLine = null;
1569 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1570 statusLine = sr.ReadLine ();
1573 string verb = "DEFAULT";
1574 if (statusLine != null) {
1575 string [] parts = statusLine.Split (' ');
1576 if (parts.Length > 0)
1580 StringWriter sw = new StringWriter ();
1581 sw.WriteLine ("HTTP/1.1 200 OK");
1582 sw.WriteLine ("Content-Type: text/plain");
1583 sw.WriteLine ("Content-Length: " + verb.Length);
1588 return Encoding.UTF8.GetBytes (sw.ToString ());
1591 static byte [] PostAnd401Handler (Socket socket)
1593 MemoryStream ms = new MemoryStream ();
1594 byte [] buffer = new byte [4096];
1595 int bytesReceived = socket.Receive (buffer);
1596 while (bytesReceived > 0) {
1597 ms.Write (buffer, 0, bytesReceived);
1598 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1599 // after sending the headers
1601 if (socket.Available > 0) {
1602 bytesReceived = socket.Receive (buffer);
1609 string statusLine = null;
1610 bool have_auth = false;
1612 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1614 while ((l = sr.ReadLine ()) != null) {
1615 if (statusLine == null) {
1617 } else if (l.StartsWith ("Authorization:")) {
1619 } else if (l.StartsWith ("Content-Length:")) {
1620 cl = Int32.Parse (l.Substring ("content-length: ".Length));
1625 StringWriter sw = new StringWriter ();
1627 sw.WriteLine ("HTTP/1.0 401 Invalid Credentials");
1628 sw.WriteLine ("WWW-Authenticate: basic Yeah");
1631 } else if (cl > 0 && statusLine.StartsWith ("POST ")) {
1632 sw.WriteLine ("HTTP/1.0 200 OK");
1633 sw.WriteLine ("Content-Type: text/plain");
1634 sw.WriteLine ("Content-Length: 8");
1636 sw.Write ("LOOKS OK");
1639 sw.WriteLine ("HTTP/1.0 500 test failed");
1640 sw.WriteLine ("Content-Length: 0");
1645 return Encoding.UTF8.GetBytes (sw.ToString ());
1648 public void NtlmAuthentication ()
1650 NtlmServer server = new NtlmServer ();
1653 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
1654 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
1655 request.Timeout = 5000;
1656 request.Credentials = new NetworkCredential ("user", "password", "domain");
1657 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
1659 using (StreamReader reader = new StreamReader (resp.GetResponseStream ())) {
1660 res = reader.ReadToEnd ();
1664 Assert.AreEqual ("OK", res);
1667 class NtlmServer : HttpServer {
1668 public string Where = "";
1669 protected override void Run ()
1671 Where = "before accept";
1672 Socket client = sock.Accept ();
1673 NetworkStream ns = new NetworkStream (client, false);
1674 StreamReader reader = new StreamReader (ns, Encoding.ASCII);
1676 Where = "first read";
1677 while ((line = reader.ReadLine ()) != null) {
1678 if (line.Trim () == String.Empty) {
1682 Where = "first write";
1683 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1684 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1685 "WWW-Authenticate: NTLM\r\n" +
1686 "Content-Length: 5\r\n\r\nWRONG");
1689 Where = "second read";
1690 while ((line = reader.ReadLine ()) != null) {
1691 if (line.Trim () == String.Empty) {
1695 Where = "second write";
1696 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1697 "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAADgAAAABggAC8GDhqIONH3sAAAAAAAAAAAAAAAA4AAAABQLODgAAAA8=\r\n" +
1698 "Content-Length: 5\r\n\r\nWRONG");
1701 Where = "third read";
1702 while ((line = reader.ReadLine ()) != null) {
1703 if (line.Trim () == String.Empty) {
1707 Where = "third write";
1708 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1709 "Keep-Alive: true\r\n" +
1710 "Content-Length: 2\r\n\r\nOK");
1712 Thread.Sleep (1000);
1719 class BadChunkedServer : HttpServer {
1720 protected override void Run ()
1722 Socket client = sock.Accept ();
1723 NetworkStream ns = new NetworkStream (client, true);
1724 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1725 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1726 "Transfer-Encoding: chunked\r\n" +
1727 "Connection: close\r\n" +
1728 "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
1730 // This body lacks a 'last-chunk' (see RFC 2616)
1731 writer.Write ("10\r\n1234567890123456\r\n");
1733 client.Shutdown (SocketShutdown.Send);
1734 Thread.Sleep (1000);
1739 class AcceptAllPolicy : ICertificatePolicy {
1740 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
1746 abstract class HttpServer
1748 protected Socket sock;
1749 protected Exception error;
1750 protected ManualResetEvent evt;
1752 public HttpServer ()
1754 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
1755 sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
1759 public void Start ()
1761 evt = new ManualResetEvent (false);
1762 Thread th = new Thread (new ThreadStart (Run));
1772 public IPAddress IPAddress {
1773 get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
1777 get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
1780 public Exception Error {
1781 get { return error; }
1784 protected abstract void Run ();
1788 public void BeginGetRequestStream ()
1794 r.ContentLength = 0;
1795 r.BeginGetRequestStream ((a) =>
1797 using (Stream s = r.EndGetRequestStream (a)) { };
1806 public void BeginGetRequestStreamNoClose ()
1811 r.ContentLength = 1;
1812 r.BeginGetRequestStream ((a) =>
1814 r.EndGetRequestStream (a);
1823 public void BeginGetRequestStreamCancelIfNotAllBytesWritten ()
1829 r.ContentLength = 10;
1830 r.BeginGetRequestStream ((a) =>
1832 WebException ex = ExceptionAssert.Throws<WebException> (() =>
1834 using (Stream s = r.EndGetRequestStream (a)) {
1838 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1847 public void GetRequestStream2 ()
1853 r.ContentLength = data64KB.Length;
1854 using (Stream s = r.GetRequestStream ()) {
1855 s.Write (data64KB, 0, data64KB.Length);
1863 public void GetRequestStreamNotAllBytesWritten ()
1869 r.ContentLength = data64KB.Length;
1870 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ().Close ());
1871 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1878 public void GetRequestStreamTimeout ()
1884 r.ContentLength = data64KB.Length;
1886 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ());
1887 Assert.IsTrue (ex.Status == WebExceptionStatus.Timeout || ex.Status == WebExceptionStatus.ConnectFailure);
1893 public void BeginWrite ()
1895 byte[] received = new byte[data64KB.Length];
1901 r.ContentLength = data64KB.Length;
1903 Stream s = r.GetRequestStream ();
1904 s.BeginWrite (data64KB, 0, data64KB.Length,
1909 r.GetResponse ().Close ();
1916 c.Request.InputStream.ReadAll (received, 0, received.Length);
1917 c.Response.StatusCode = 204;
1918 c.Response.Close ();
1921 Assert.AreEqual (data64KB, received);
1925 public void BeginWriteAfterAbort ()
1927 byte [] received = new byte [data64KB.Length];
1933 r.ContentLength = data64KB.Length;
1935 Stream s = r.GetRequestStream ();
1938 WebException ex = ExceptionAssert.Throws<WebException> (() => s.BeginWrite (data64KB, 0, data64KB.Length, null, null));
1939 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1945 c.Request.InputStream.ReadAll (received, 0, received.Length);
1946 c.Response.StatusCode = 204;
1952 public void PrematureStreamCloseAborts ()
1954 byte [] received = new byte [data64KB.Length];
1960 r.ContentLength = data64KB.Length * 2;
1962 Stream s = r.GetRequestStream ();
1963 s.Write (data64KB, 0, data64KB.Length);
1965 WebException ex = ExceptionAssert.Throws<WebException>(() => s.Close());
1966 Assert.AreEqual(ex.Status, WebExceptionStatus.RequestCanceled);
1972 c.Request.InputStream.ReadAll (received, 0, received.Length);
1973 c.Response.StatusCode = 204;
1974 c.Response.Close ();
1979 public void Write ()
1981 byte [] received = new byte [data64KB.Length];
1987 r.ContentLength = data64KB.Length;
1989 using (Stream s = r.GetRequestStream ()) {
1990 s.Write (data64KB, 0, data64KB.Length);
1993 r.GetResponse ().Close ();
1998 c.Request.InputStream.ReadAll (received, 0, received.Length);
1999 c.Response.StatusCode = 204;
2000 c.Response.Close ();
2003 Assert.AreEqual(data64KB, received);
2007 Invalid test: it does not work on linux.
2008 [pid 30973] send(9, "POST / HTTP/1.1\r\nContent-Length:"..., 89, 0) = 89
2010 [pid 30970] send(16, "HTTP/1.1 200 OK\r\nServer: Mono-HT"..., 133, 0) = 133
2012 [pid 30970] close(16) = 0
2014 [pid 30980] send(9, "\213t\326\350\312u\36n\234\351\225L\r\243a\200\226\371\350F\271~oZ\32\270\24\226z4\211\345"..., 65536, 0) = 65536
2016 [pid 30966] close(4) = 0
2019 The server sideis closed (FD 16) and the send on the client side (FD 9) succeeds.
2021 [Category("NotWorking")]
2022 public void WriteServerAborts ()
2024 ManualResetEvent abort = new ManualResetEvent (false);
2025 byte [] received = new byte [data64KB.Length];
2031 r.ContentLength = data64KB.Length;
2033 using (Stream s = r.GetRequestStream()) {
2036 IOException ex = ExceptionAssert.Throws<IOException> (() => s.Write(data64KB, 0, data64KB.Length));
2052 byte [] received = new byte [data64KB.Length];
2057 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2058 using (Stream s = x.GetResponseStream()) {
2059 s.ReadAll (received, 0, received.Length);
2066 c.Response.StatusCode = 200;
2067 c.Response.ContentLength64 = data64KB.Length;
2068 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2069 c.Response.OutputStream.Close ();
2070 c.Response.Close ();
2073 Assert.AreEqual (data64KB, received);
2077 public void ReadTimeout2 ()
2079 byte [] received = new byte [data64KB.Length];
2084 r.ReadWriteTimeout = 10;
2085 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2086 using (Stream s = x.GetResponseStream ()) {
2087 WebException ex = ExceptionAssert.Throws<WebException> (() => s.ReadAll (received, 0, received.Length));
2088 Assert.AreEqual (ex.Status, WebExceptionStatus.Timeout);
2095 c.Response.StatusCode = 200;
2096 c.Response.ContentLength64 = data64KB.Length;
2097 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length / 2);
2098 Thread.Sleep (1000);
2099 c.Response.OutputStream.Write (data64KB, data64KB.Length / 2, data64KB.Length / 2);
2100 c.Response.OutputStream.Close ();
2101 c.Response.Close ();
2106 public void ReadServerAborted ()
2108 byte [] received = new byte [data64KB.Length];
2113 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2114 using (Stream s = x.GetResponseStream ()) {
2115 Assert.AreEqual (1, s.ReadAll (received, 0, received.Length));
2122 c.Response.StatusCode = 200;
2123 c.Response.ContentLength64 = data64KB.Length;
2124 c.Response.OutputStream.Write (data64KB, 0, 1);
2125 c.Response.Abort ();
2130 public void BeginGetResponse2 ()
2132 byte [] received = new byte [data64KB.Length];
2137 r.BeginGetResponse ((a) =>
2139 using (HttpWebResponse x = (HttpWebResponse) r.EndGetResponse (a))
2140 using (Stream s = x.GetResponseStream ()) {
2141 s.ReadAll (received, 0, received.Length);
2149 c.Response.StatusCode = 200;
2150 c.Response.ContentLength64 = data64KB.Length;
2151 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2152 c.Response.OutputStream.Close ();
2153 c.Response.Close ();
2156 Assert.AreEqual (data64KB, received);
2160 public void BeginGetResponseAborts ()
2162 ManualResetEvent aborted = new ManualResetEvent(false);
2167 r.BeginGetResponse((a) =>
2169 WebException ex = ExceptionAssert.Throws<WebException> (() => r.EndGetResponse (a));
2170 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2181 c.Response.StatusCode = 200;
2182 c.Response.ContentLength64 = 0;
2183 c.Response.Close ();
2187 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request)
2189 int port = rand.Next (20000, 65535);
2191 ManualResetEvent completed = new ManualResetEvent (false);
2192 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2193 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2195 request (client, completed);
2197 if (!completed.WaitOne (10000))
2198 Assert.Fail ("Test hung");
2201 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request, Action<HttpListenerContext> processor)
2203 int port = rand.Next (20000, 65535);
2205 ManualResetEvent [] completed = new ManualResetEvent [2];
2206 completed [0] = new ManualResetEvent (false);
2207 completed [1] = new ManualResetEvent (false);
2209 using (ListenerScope scope = new ListenerScope (processor, port, completed [0])) {
2210 ManualResetEvent clientCompleted = new ManualResetEvent (false);
2211 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2212 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2214 ThreadPool.QueueUserWorkItem ((o) => request (client, completed [1]));
2216 if (!WaitHandle.WaitAll (completed, 10000))
2217 Assert.Fail ("Test hung.");
2223 [ExpectedException (typeof (ArgumentNullException))]
2224 public void NullHost ()
2226 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2231 public void NoHost ()
2233 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2234 Assert.AreEqual (req.Host, "go-mono.com");
2238 [ExpectedException (typeof (ArgumentException))]
2239 public void EmptyHost ()
2241 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2246 public void HostAndPort ()
2248 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:80");
2249 Assert.AreEqual ("go-mono.com", req.Host, "#01");
2250 req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:9000");
2251 Assert.AreEqual ("go-mono.com:9000", req.Host, "#02");
2255 public void PortRange ()
2257 for (int i = 0; i < 65536; i++) {
2260 string s = i.ToString ();
2261 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:" + s);
2262 Assert.AreEqual ("go-mono.com:" + s, req.Host, "#" + s);
2267 [ExpectedException (typeof (ArgumentException))]
2268 public void PortBelow ()
2270 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2271 req.Host = "go-mono.com:-1";
2275 [ExpectedException (typeof (ArgumentException))]
2276 public void PortAbove ()
2278 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2279 req.Host = "go-mono.com:65536";
2283 [ExpectedException (typeof (ArgumentException))]
2284 public void HostTooLong ()
2286 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2287 string s = new string ('a', 100);
2288 req.Host = s + "." + s + "." + s + "." + s + "." + s + "." + s; // Over 255 bytes
2292 public void InvalidNamesThatWork ()
2294 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2298 req.Host = new string ('a', 64); // Should fail. Max. is 63.
2302 public void NoDate ()
2304 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2305 Assert.AreEqual (DateTime.MinValue, req.Date);
2309 public void UtcDate ()
2311 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2312 req.Date = DateTime.UtcNow;
2313 DateTime date = req.Date;
2314 Assert.AreEqual (DateTimeKind.Local, date.Kind);
2318 public void AddAndRemoveDate ()
2320 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2321 req.Date = DateTime.UtcNow;
2322 req.Date = DateTime.MinValue;
2323 Assert.AreEqual (DateTime.MinValue, req.Date);
2326 class ListenerScope : IDisposable {
2327 EventWaitHandle completed;
2328 public HttpListener listener;
2329 Action<HttpListenerContext> processor;
2331 public ListenerScope (Action<HttpListenerContext> processor, int port, EventWaitHandle completed)
2333 this.processor = processor;
2334 this.completed = completed;
2336 this.listener = new HttpListener ();
2337 this.listener.Prefixes.Add (string.Format ("http://localhost:{0}/", port));
2338 this.listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
2339 this.listener.Start ();
2341 this.listener.BeginGetContext (this.RequestHandler, null);
2344 void RequestHandler (IAsyncResult result)
2346 HttpListenerContext context = null;
2349 context = this.listener.EndGetContext (result);
2350 } catch (HttpListenerException ex) {
2351 // check if the thread has been aborted as in the case when we are shutting down.
2352 if (ex.ErrorCode == 995)
2354 } catch (ObjectDisposedException) {
2358 ThreadPool.QueueUserWorkItem ((o) =>
2361 this.processor (context);
2362 } catch (HttpListenerException) {
2366 this.completed.Set ();
2369 public void Dispose ()
2371 this.listener.Stop ();
2376 class SslHttpServer : HttpServer {
2377 X509Certificate _certificate;
2379 protected override void Run ()
2382 Socket client = sock.Accept ();
2383 NetworkStream ns = new NetworkStream (client, true);
2384 SslServerStream s = new SslServerStream (ns, Certificate, false, false);
2385 s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
2387 StreamReader reader = new StreamReader (s);
2388 StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
2391 string hello = "<html><body><h1>Hello World!</h1></body></html>";
2392 string answer = "HTTP/1.0 200\r\n" +
2393 "Connection: close\r\n" +
2394 "Content-Type: text/html\r\n" +
2395 "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
2396 "Content-Length: " + hello.Length + "\r\n" +
2401 line = reader.ReadLine ();
2402 } while (line != "" && line != null && line.Length > 0);
2404 // Now the content. We know it's 100 bytes.
2405 // This makes BeginRead in sslclientstream block.
2406 char [] cs = new char [100];
2407 reader.Read (cs, 0, 100);
2409 writer.Write (answer);
2411 if (evt.WaitOne (5000, false))
2412 error = new Exception ("Timeout when stopping the server");
2413 } catch (Exception e) {
2418 X509Certificate Certificate {
2420 if (_certificate == null)
2421 _certificate = new X509Certificate (CertData.Certificate);
2423 return _certificate;
2427 AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
2429 PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
2435 public readonly static byte [] Certificate = {
2436 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
2437 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
2438 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
2439 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
2440 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
2441 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
2442 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
2443 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
2444 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
2445 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
2446 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
2447 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
2448 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
2449 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
2450 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
2451 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
2452 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
2453 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
2454 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
2455 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
2456 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
2457 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
2458 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
2459 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
2460 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
2461 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
2462 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
2463 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
2467 public readonly static byte [] PrivateKey = {
2468 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
2469 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
2470 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
2471 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
2472 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
2473 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
2474 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
2475 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
2476 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
2477 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
2478 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
2479 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
2480 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
2481 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
2482 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
2483 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
2484 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
2485 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
2486 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
2487 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
2488 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
2489 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
2490 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
2491 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
2492 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
2493 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
2494 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
2495 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
2496 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
2497 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
2498 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
2499 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
2500 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
2501 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
2502 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
2503 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
2504 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
2505 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
2506 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
2513 public class HttpRequestStreamTest
2516 public void BeginRead ()
2518 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9124);
2519 string url = "http://" + ep.ToString () + "/test/";
2521 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2524 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2525 req.Method = "POST";
2527 using (Stream rs = req.GetRequestStream ()) {
2528 byte [] buffer = new byte [10];
2530 rs.BeginRead (buffer, 0, buffer.Length, null, null);
2532 } catch (NotSupportedException ex) {
2533 // The stream does not support reading
2534 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2535 Assert.IsNull (ex.InnerException, "#3");
2536 Assert.IsNotNull (ex.Message, "#4");
2545 public void BeginWrite_Request_Aborted ()
2547 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9125);
2548 string url = "http://" + ep.ToString () + "/test/";
2550 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2553 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2554 req.Method = "POST";
2556 using (Stream rs = req.GetRequestStream ()) {
2559 rs.BeginWrite (new byte [] { 0x2a, 0x2f }, 0, 2, null, null);
2561 } catch (WebException ex) {
2562 // The request was aborted: The request was canceled
2563 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
2564 Assert.IsNull (ex.InnerException, "#3");
2565 Assert.IsNotNull (ex.Message, "#4");
2566 Assert.IsNull (ex.Response, "#5");
2567 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
2574 public void CanRead ()
2576 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9126);
2577 string url = "http://" + ep.ToString () + "/test/";
2579 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2582 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2583 req.Method = "POST";
2585 Stream rs = req.GetRequestStream ();
2587 Assert.IsFalse (rs.CanRead, "#1");
2589 Assert.IsFalse (rs.CanRead, "#2");
2598 public void CanSeek ()
2600 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9127);
2601 string url = "http://" + ep.ToString () + "/test/";
2603 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2606 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2607 req.Method = "POST";
2609 Stream rs = req.GetRequestStream ();
2611 Assert.IsFalse (rs.CanSeek, "#1");
2613 Assert.IsFalse (rs.CanSeek, "#2");
2621 [Test] // bug #324182
2623 [Category ("NotWorking")]
2625 public void CanTimeout ()
2627 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9128);
2628 string url = "http://" + ep.ToString () + "/test/";
2630 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2633 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2634 req.Method = "POST";
2636 Stream rs = req.GetRequestStream ();
2638 Assert.IsTrue (rs.CanTimeout, "#1");
2640 Assert.IsTrue (rs.CanTimeout, "#2");
2649 public void CanWrite ()
2651 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9129);
2652 string url = "http://" + ep.ToString () + "/test/";
2654 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2657 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2658 req.Method = "POST";
2660 Stream rs = req.GetRequestStream ();
2662 Assert.IsTrue (rs.CanWrite, "#1");
2664 Assert.IsFalse (rs.CanWrite, "#2");
2675 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9130);
2676 string url = "http://" + ep.ToString () + "/test/";
2678 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2681 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2682 req.Method = "POST";
2684 using (Stream rs = req.GetRequestStream ()) {
2685 byte [] buffer = new byte [10];
2687 rs.Read (buffer, 0, buffer.Length);
2689 } catch (NotSupportedException ex) {
2690 // The stream does not support reading
2691 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2692 Assert.IsNull (ex.InnerException, "#3");
2693 Assert.IsNotNull (ex.Message, "#4");
2702 public void ReadByte ()
2704 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9140);
2705 string url = "http://" + ep.ToString () + "/test/";
2707 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2710 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2711 req.Method = "POST";
2713 using (Stream rs = req.GetRequestStream ()) {
2717 } catch (NotSupportedException ex) {
2718 // The stream does not support reading
2719 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2720 Assert.IsNull (ex.InnerException, "#3");
2721 Assert.IsNotNull (ex.Message, "#4");
2730 public void ReadTimeout ()
2732 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9141);
2733 string url = "http://" + ep.ToString () + "/test/";
2735 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2738 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2739 req.Method = "POST";
2741 Stream rs = req.GetRequestStream ();
2743 Assert.AreEqual (300000, rs.ReadTimeout, "#1");
2745 Assert.AreEqual (300000, rs.ReadTimeout, "#2");
2756 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9142);
2757 string url = "http://" + ep.ToString () + "/test/";
2759 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2762 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2763 req.Method = "POST";
2765 using (Stream rs = req.GetRequestStream ()) {
2767 rs.Seek (0, SeekOrigin.Current);
2769 } catch (NotSupportedException ex) {
2770 // This stream does not support seek operations
2771 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2772 Assert.IsNull (ex.InnerException, "#3");
2773 Assert.IsNotNull (ex.Message, "#4");
2782 public void Write_Buffer_Null ()
2784 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9143);
2785 string url = "http://" + ep.ToString () + "/test/";
2787 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2790 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2791 req.Method = "POST";
2793 using (Stream rs = req.GetRequestStream ()) {
2795 rs.Write ((byte []) null, -1, -1);
2797 } catch (ArgumentNullException ex) {
2798 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2799 Assert.IsNull (ex.InnerException, "#3");
2800 Assert.IsNotNull (ex.Message, "#4");
2801 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2810 public void Write_Count_Negative ()
2812 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9144);
2813 string url = "http://" + ep.ToString () + "/test/";
2815 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2818 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2819 req.Method = "POST";
2821 using (Stream rs = req.GetRequestStream ()) {
2822 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
2824 rs.Write (buffer, 1, -1);
2826 } catch (ArgumentOutOfRangeException ex) {
2827 // Specified argument was out of the range of valid values
2828 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
2829 Assert.IsNull (ex.InnerException, "#A3");
2830 Assert.IsNotNull (ex.Message, "#A4");
2831 Assert.AreEqual ("size", ex.ParamName, "#A5");
2840 public void Write_Count_Overflow ()
2842 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9145);
2843 string url = "http://" + ep.ToString () + "/test/";
2845 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2848 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2849 req.Method = "POST";
2851 using (Stream rs = req.GetRequestStream ()) {
2852 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
2854 rs.Write (buffer, buffer.Length - 2, 3);
2856 } catch (ArgumentOutOfRangeException ex) {
2857 // Specified argument was out of the range of valid values
2858 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
2859 Assert.IsNull (ex.InnerException, "#3");
2860 Assert.IsNotNull (ex.Message, "#4");
2861 Assert.AreEqual ("size", ex.ParamName, "#5");
2870 public void Write_Offset_Negative ()
2872 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9146);
2873 string url = "http://" + ep.ToString () + "/test/";
2875 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2878 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2879 req.Method = "POST";
2881 using (Stream rs = req.GetRequestStream ()) {
2882 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
2884 rs.Write (buffer, -1, 0);
2886 } catch (ArgumentOutOfRangeException ex) {
2887 // Specified argument was out of the range of valid values
2888 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
2889 Assert.IsNull (ex.InnerException, "#3");
2890 Assert.IsNotNull (ex.Message, "#4");
2891 Assert.AreEqual ("offset", ex.ParamName, "#5");
2900 public void Write_Offset_Overflow ()
2902 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9147);
2903 string url = "http://" + ep.ToString () + "/test/";
2905 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2908 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2909 req.Method = "POST";
2911 using (Stream rs = req.GetRequestStream ()) {
2912 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
2914 rs.Write (buffer, buffer.Length + 1, 0);
2916 } catch (ArgumentOutOfRangeException ex) {
2917 // Specified argument was out of the range of valid values
2918 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
2919 Assert.IsNull (ex.InnerException, "#3");
2920 Assert.IsNotNull (ex.Message, "#4");
2921 Assert.AreEqual ("offset", ex.ParamName, "#5");
2930 public void Write_Request_Aborted ()
2932 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9148);
2933 string url = "http://" + ep.ToString () + "/test/";
2935 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2938 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2939 req.Method = "POST";
2941 using (Stream rs = req.GetRequestStream ()) {
2944 rs.Write (new byte [0], 0, 0);
2946 } catch (WebException ex) {
2947 // The request was aborted: The request was canceled
2948 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
2949 Assert.IsNull (ex.InnerException, "#3");
2950 Assert.IsNotNull (ex.Message, "#4");
2951 Assert.IsNull (ex.Response, "#5");
2952 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
2959 [Category ("NotWorking")]
2960 public void Write_Stream_Closed ()
2962 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9149);
2963 string url = "http://" + ep.ToString () + "/test/";
2965 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2968 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2969 req.Method = "POST";
2971 using (Stream rs = req.GetRequestStream ()) {
2974 rs.Write (new byte [0], 0, 0);
2976 } catch (WebException ex) {
2977 // The request was aborted: The connection was closed unexpectedly
2978 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
2979 Assert.IsNull (ex.InnerException, "#3");
2980 Assert.IsNotNull (ex.Message, "#4");
2981 Assert.IsNull (ex.Response, "#5");
2982 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#6");
2989 public void WriteByte_Request_Aborted ()
2991 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9150);
2992 string url = "http://" + ep.ToString () + "/test/";
2994 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2997 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2998 req.Method = "POST";
3000 using (Stream rs = req.GetRequestStream ()) {
3003 rs.WriteByte (0x2a);
3005 } catch (WebException ex) {
3006 // The request was aborted: The request was canceled
3007 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3008 Assert.IsNull (ex.InnerException, "#3");
3009 Assert.IsNotNull (ex.Message, "#4");
3010 Assert.IsNull (ex.Response, "#5");
3011 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
3018 public void WriteTimeout ()
3020 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9151);
3021 string url = "http://" + ep.ToString () + "/test/";
3023 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3026 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3027 req.Method = "POST";
3029 Stream rs = req.GetRequestStream ();
3031 Assert.AreEqual (300000, rs.WriteTimeout, "#1");
3033 Assert.AreEqual (300000, rs.WriteTimeout, "#2");
3042 static class StreamExtensions {
3043 public static int ReadAll(this Stream stream, byte[] buffer, int offset, int count)
3047 while (totalRead < count) {
3048 int bytesRead = stream.Read (buffer, offset + totalRead, count - totalRead);
3052 totalRead += bytesRead;
3059 static class ExceptionAssert {
3061 /// Asserts that the function throws an exception.
3063 /// <param name="f">A function execute that is expected to raise an exception.</param>
3064 /// <typeparam name="T">The type of exception that is expected.</typeparam>
3065 /// <returns>The exception thrown.</returns>
3066 /// <exception cref="AssertFailedException">If the function does not throw an exception
3067 /// or throws a different exception.</exception>
3068 /// <example><![CDATA[
3069 /// ExceptionAssert.Throws(typeof(ArgumentNullException), delegate {
3070 /// myObject.myFunction(null); });
3072 public static T Throws<T> (Action f) where T : Exception {
3073 Exception actualException = null;
3077 } catch (Exception ex) {
3078 actualException = ex;
3081 if (actualException == null)
3082 throw new AssertionException (string.Format (
3083 "No exception thrown. Expected '{0}'",
3084 typeof (T).FullName));
3085 else if (typeof(T) != actualException.GetType())
3086 throw new AssertionException (string.Format (
3087 "Caught exception of type '{0}'. Expected '{1}':{2}",
3088 actualException.GetType().FullName,
3089 typeof (T).FullName,
3090 Environment.NewLine + actualException));
3092 return (T) actualException;