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)
8 // Andres G. Aragoneses (andres@7digital.com)
10 // (C) 2003 Martin Willemoes Hansen
11 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com
12 // Copyright (c) 2013 7digital Media Ltd (http://www.7digital.com)
15 using NUnit.Framework;
17 using System.Collections;
18 using System.Collections.Specialized;
19 using System.Globalization;
22 using System.Net.Sockets;
23 using System.Security.Cryptography;
24 using System.Security.Cryptography.X509Certificates;
26 using System.Threading;
27 using Mono.Security.Authenticode;
29 using Mono.Security.Protocol.Tls;
32 namespace MonoTests.System.Net
35 public class HttpWebRequestTest
37 private Random rand = new Random ();
38 private byte [] data64KB = new byte [64 * 1024];
43 ServicePointManager.Expect100Continue = false;
44 rand.NextBytes (data64KB);
49 [Ignore ("Ignore failures in Sys.Net")]
51 public void Proxy_Null ()
53 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
54 Assert.IsNotNull (req.Proxy, "#1");
56 Assert.IsNull (req.Proxy, "#2");
60 [Category("InetAccess")]
62 [Ignore ("NMA - wrong cookies number returned")]
66 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
67 Assert.IsNotNull (req.IfModifiedSince, "req:If Modified Since: ");
69 req.UserAgent = "MonoClient v1.0";
70 Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
71 Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
73 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
74 Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
75 Assert.AreEqual ("OK", res.StatusDescription, "#B2");
77 Assert.AreEqual ("text/html; charset=ISO-8859-1", res.Headers.Get ("Content-Type"), "#C1");
78 Assert.IsNotNull (res.LastModified, "#C2");
79 Assert.AreEqual (0, res.Cookies.Count, "#C3");
85 public void AddRange ()
87 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
89 req.AddRange (50, 90);
90 req.AddRange ("bytes", 100);
91 req.AddRange ("bytes", 100, 120);
92 Assert.AreEqual ("bytes=10-,50-90,100-,100-120", req.Headers ["Range"], "#1");
94 req.AddRange ("bits", 2000);
96 } catch (InvalidOperationException) {}
100 public void CloseRequestStreamAfterReadingResponse ()
102 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9152);
103 string url = "http://" + ep.ToString () + "/test/";
105 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
108 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
111 req.ReadWriteTimeout = 2000;
113 byte [] data = new byte [128];
114 req.ContentLength = data.Length;
116 Stream rs = req.GetRequestStream ();
117 rs.Write (data, 0, data.Length);
120 HttpWebResponse response = (HttpWebResponse) req.GetResponse ();
130 [Category("InetAccess")]
131 public void Cookies1 ()
133 // The purpose of this test is to ensure that the cookies we get from a request
134 // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer
135 // in HttpWebRequest.
136 // If this URL stops sending *one* and only one cookie, replace it.
137 string url = "http://www.elmundo.es";
138 CookieContainer cookies = new CookieContainer ();
139 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
140 req.KeepAlive = false;
141 req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2";
142 req.CookieContainer = cookies;
143 Assert.AreEqual (0, cookies.Count, "#01");
144 using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) {
145 CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url));
146 Assert.AreEqual (1, coll.Count, "#02");
147 Assert.AreEqual (1, res.Cookies.Count, "#03");
148 Cookie one = coll [0];
149 Cookie two = res.Cookies [0];
150 Assert.AreEqual (true, object.ReferenceEquals (one, two), "#04");
154 #if !TARGET_JVM && !MOBILE
156 [Ignore ("Fails on MS.NET")]
157 public void SslClientBlock ()
159 // This tests that the write request/initread/write body sequence does not hang
161 // If there's a regression for this, the test will hang.
162 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
164 SslHttpServer server = new SslHttpServer ();
167 string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port);
168 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
169 request.Method = "POST";
170 Stream stream = request.GetRequestStream ();
171 byte [] bytes = new byte [100];
172 stream.Write (bytes, 0, bytes.Length);
174 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
175 Assert.AreEqual (200, (int) resp.StatusCode, "StatusCode");
176 StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
181 if (server.Error != null)
184 ServicePointManager.CertificatePolicy = null;
190 [Category("NotWorking")]
192 public void Missing_ContentEncoding ()
194 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
196 BadChunkedServer server = new BadChunkedServer ();
199 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
200 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
201 request.Method = "GET";
202 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
203 Assert.AreEqual ("", resp.ContentEncoding);
206 if (server.Error != null)
209 ServicePointManager.CertificatePolicy = null;
215 [Category ("NotWorking")]
217 public void BadServer_ChunkedClose ()
219 // The server will send a chunked response without a 'last-chunked' mark
220 // and then shutdown the socket for sending.
221 BadChunkedServer server = new BadChunkedServer ();
223 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
224 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
225 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
228 byte [] bytes = new byte [32];
229 // Using StreamReader+UTF8Encoding here fails on MS runtime
230 Stream stream = resp.GetResponseStream ();
231 int nread = stream.Read (bytes, 0, 32);
232 Assert.AreEqual (16, nread, "#01");
233 x = Encoding.ASCII.GetString (bytes, 0, 16);
239 if (server.Error != null)
242 Assert.AreEqual ("1234567890123456", x);
246 [Ignore ("This test asserts that our code violates RFC 2616")]
247 public void MethodCase ()
249 ListDictionary methods = new ListDictionary ();
250 methods.Add ("post", "POST");
251 methods.Add ("puT", "PUT");
252 methods.Add ("POST", "POST");
253 methods.Add ("whatever", "whatever");
254 methods.Add ("PUT", "PUT");
256 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9153);
257 string url = "http://" + ep.ToString () + "/test/";
259 foreach (DictionaryEntry de in methods) {
260 SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler));
263 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
264 req.Method = (string) de.Key;
266 req.ReadWriteTimeout = 2000;
267 req.KeepAlive = false;
268 Stream rs = req.GetRequestStream ();
270 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
271 StreamReader sr = new StreamReader (resp.GetResponseStream (),
273 string line = sr.ReadLine ();
275 Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
284 public void BeginGetRequestStream_Body_NotAllowed ()
286 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9154);
287 string url = "http://" + ep.ToString () + "/test/";
289 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
292 HttpWebRequest request;
294 request = (HttpWebRequest) WebRequest.Create (url);
295 request.Method = "GET";
298 request.BeginGetRequestStream (null, null);
300 } catch (ProtocolViolationException ex) {
301 // Cannot send a content-body with this
303 Assert.IsNull (ex.InnerException, "#A2");
304 Assert.IsNotNull (ex.Message, "#A3");
307 request = (HttpWebRequest) WebRequest.Create (url);
308 request.Method = "HEAD";
311 request.BeginGetRequestStream (null, null);
313 } catch (ProtocolViolationException ex) {
314 // Cannot send a content-body with this
316 Assert.IsNull (ex.InnerException, "#B2");
317 Assert.IsNotNull (ex.Message, "#B3");
322 [Test] // bug #465613
323 public void BeginGetRequestStream_NoBuffering ()
325 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 11001);
326 string url = "http://" + ep.ToString () + "/test/";
328 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
335 req = (HttpWebRequest) WebRequest.Create (url);
337 req.SendChunked = false;
338 req.KeepAlive = false;
339 req.AllowWriteStreamBuffering = false;
341 ar = req.BeginGetRequestStream (null, null);
342 rs = req.EndGetRequestStream (ar);
345 req = (HttpWebRequest) WebRequest.Create (url);
347 req.SendChunked = false;
348 req.KeepAlive = true;
349 req.AllowWriteStreamBuffering = false;
352 req.BeginGetRequestStream (null, null);
354 } catch (ProtocolViolationException ex) {
355 // When performing a write operation with
356 // AllowWriteStreamBuffering set to false,
357 // you must either set ContentLength to a
358 // non-negative number or set SendChunked
360 Assert.IsNull (ex.InnerException, "#A2");
361 Assert.IsNotNull (ex.Message, "#A3");
364 req = (HttpWebRequest) WebRequest.Create (url);
366 req.SendChunked = false;
367 req.KeepAlive = true;
368 req.AllowWriteStreamBuffering = false;
369 req.ContentLength = 0;
371 ar = req.BeginGetRequestStream (null, null);
372 rs = req.EndGetRequestStream (ar);
377 [Test] // bug #508027
378 [Category ("NotWorking")] // #5842
379 public void BeginGetResponse ()
381 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8001);
382 string url = "http://" + ep.ToString () + "/test/";
384 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
389 req = (HttpWebRequest) WebRequest.Create (url);
392 req.SendChunked = false;
393 req.KeepAlive = false;
394 req.AllowWriteStreamBuffering = false;
395 req.BeginGetResponse (null, null);
398 req = (HttpWebRequest) WebRequest.Create (url);
401 req.SendChunked = true;
402 req.KeepAlive = false;
403 req.AllowWriteStreamBuffering = false;
404 req.GetRequestStream ().WriteByte (1);
405 req.BeginGetResponse (null, null);
408 req = (HttpWebRequest) WebRequest.Create (url);
411 req.ContentLength = 5;
412 req.SendChunked = false;
413 req.KeepAlive = false;
414 req.AllowWriteStreamBuffering = false;
415 req.GetRequestStream ().WriteByte (5);
416 req.BeginGetResponse (null, null);
419 req = (HttpWebRequest) WebRequest.Create (url);
422 req.SendChunked = false;
423 req.KeepAlive = true;
424 req.AllowWriteStreamBuffering = false;
426 req.BeginGetResponse (null, null);
429 req = (HttpWebRequest) WebRequest.Create (url);
432 req.SendChunked = false;
433 req.KeepAlive = false;
434 req.AllowWriteStreamBuffering = false;
435 req.ContentLength = 5;
436 req.BeginGetResponse (null, null);
439 req = (HttpWebRequest) WebRequest.Create (url);
442 req.SendChunked = false;
443 req.KeepAlive = true;
444 req.AllowWriteStreamBuffering = false;
445 req.ContentLength = 5;
446 req.BeginGetResponse (null, null);
449 req = (HttpWebRequest) WebRequest.Create (url);
452 req.SendChunked = true;
454 req.BeginGetResponse (null, null);
457 req = (HttpWebRequest) WebRequest.Create (url);
460 req.ContentLength = 5;
462 req.BeginGetResponse (null, null);
465 req = (HttpWebRequest) WebRequest.Create (url);
468 req.ContentLength = 0;
470 req.BeginGetResponse (null, null);
475 [Test] // bug #511851
476 public void BeginGetRequestStream_Request_Aborted ()
478 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8002);
479 string url = "http://" + ep.ToString () + "/test/";
481 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
484 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
489 req.BeginGetRequestStream (null, null);
491 } catch (WebException ex) {
492 // The request was aborted: The request was canceled
493 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
494 Assert.IsNull (ex.InnerException, "#3");
495 Assert.IsNotNull (ex.Message, "#4");
496 Assert.IsNull (ex.Response, "#5");
497 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
502 [Test] // bug #511851
503 public void BeginGetResponse_Request_Aborted ()
505 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9155);
506 string url = "http://" + ep.ToString () + "/test/";
508 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
511 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
516 req.BeginGetResponse (null, null);
518 } catch (WebException ex) {
519 // The request was aborted: The request was canceled
520 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
521 Assert.IsNull (ex.InnerException, "#3");
522 Assert.IsNotNull (ex.Message, "#4");
523 Assert.IsNull (ex.Response, "#5");
524 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
530 public void EndGetRequestStream_AsyncResult_Null ()
532 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9156);
533 string url = "http://" + ep.ToString () + "/test/";
535 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
538 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
540 req.BeginGetRequestStream (null, null);
543 req.EndGetRequestStream (null);
545 } catch (ArgumentNullException ex) {
546 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
547 Assert.IsNull (ex.InnerException, "#3");
548 Assert.IsNotNull (ex.Message, "#4");
549 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
557 [Category ("NotWorking")] // do not get consistent result on MS
558 public void EndGetRequestStream_Request_Aborted ()
560 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8003);
561 string url = "http://" + ep.ToString () + "/test/";
563 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
566 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
568 IAsyncResult ar = req.BeginGetRequestStream (null, null);
573 req.EndGetRequestStream (ar);
575 } catch (WebException ex) {
576 // The request was aborted: The request was canceled
577 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
578 Assert.IsNull (ex.InnerException, "#3");
579 Assert.IsNotNull (ex.Message, "#4");
580 Assert.IsNull (ex.Response, "#5");
581 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
586 [Test] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=471522
587 [Category ("NotWorking")]
588 public void EndGetResponse_AsyncResult_Invalid ()
590 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9157);
591 string url = "http://" + ep.ToString () + "/test/";
593 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
596 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
599 req.ReadWriteTimeout = 2000;
600 IAsyncResult ar = req.BeginGetRequestStream (null, null);
602 // AsyncResult was not returned from call to BeginGetResponse
604 req.EndGetResponse (ar);
606 } catch (InvalidCastException) {
614 public void EndGetResponse_AsyncResult_Null ()
616 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9158);
617 string url = "http://" + ep.ToString () + "/test/";
619 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
622 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
624 req.ReadWriteTimeout = 2000;
626 IAsyncResult ar = req.BeginGetResponse (null, null);
629 req.EndGetResponse (null);
631 } catch (ArgumentNullException ex) {
632 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
633 Assert.IsNull (ex.InnerException, "#3");
634 Assert.IsNotNull (ex.Message, "#4");
635 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
639 using (HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse (ar)) {
646 [Test] // bug #429200
647 public void GetRequestStream ()
649 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10000);
650 string url = "http://" + ep.ToString () + "/test/";
652 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
655 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
658 req.ReadWriteTimeout = 2000;
660 Stream rs1 = req.GetRequestStream ();
661 Stream rs2 = req.GetRequestStream ();
663 Assert.IsNotNull (rs1, "#1");
664 Assert.AreSame (rs1, rs2, "#2");
670 [Test] // bug #511851
671 public void GetRequestStream_Request_Aborted ()
673 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10001);
674 string url = "http://" + ep.ToString () + "/test/";
676 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
679 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
684 req.GetRequestStream ();
686 } catch (WebException ex) {
687 // The request was aborted: The request was canceled
688 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
689 Assert.IsNull (ex.InnerException, "#3");
690 Assert.IsNotNull (ex.Message, "#4");
691 Assert.IsNull (ex.Response, "#5");
692 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
697 [Test] // bug #510661
698 [Category ("NotWorking")] // #5842
699 public void GetRequestStream_Close_NotAllBytesWritten ()
701 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10002);
702 string url = "http://" + ep.ToString () + "/test/";
704 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
710 req = (HttpWebRequest) WebRequest.Create (url);
712 req.ContentLength = 2;
713 rs = req.GetRequestStream ();
717 } catch (WebException ex) {
718 // The request was aborted: The request was canceled
719 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
720 Assert.IsNotNull (ex.Message, "#A3");
721 Assert.IsNull (ex.Response, "#A4");
722 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#A5");
724 // Cannot close stream until all bytes are written
725 Exception inner = ex.InnerException;
726 Assert.IsNotNull (inner, "#A6");
727 Assert.AreEqual (typeof (IOException), inner.GetType (), "#A7");
728 Assert.IsNull (inner.InnerException, "#A8");
729 Assert.IsNotNull (inner.Message, "#A9");
732 req = (HttpWebRequest) WebRequest.Create (url);
734 req.ContentLength = 2;
735 rs = req.GetRequestStream ();
740 } catch (WebException ex) {
741 // The request was aborted: The request was canceled
742 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
743 Assert.IsNotNull (ex.Message, "#B3");
744 Assert.IsNull (ex.Response, "#B4");
745 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#B5");
747 // Cannot close stream until all bytes are written
748 Exception inner = ex.InnerException;
749 Assert.IsNotNull (inner, "#B6");
750 Assert.AreEqual (typeof (IOException), inner.GetType (), "#B7");
751 Assert.IsNull (inner.InnerException, "#B8");
752 Assert.IsNotNull (inner.Message, "#B9");
755 req = (HttpWebRequest) WebRequest.Create (url);
757 req.ContentLength = 2;
758 rs = req.GetRequestStream ();
765 [Test] // bug #510642
766 [Category ("NotWorking")] // #5842
767 public void GetRequestStream_Write_Overflow ()
769 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8010);
770 string url = "http://" + ep.ToString () + "/test/";
772 // buffered, non-chunked
773 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
780 req = (HttpWebRequest) WebRequest.Create (url);
783 req.ReadWriteTimeout = 2000;
784 req.ContentLength = 2;
786 rs = req.GetRequestStream ();
789 buffer = new byte [] { 0x2a, 0x1d };
791 rs.Write (buffer, 0, buffer.Length);
793 } catch (ProtocolViolationException ex) {
794 // Bytes to be written to the stream exceed
795 // Content-Length bytes size specified
796 Assert.IsNull (ex.InnerException, "#A2");
797 Assert.IsNotNull (ex.Message, "#A3");
802 req = (HttpWebRequest) WebRequest.Create (url);
805 req.ReadWriteTimeout = 2000;
806 req.ContentLength = 2;
808 rs = req.GetRequestStream ();
810 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
812 rs.Write (buffer, 0, buffer.Length);
814 } catch (ProtocolViolationException ex) {
815 // Bytes to be written to the stream exceed
816 // Content-Length bytes size specified
817 Assert.IsNull (ex.InnerException, "#B2");
818 Assert.IsNotNull (ex.Message, "#B3");
825 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
833 req = (HttpWebRequest) WebRequest.Create (url);
835 req.SendChunked = true;
837 req.ReadWriteTimeout = 2000;
838 req.ContentLength = 2;
840 rs = req.GetRequestStream ();
843 buffer = new byte [] { 0x2a, 0x1d };
844 rs.Write (buffer, 0, buffer.Length);
848 req = (HttpWebRequest) WebRequest.Create (url);
850 req.SendChunked = true;
852 req.ReadWriteTimeout = 2000;
853 req.ContentLength = 2;
855 rs = req.GetRequestStream ();
857 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
858 rs.Write (buffer, 0, buffer.Length);
862 // non-buffered, non-chunked
863 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
870 req = (HttpWebRequest) WebRequest.Create (url);
871 req.AllowWriteStreamBuffering = false;
874 req.ReadWriteTimeout = 2000;
875 req.ContentLength = 2;
877 rs = req.GetRequestStream ();
880 buffer = new byte [] { 0x2a, 0x1d };
882 rs.Write (buffer, 0, buffer.Length);
884 } catch (ProtocolViolationException ex) {
885 // Bytes to be written to the stream exceed
886 // Content-Length bytes size specified
887 Assert.IsNull (ex.InnerException, "#C2");
888 Assert.IsNotNull (ex.Message, "#3");
893 req = (HttpWebRequest) WebRequest.Create (url);
894 req.AllowWriteStreamBuffering = false;
897 req.ReadWriteTimeout = 2000;
898 req.ContentLength = 2;
900 rs = req.GetRequestStream ();
902 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
904 rs.Write (buffer, 0, buffer.Length);
906 } catch (ProtocolViolationException ex) {
907 // Bytes to be written to the stream exceed
908 // Content-Length bytes size specified
909 Assert.IsNull (ex.InnerException, "#D2");
910 Assert.IsNotNull (ex.Message, "#D3");
916 // non-buffered, chunked
917 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
924 req = (HttpWebRequest) WebRequest.Create (url);
925 req.AllowWriteStreamBuffering = false;
927 req.SendChunked = true;
929 req.ReadWriteTimeout = 2000;
930 req.ContentLength = 2;
932 rs = req.GetRequestStream ();
935 buffer = new byte [] { 0x2a, 0x1d };
936 rs.Write (buffer, 0, buffer.Length);
939 req = (HttpWebRequest) WebRequest.Create (url);
940 req.AllowWriteStreamBuffering = false;
942 req.SendChunked = true;
944 req.ReadWriteTimeout = 2000;
945 req.ContentLength = 2;
947 rs = req.GetRequestStream ();
949 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
950 rs.Write (buffer, 0, buffer.Length);
956 [Ignore ("This test asserts that our code violates RFC 2616")]
957 public void GetRequestStream_Body_NotAllowed ()
959 string [] methods = new string [] { "GET", "HEAD", "CONNECT",
960 "get", "HeAd", "ConNect" };
962 foreach (string method in methods) {
963 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
964 "http://localhost:8000");
967 req.GetRequestStream ();
968 Assert.Fail ("#1:" + method);
969 } catch (ProtocolViolationException ex) {
970 Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
971 Assert.IsNull (ex.InnerException, "#3:" + method);
972 Assert.IsNotNull (ex.Message, "#4:" + method);
977 [Test] // bug #511851
978 public void GetResponse_Request_Aborted ()
980 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 10100);
981 string url = "http://" + ep.ToString () + "/test/";
983 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
986 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
993 } catch (WebException ex) {
994 // The request was aborted: The request was canceled
995 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
996 Assert.IsNull (ex.InnerException, "#3");
997 Assert.IsNotNull (ex.Message, "#4");
998 Assert.IsNull (ex.Response, "#5");
999 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
1006 [Category("NotWorking")]
1008 [Ignore ("This does not timeout any more. That's how MS works when reading small responses")]
1009 public void ReadTimeout ()
1011 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
1012 string url = "http://" + localEP.ToString () + "/original/";
1014 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1017 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1018 req.Method = "POST";
1019 req.AllowAutoRedirect = false;
1021 req.ReadWriteTimeout = 2000;
1022 req.KeepAlive = false;
1023 Stream rs = req.GetRequestStream ();
1025 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1027 Stream s = resp.GetResponseStream ();
1030 } catch (WebException ex) {
1031 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
1032 Assert.IsNull (ex.InnerException, "#3");
1033 Assert.IsNull (ex.Response, "#4");
1034 Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
1041 [Test] // bug #324300
1043 [Category("NotWorking")]
1045 public void AllowAutoRedirect ()
1047 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
1048 string url = "http://" + localEP.ToString () + "/original/";
1050 // allow autoredirect
1051 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1054 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1055 req.Method = "POST";
1057 req.ReadWriteTimeout = 2000;
1058 req.KeepAlive = false;
1059 Stream rs = req.GetRequestStream ();
1061 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1062 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1064 string body = sr.ReadToEnd ();
1066 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1067 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1068 localEP.ToString () + "/moved/", "#A2");
1069 Assert.AreEqual ("GET", resp.Method, "#A3");
1070 Assert.AreEqual ("LOOKS OK", body, "#A4");
1075 // do not allow autoredirect
1076 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1079 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1080 req.Method = "POST";
1081 req.AllowAutoRedirect = false;
1083 req.ReadWriteTimeout = 1000;
1084 req.KeepAlive = false;
1085 Stream rs = req.GetRequestStream ();
1087 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1088 Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
1089 Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
1090 Assert.AreEqual ("POST", resp.Method, "#B3");
1097 public void PostAndRedirect_NoCL ()
1099 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8769);
1100 string url = "http://" + localEP.ToString () + "/original/";
1102 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1105 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1106 req.Method = "POST";
1108 req.ReadWriteTimeout = 2000;
1109 Stream rs = req.GetRequestStream ();
1112 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1113 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1115 string body = sr.ReadToEnd ();
1117 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1118 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1119 localEP.ToString () + "/moved/", "#A2");
1120 Assert.AreEqual ("GET", resp.Method, "#A3");
1121 Assert.AreEqual ("LOOKS OK", body, "#A4");
1128 public void PostAndRedirect_CL ()
1130 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8770);
1131 string url = "http://" + localEP.ToString () + "/original/";
1133 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1136 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1137 req.Method = "POST";
1139 req.ReadWriteTimeout = 2000;
1140 req.ContentLength = 1;
1141 Stream rs = req.GetRequestStream ();
1143 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1144 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1146 string body = sr.ReadToEnd ();
1148 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1149 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1150 localEP.ToString () + "/moved/", "#A2");
1151 Assert.AreEqual ("GET", resp.Method, "#A3");
1152 Assert.AreEqual ("LOOKS OK", body, "#A4");
1159 public void PostAnd401 ()
1161 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8771);
1162 string url = "http://" + localEP.ToString () + "/original/";
1164 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1167 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1168 req.Method = "POST";
1170 req.ReadWriteTimeout = 2000;
1171 req.ContentLength = 1;
1172 Stream rs = req.GetRequestStream ();
1174 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1175 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1177 string body = sr.ReadToEnd ();
1179 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1180 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1181 localEP.ToString () + "/moved/", "#A2");
1182 Assert.AreEqual ("GET", resp.Method, "#A3");
1183 Assert.AreEqual ("LOOKS OK", body, "#A4");
1189 [Test] // bug #324347
1190 [Category ("NotWorking")]
1191 public void InternalServerError ()
1193 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8766);
1194 string url = "http://" + localEP.ToString () + "/original/";
1197 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
1200 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1201 req.Method = "POST";
1203 req.ReadWriteTimeout = 2000;
1204 req.KeepAlive = false;
1205 Stream rs = req.GetRequestStream ();
1210 Assert.Fail ("#A1");
1211 } catch (WebException ex) {
1212 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1213 Assert.IsNull (ex.InnerException, "#A3");
1214 Assert.IsNotNull (ex.Message, "#A4");
1215 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
1217 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1218 Assert.IsNotNull (webResponse, "#A6");
1219 Assert.AreEqual ("POST", webResponse.Method, "#A7");
1220 webResponse.Close ();
1227 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
1230 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1233 req.ReadWriteTimeout = 2000;
1234 req.KeepAlive = false;
1238 Assert.Fail ("#B1");
1239 } catch (WebException ex) {
1240 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1241 Assert.IsNull (ex.InnerException, "#B3");
1242 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1244 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1245 Assert.IsNotNull (webResponse, "#B5");
1246 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1247 webResponse.Close ();
1255 [Category ("NotWorking")] // #B3 fails; we get a SocketException: An existing connection was forcibly closed by the remote host
1256 public void NoContentLength ()
1258 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8767);
1259 string url = "http://" + localEP.ToString () + "/original/";
1262 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
1265 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1266 req.Method = "POST";
1268 req.ReadWriteTimeout = 2000;
1269 req.KeepAlive = false;
1270 Stream rs = req.GetRequestStream ();
1275 Assert.Fail ("#A1");
1276 } catch (WebException ex) {
1277 // The underlying connection was closed:
1278 // An unexpected error occurred on a
1280 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1281 Assert.IsNotNull (ex.InnerException, "#A3");
1282 Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
1283 Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
1285 // Unable to read data from the transport connection:
1286 // A connection attempt failed because the connected party
1287 // did not properly respond after a period of time, or
1288 // established connection failed because connected host has
1289 // failed to respond
1290 IOException ioe = (IOException) ex.InnerException;
1291 Assert.IsNotNull (ioe.InnerException, "#A6");
1292 Assert.IsNotNull (ioe.Message, "#A7");
1293 Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
1295 // An existing connection was forcibly
1296 // closed by the remote host
1297 SocketException soe = (SocketException) ioe.InnerException;
1298 Assert.IsNull (soe.InnerException, "#A9");
1299 Assert.IsNotNull (soe.Message, "#A10");
1301 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1302 Assert.IsNull (webResponse, "#A11");
1309 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
1312 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1315 req.ReadWriteTimeout = 2000;
1316 req.KeepAlive = false;
1320 Assert.Fail ("#B1");
1321 } catch (WebException ex) {
1322 // The remote server returned an error:
1323 // (500) Internal Server Error
1324 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1325 Assert.IsNull (ex.InnerException, "#B3");
1326 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1328 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1329 Assert.IsNotNull (webResponse, "#B5");
1330 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1331 webResponse.Close ();
1338 [Test] // bug #513087
1339 public void NonStandardVerb ()
1341 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8123);
1342 string url = "http://" + ep.ToString () + "/moved/";
1344 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (VerbEchoHandler))) {
1347 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1348 req.Method = "WhatEver";
1349 req.KeepAlive = false;
1350 req.Timeout = 20000;
1351 req.ReadWriteTimeout = 20000;
1353 Stream rs = req.GetRequestStream ();
1356 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1357 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1359 string body = sr.ReadToEnd ();
1361 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#1");
1362 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1363 ep.ToString () + "/moved/", "#2");
1364 Assert.AreEqual ("WhatEver", resp.Method, "#3");
1365 Assert.AreEqual ("WhatEver", body, "#4");
1373 [Category ("NotWorking")] // Assert #2 fails
1374 public void NotModifiedSince ()
1376 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9123);
1377 string url = "http://" + ep.ToString () + "/test/";
1379 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (NotModifiedSinceHandler))) {
1382 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1384 req.KeepAlive = false;
1385 req.Timeout = 20000;
1386 req.ReadWriteTimeout = 20000;
1387 req.Headers.Add (HttpRequestHeader.IfNoneMatch, "898bbr2347056cc2e096afc66e104653");
1388 req.IfModifiedSince = new DateTime (2010, 01, 04);
1390 DateTime start = DateTime.Now;
1391 HttpWebResponse response = null;
1396 } catch (WebException e) {
1397 response = (HttpWebResponse) e.Response;
1400 Assert.IsNotNull (response, "#2");
1401 using (Stream stream = response.GetResponseStream ()) {
1402 byte [] buffer = new byte [4096];
1403 int bytesRead = stream.Read (buffer, 0, buffer.Length);
1404 Assert.AreEqual (0, bytesRead, "#3");
1407 TimeSpan elapsed = DateTime.Now - start;
1408 Assert.IsTrue (elapsed.TotalMilliseconds < 2000, "#4");
1414 [Test] // bug #353495
1415 [Category ("NotWorking")]
1416 public void LastModifiedKind ()
1418 const string reqURL = "http://coffeefaq.com/site/node/25";
1419 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
1420 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
1421 DateTime lastMod = resp.LastModified;
1422 //string rawLastMod = resp.Headers ["Last-Modified"];
1424 //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
1425 Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
1426 req = (HttpWebRequest) WebRequest.Create (reqURL);
1427 req.IfModifiedSince = lastMod;
1429 resp = (HttpWebResponse) req.GetResponse ();
1431 Assert.Fail ("Should result in 304");
1432 } catch (WebException ex) {
1433 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
1434 Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
1439 #region Timeout_Bug // https://bugzilla.novell.com/show_bug.cgi?id=317553
1441 class TimeoutTestHelper {
1444 internal DateTime? Start { get; private set; }
1445 internal DateTime? End { get; private set; }
1446 internal Exception Exception { get; private set; }
1447 internal string Body { get; private set; }
1448 internal int TimeOutInMilliSeconds { get; private set; }
1450 internal TimeoutTestHelper (string url, int timeoutInMilliseconds)
1453 TimeOutInMilliSeconds = timeoutInMilliseconds;
1456 internal void LaunchWebRequest ()
1458 var req = (HttpWebRequest) WebRequest.Create (url_to_test);
1459 req.Timeout = TimeOutInMilliSeconds;
1461 Start = DateTime.Now;
1463 using (var resp = (HttpWebResponse) req.GetResponse ())
1465 var sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
1466 Body = sr.ReadToEnd ();
1468 } catch (Exception e) {
1475 void TestTimeOut (string url)
1477 var timeoutWorker = new TimeoutTestHelper (url, three_seconds_in_milliseconds);
1478 var threadStart = new ThreadStart (timeoutWorker.LaunchWebRequest);
1479 var thread = new Thread (threadStart);
1481 Thread.Sleep (three_seconds_in_milliseconds * 3);
1483 if (timeoutWorker.End == null) {
1485 Assert.Fail ("Thread did not finish after double the timout specified passed");
1488 if (!String.IsNullOrEmpty (timeoutWorker.Body)) {
1489 if (timeoutWorker.Body == response_of_timeout_handler) {
1490 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve proper body");
1492 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve an incorrect body: " + timeoutWorker.Body);
1495 Assert.IsNotNull (timeoutWorker.Exception,
1496 "Timeout exception was not thrown");
1498 var webEx = timeoutWorker.Exception as WebException;
1499 Assert.IsNotNull (webEx, "Exception thrown should be WebException, but was: " +
1500 timeoutWorker.Exception.GetType ().FullName);
1502 Assert.AreEqual (webEx.Status, WebExceptionStatus.Timeout,
1503 "WebException was thrown, but with a wrong status (should be timeout): " + webEx.Status);
1505 Assert.IsFalse (timeoutWorker.End > (timeoutWorker.Start + TimeSpan.FromMilliseconds (three_seconds_in_milliseconds + 500)));
1508 [Test] // 1st possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1509 public void TestTimeoutPropertyWithServerThatExistsAndRespondsButTooLate ()
1511 var ep = new IPEndPoint (IPAddress.Loopback, 8123);
1512 string url = "http://" + ep + "/foobar/";
1514 using (var responder = new SocketResponder (ep, TimeOutHandler))
1524 [Test] // 2nd possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1525 public void TestTimeoutPropertyWithServerThatDoesntExist ()
1527 string url = "http://10.128.200.100:8271/"; // some endpoint that is unlikely to exist
1532 const string response_of_timeout_handler = "RESPONSE_OF_TIMEOUT_HANDLER";
1533 const int three_seconds_in_milliseconds = 3000;
1535 private static byte[] TimeOutHandler (Socket socket)
1537 socket.Receive (new byte[4096]);
1539 Thread.Sleep (three_seconds_in_milliseconds * 2);
1541 var sw = new StringWriter ();
1542 sw.WriteLine ("HTTP/1.1 200 OK");
1543 sw.WriteLine ("Content-Type: text/plain");
1544 sw.WriteLine ("Content-Length: " + response_of_timeout_handler.Length);
1546 sw.Write (response_of_timeout_handler);
1549 return Encoding.UTF8.GetBytes (sw.ToString ());
1554 internal static byte [] EchoRequestHandler (Socket socket)
1556 MemoryStream ms = new MemoryStream ();
1557 byte [] buffer = new byte [4096];
1558 int bytesReceived = socket.Receive (buffer);
1559 while (bytesReceived > 0) {
1560 ms.Write (buffer, 0, bytesReceived);
1561 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1562 // after sending the headers
1564 if (socket.Available > 0) {
1565 bytesReceived = socket.Receive (buffer);
1572 StreamReader sr = new StreamReader (ms, Encoding.UTF8);
1573 string request = sr.ReadToEnd ();
1575 StringWriter sw = new StringWriter ();
1576 sw.WriteLine ("HTTP/1.1 200 OK");
1577 sw.WriteLine ("Content-Type: text/xml");
1578 sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
1583 return Encoding.UTF8.GetBytes (sw.ToString ());
1586 static byte [] RedirectRequestHandler (Socket socket)
1588 MemoryStream ms = new MemoryStream ();
1589 byte [] buffer = new byte [4096];
1590 int bytesReceived = socket.Receive (buffer);
1591 while (bytesReceived > 0) {
1592 ms.Write (buffer, 0, bytesReceived);
1593 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1594 // after sending the headers
1596 if (socket.Available > 0) {
1597 bytesReceived = socket.Receive (buffer);
1604 string statusLine = null;
1605 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1606 statusLine = sr.ReadLine ();
1609 StringWriter sw = new StringWriter ();
1610 if (statusLine.StartsWith ("POST /original/")) {
1611 sw.WriteLine ("HTTP/1.0 302 Found");
1612 EndPoint ep = socket.LocalEndPoint;
1613 sw.WriteLine ("Location: " + "http://" + ep.ToString () + "/moved/");
1616 } else if (statusLine.StartsWith ("GET /moved/")) {
1617 sw.WriteLine ("HTTP/1.0 200 OK");
1618 sw.WriteLine ("Content-Type: text/plain");
1619 sw.WriteLine ("Content-Length: 8");
1621 sw.Write ("LOOKS OK");
1624 sw.WriteLine ("HTTP/1.0 500 Too Lazy");
1629 return Encoding.UTF8.GetBytes (sw.ToString ());
1632 static byte [] InternalErrorHandler (Socket socket)
1634 byte [] buffer = new byte [4096];
1635 int bytesReceived = socket.Receive (buffer);
1636 while (bytesReceived > 0) {
1637 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1638 // after sending the headers
1640 if (socket.Available > 0) {
1641 bytesReceived = socket.Receive (buffer);
1646 StringWriter sw = new StringWriter ();
1647 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1648 sw.WriteLine ("Content-Length: 0");
1652 return Encoding.UTF8.GetBytes (sw.ToString ());
1655 static byte [] NoContentLengthHandler (Socket socket)
1657 StringWriter sw = new StringWriter ();
1658 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1662 return Encoding.UTF8.GetBytes (sw.ToString ());
1665 static byte [] NotModifiedSinceHandler (Socket socket)
1667 StringWriter sw = new StringWriter ();
1668 sw.WriteLine ("HTTP/1.1 304 Not Modified");
1669 sw.WriteLine ("Date: Fri, 06 Feb 2009 12:50:26 GMT");
1670 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");
1671 sw.WriteLine ("Not-Modified-Since: Sun, 08 Feb 2009 08:49:26 GMT");
1672 sw.WriteLine ("ETag: 898bbr2347056cc2e096afc66e104653");
1673 sw.WriteLine ("Connection: close");
1677 return Encoding.UTF8.GetBytes (sw.ToString ());
1680 static byte [] VerbEchoHandler (Socket socket)
1682 MemoryStream ms = new MemoryStream ();
1683 byte [] buffer = new byte [4096];
1684 int bytesReceived = socket.Receive (buffer);
1685 while (bytesReceived > 0) {
1686 ms.Write (buffer, 0, bytesReceived);
1687 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1688 // after sending the headers
1690 if (socket.Available > 0) {
1691 bytesReceived = socket.Receive (buffer);
1698 string statusLine = null;
1699 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1700 statusLine = sr.ReadLine ();
1703 string verb = "DEFAULT";
1704 if (statusLine != null) {
1705 string [] parts = statusLine.Split (' ');
1706 if (parts.Length > 0)
1710 StringWriter sw = new StringWriter ();
1711 sw.WriteLine ("HTTP/1.1 200 OK");
1712 sw.WriteLine ("Content-Type: text/plain");
1713 sw.WriteLine ("Content-Length: " + verb.Length);
1718 return Encoding.UTF8.GetBytes (sw.ToString ());
1721 static byte [] PostAnd401Handler (Socket socket)
1723 MemoryStream ms = new MemoryStream ();
1724 byte [] buffer = new byte [4096];
1725 int bytesReceived = socket.Receive (buffer);
1726 while (bytesReceived > 0) {
1727 ms.Write (buffer, 0, bytesReceived);
1728 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1729 // after sending the headers
1731 if (socket.Available > 0) {
1732 bytesReceived = socket.Receive (buffer);
1739 string statusLine = null;
1740 bool have_auth = false;
1742 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1744 while ((l = sr.ReadLine ()) != null) {
1745 if (statusLine == null) {
1747 } else if (l.StartsWith ("Authorization:")) {
1749 } else if (l.StartsWith ("Content-Length:")) {
1750 cl = Int32.Parse (l.Substring ("content-length: ".Length));
1755 StringWriter sw = new StringWriter ();
1757 sw.WriteLine ("HTTP/1.0 401 Invalid Credentials");
1758 sw.WriteLine ("WWW-Authenticate: basic Yeah");
1761 } else if (cl > 0 && statusLine.StartsWith ("POST ")) {
1762 sw.WriteLine ("HTTP/1.0 200 OK");
1763 sw.WriteLine ("Content-Type: text/plain");
1764 sw.WriteLine ("Content-Length: 8");
1766 sw.Write ("LOOKS OK");
1769 sw.WriteLine ("HTTP/1.0 500 test failed");
1770 sw.WriteLine ("Content-Length: 0");
1775 return Encoding.UTF8.GetBytes (sw.ToString ());
1778 public void NtlmAuthentication ()
1780 NtlmServer server = new NtlmServer ();
1783 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
1784 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
1785 request.Timeout = 5000;
1786 request.Credentials = new NetworkCredential ("user", "password", "domain");
1787 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
1789 using (StreamReader reader = new StreamReader (resp.GetResponseStream ())) {
1790 res = reader.ReadToEnd ();
1794 Assert.AreEqual ("OK", res);
1797 class NtlmServer : HttpServer {
1798 public string Where = "";
1799 protected override void Run ()
1801 Where = "before accept";
1802 Socket client = sock.Accept ();
1803 NetworkStream ns = new NetworkStream (client, false);
1804 StreamReader reader = new StreamReader (ns, Encoding.ASCII);
1806 Where = "first read";
1807 while ((line = reader.ReadLine ()) != null) {
1808 if (line.Trim () == String.Empty) {
1812 Where = "first write";
1813 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1814 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1815 "WWW-Authenticate: NTLM\r\n" +
1816 "Content-Length: 5\r\n\r\nWRONG");
1819 Where = "second read";
1820 while ((line = reader.ReadLine ()) != null) {
1821 if (line.Trim () == String.Empty) {
1825 Where = "second write";
1826 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1827 "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAADgAAAABggAC8GDhqIONH3sAAAAAAAAAAAAAAAA4AAAABQLODgAAAA8=\r\n" +
1828 "Content-Length: 5\r\n\r\nWRONG");
1831 Where = "third read";
1832 while ((line = reader.ReadLine ()) != null) {
1833 if (line.Trim () == String.Empty) {
1837 Where = "third write";
1838 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1839 "Keep-Alive: true\r\n" +
1840 "Content-Length: 2\r\n\r\nOK");
1842 Thread.Sleep (1000);
1849 class BadChunkedServer : HttpServer {
1850 protected override void Run ()
1852 Socket client = sock.Accept ();
1853 NetworkStream ns = new NetworkStream (client, true);
1854 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1855 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1856 "Transfer-Encoding: chunked\r\n" +
1857 "Connection: close\r\n" +
1858 "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
1860 // This body lacks a 'last-chunk' (see RFC 2616)
1861 writer.Write ("10\r\n1234567890123456\r\n");
1863 client.Shutdown (SocketShutdown.Send);
1864 Thread.Sleep (1000);
1869 class AcceptAllPolicy : ICertificatePolicy {
1870 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
1876 abstract class HttpServer
1878 protected Socket sock;
1879 protected Exception error;
1880 protected ManualResetEvent evt;
1882 public HttpServer ()
1884 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
1885 sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
1889 public void Start ()
1891 evt = new ManualResetEvent (false);
1892 Thread th = new Thread (new ThreadStart (Run));
1902 public IPAddress IPAddress {
1903 get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
1907 get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
1910 public Exception Error {
1911 get { return error; }
1914 protected abstract void Run ();
1918 public void BeginGetRequestStream ()
1924 r.ContentLength = 0;
1925 r.BeginGetRequestStream ((a) =>
1927 using (Stream s = r.EndGetRequestStream (a)) { };
1936 public void BeginGetRequestStreamNoClose ()
1941 r.ContentLength = 1;
1942 r.BeginGetRequestStream ((a) =>
1944 r.EndGetRequestStream (a);
1953 public void BeginGetRequestStreamCancelIfNotAllBytesWritten ()
1959 r.ContentLength = 10;
1960 r.BeginGetRequestStream ((a) =>
1962 WebException ex = ExceptionAssert.Throws<WebException> (() =>
1964 using (Stream s = r.EndGetRequestStream (a)) {
1968 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1977 public void GetRequestStream2 ()
1983 r.ContentLength = data64KB.Length;
1984 using (Stream s = r.GetRequestStream ()) {
1985 s.Write (data64KB, 0, data64KB.Length);
1993 public void GetRequestStreamNotAllBytesWritten ()
1999 r.ContentLength = data64KB.Length;
2000 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ().Close ());
2001 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2008 public void GetRequestStreamTimeout ()
2014 r.ContentLength = data64KB.Length;
2016 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ());
2017 Assert.IsTrue (ex.Status == WebExceptionStatus.Timeout || ex.Status == WebExceptionStatus.ConnectFailure);
2023 public void BeginWrite ()
2025 byte[] received = new byte[data64KB.Length];
2031 r.ContentLength = data64KB.Length;
2033 Stream s = r.GetRequestStream ();
2034 s.BeginWrite (data64KB, 0, data64KB.Length,
2039 r.GetResponse ().Close ();
2046 c.Request.InputStream.ReadAll (received, 0, received.Length);
2047 c.Response.StatusCode = 204;
2048 c.Response.Close ();
2051 Assert.AreEqual (data64KB, received);
2055 public void BeginWriteAfterAbort ()
2057 byte [] received = new byte [data64KB.Length];
2063 r.ContentLength = data64KB.Length;
2065 Stream s = r.GetRequestStream ();
2068 WebException ex = ExceptionAssert.Throws<WebException> (() => s.BeginWrite (data64KB, 0, data64KB.Length, null, null));
2069 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2075 //c.Request.InputStream.ReadAll (received, 0, received.Length);
2076 //c.Response.StatusCode = 204;
2077 //c.Response.Close();
2082 public void PrematureStreamCloseAborts ()
2084 byte [] received = new byte [data64KB.Length];
2090 r.ContentLength = data64KB.Length * 2;
2092 Stream s = r.GetRequestStream ();
2093 s.Write (data64KB, 0, data64KB.Length);
2095 WebException ex = ExceptionAssert.Throws<WebException>(() => s.Close());
2096 Assert.AreEqual(ex.Status, WebExceptionStatus.RequestCanceled);
2102 c.Request.InputStream.ReadAll (received, 0, received.Length);
2103 // c.Response.StatusCode = 204;
2104 // c.Response.Close ();
2109 public void Write ()
2111 byte [] received = new byte [data64KB.Length];
2117 r.ContentLength = data64KB.Length;
2119 using (Stream s = r.GetRequestStream ()) {
2120 s.Write (data64KB, 0, data64KB.Length);
2123 r.GetResponse ().Close ();
2128 c.Request.InputStream.ReadAll (received, 0, received.Length);
2129 c.Response.StatusCode = 204;
2130 c.Response.Close ();
2133 Assert.AreEqual(data64KB, received);
2137 Invalid test: it does not work on linux.
2138 [pid 30973] send(9, "POST / HTTP/1.1\r\nContent-Length:"..., 89, 0) = 89
2140 [pid 30970] send(16, "HTTP/1.1 200 OK\r\nServer: Mono-HT"..., 133, 0) = 133
2142 [pid 30970] close(16) = 0
2144 [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
2146 [pid 30966] close(4) = 0
2149 The server sideis closed (FD 16) and the send on the client side (FD 9) succeeds.
2151 [Category("NotWorking")]
2152 public void WriteServerAborts ()
2154 ManualResetEvent abort = new ManualResetEvent (false);
2155 byte [] received = new byte [data64KB.Length];
2161 r.ContentLength = data64KB.Length;
2163 using (Stream s = r.GetRequestStream()) {
2166 IOException ex = ExceptionAssert.Throws<IOException> (() => s.Write(data64KB, 0, data64KB.Length));
2182 byte [] received = new byte [data64KB.Length];
2187 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2188 using (Stream s = x.GetResponseStream()) {
2189 s.ReadAll (received, 0, received.Length);
2196 c.Response.StatusCode = 200;
2197 c.Response.ContentLength64 = data64KB.Length;
2198 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2199 c.Response.OutputStream.Close ();
2200 c.Response.Close ();
2203 Assert.AreEqual (data64KB, received);
2207 public void ReadTimeout2 ()
2209 byte [] received = new byte [data64KB.Length];
2214 r.ReadWriteTimeout = 10;
2215 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2216 using (Stream s = x.GetResponseStream ()) {
2217 WebException ex = ExceptionAssert.Throws<WebException> (() => s.ReadAll (received, 0, received.Length));
2218 Assert.AreEqual (ex.Status, WebExceptionStatus.Timeout);
2225 c.Response.StatusCode = 200;
2226 c.Response.ContentLength64 = data64KB.Length;
2227 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length / 2);
2228 Thread.Sleep (1000);
2229 // c.Response.OutputStream.Write (data64KB, data64KB.Length / 2, data64KB.Length / 2);
2230 c.Response.OutputStream.Close ();
2231 c.Response.Close ();
2236 public void ReadServerAborted ()
2238 byte [] received = new byte [data64KB.Length];
2243 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2244 using (Stream s = x.GetResponseStream ()) {
2245 Assert.AreEqual (1, s.ReadAll (received, 0, received.Length));
2252 c.Response.StatusCode = 200;
2253 c.Response.ContentLength64 = data64KB.Length;
2254 c.Response.OutputStream.Write (data64KB, 0, 1);
2255 c.Response.Abort ();
2260 public void BeginGetResponse2 ()
2262 byte [] received = new byte [data64KB.Length];
2267 r.BeginGetResponse ((a) =>
2269 using (HttpWebResponse x = (HttpWebResponse) r.EndGetResponse (a))
2270 using (Stream s = x.GetResponseStream ()) {
2271 s.ReadAll (received, 0, received.Length);
2279 c.Response.StatusCode = 200;
2280 c.Response.ContentLength64 = data64KB.Length;
2281 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2282 c.Response.OutputStream.Close ();
2283 c.Response.Close ();
2286 Assert.AreEqual (data64KB, received);
2290 public void BeginGetResponseAborts ()
2292 ManualResetEvent aborted = new ManualResetEvent(false);
2297 r.BeginGetResponse((a) =>
2299 WebException ex = ExceptionAssert.Throws<WebException> (() => r.EndGetResponse (a));
2300 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2310 // Thread.Sleep (100);
2311 // c.Response.StatusCode = 200;
2312 // c.Response.ContentLength64 = 0;
2313 // c.Response.Close ();
2319 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request)
2321 int port = rand.Next (20000, 65535);
2323 ManualResetEvent completed = new ManualResetEvent (false);
2324 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2325 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2327 request (client, completed);
2329 if (!completed.WaitOne (10000))
2330 Assert.Fail ("Test hung");
2333 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request, Action<HttpListenerContext> processor)
2335 int port = rand.Next (20000, 65535);
2337 ManualResetEvent [] completed = new ManualResetEvent [2];
2338 completed [0] = new ManualResetEvent (false);
2339 completed [1] = new ManualResetEvent (false);
2341 using (ListenerScope scope = new ListenerScope (processor, port, completed [0])) {
2342 ManualResetEvent clientCompleted = new ManualResetEvent (false);
2343 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2344 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2346 ThreadPool.QueueUserWorkItem ((o) => request (client, completed [1]));
2348 if (!WaitHandle.WaitAll (completed, 10000))
2349 Assert.Fail ("Test hung.");
2355 [ExpectedException (typeof (ArgumentNullException))]
2356 public void NullHost ()
2358 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2363 public void NoHost ()
2365 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2366 Assert.AreEqual (req.Host, "go-mono.com");
2370 [ExpectedException (typeof (ArgumentException))]
2371 public void EmptyHost ()
2373 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2378 public void HostAndPort ()
2380 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:80");
2381 Assert.AreEqual ("go-mono.com", req.Host, "#01");
2382 req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:9000");
2383 Assert.AreEqual ("go-mono.com:9000", req.Host, "#02");
2387 public void PortRange ()
2389 for (int i = 0; i < 65536; i++) {
2392 string s = i.ToString ();
2393 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:" + s);
2394 Assert.AreEqual ("go-mono.com:" + s, req.Host, "#" + s);
2399 [ExpectedException (typeof (ArgumentException))]
2400 public void PortBelow ()
2402 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2403 req.Host = "go-mono.com:-1";
2407 [ExpectedException (typeof (ArgumentException))]
2408 public void PortAbove ()
2410 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2411 req.Host = "go-mono.com:65536";
2415 [ExpectedException (typeof (ArgumentException))]
2416 public void HostTooLong ()
2418 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2419 string s = new string ('a', 100);
2420 req.Host = s + "." + s + "." + s + "." + s + "." + s + "." + s; // Over 255 bytes
2424 [Category ("NotWorking")] // #5490
2425 public void InvalidNamesThatWork ()
2427 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2431 req.Host = new string ('a', 64); // Should fail. Max. is 63.
2435 public void NoDate ()
2437 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2438 Assert.AreEqual (DateTime.MinValue, req.Date);
2442 public void UtcDate ()
2444 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2445 req.Date = DateTime.UtcNow;
2446 DateTime date = req.Date;
2447 Assert.AreEqual (DateTimeKind.Local, date.Kind);
2451 public void AddAndRemoveDate ()
2453 // Neil Armstrong set his foot on Moon
2454 var landing = new DateTime (1969, 7, 21, 2, 56, 0, DateTimeKind.Utc);
2455 Assert.AreEqual (621214377600000000, landing.Ticks);
2456 var unspecified = new DateTime (1969, 7, 21, 2, 56, 0);
2457 var local = landing.ToLocalTime ();
2459 var req = (HttpWebRequest)WebRequest.Create ("http://www.mono-project.com/");
2461 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2462 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2463 Assert.AreEqual (local, req.Date);
2465 req.Date = unspecified;
2466 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2467 Assert.AreEqual (unspecified.Ticks, req.Date.Ticks);
2468 Assert.AreEqual (unspecified, req.Date);
2471 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2472 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2473 Assert.AreEqual (local, req.Date);
2475 req.Date = DateTime.MinValue;
2476 Assert.AreEqual (DateTimeKind.Unspecified, DateTime.MinValue.Kind);
2477 Assert.AreEqual (DateTimeKind.Unspecified, req.Date.Kind);
2478 Assert.AreEqual (0, req.Date.Ticks);
2480 Assert.AreEqual (null, req.Headers.Get ("Date"));
2483 class ListenerScope : IDisposable {
2484 EventWaitHandle completed;
2485 public HttpListener listener;
2486 Action<HttpListenerContext> processor;
2488 public ListenerScope (Action<HttpListenerContext> processor, int port, EventWaitHandle completed)
2490 this.processor = processor;
2491 this.completed = completed;
2493 this.listener = new HttpListener ();
2494 this.listener.Prefixes.Add (string.Format ("http://localhost:{0}/", port));
2495 this.listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
2496 this.listener.Start ();
2498 this.listener.BeginGetContext (this.RequestHandler, null);
2501 void RequestHandler (IAsyncResult result)
2503 HttpListenerContext context = null;
2506 context = this.listener.EndGetContext (result);
2507 } catch (HttpListenerException ex) {
2508 // check if the thread has been aborted as in the case when we are shutting down.
2509 if (ex.ErrorCode == 995)
2511 } catch (ObjectDisposedException) {
2515 ThreadPool.QueueUserWorkItem ((o) =>
2518 this.processor (context);
2519 } catch (HttpListenerException) {
2523 this.completed.Set ();
2526 public void Dispose ()
2528 this.listener.Stop ();
2532 #if !TARGET_JVM && !MOBILE
2533 class SslHttpServer : HttpServer {
2534 X509Certificate _certificate;
2536 protected override void Run ()
2539 Socket client = sock.Accept ();
2540 NetworkStream ns = new NetworkStream (client, true);
2541 SslServerStream s = new SslServerStream (ns, Certificate, false, false);
2542 s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
2544 StreamReader reader = new StreamReader (s);
2545 StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
2548 string hello = "<html><body><h1>Hello World!</h1></body></html>";
2549 string answer = "HTTP/1.0 200\r\n" +
2550 "Connection: close\r\n" +
2551 "Content-Type: text/html\r\n" +
2552 "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
2553 "Content-Length: " + hello.Length + "\r\n" +
2558 line = reader.ReadLine ();
2559 } while (line != "" && line != null && line.Length > 0);
2561 // Now the content. We know it's 100 bytes.
2562 // This makes BeginRead in sslclientstream block.
2563 char [] cs = new char [100];
2564 reader.Read (cs, 0, 100);
2566 writer.Write (answer);
2568 if (evt.WaitOne (5000, false))
2569 error = new Exception ("Timeout when stopping the server");
2570 } catch (Exception e) {
2575 X509Certificate Certificate {
2577 if (_certificate == null)
2578 _certificate = new X509Certificate (CertData.Certificate);
2580 return _certificate;
2584 AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
2586 PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
2592 public readonly static byte [] Certificate = {
2593 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
2594 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
2595 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
2596 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
2597 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
2598 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
2599 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
2600 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
2601 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
2602 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
2603 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
2604 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
2605 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
2606 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
2607 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
2608 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
2609 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
2610 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
2611 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
2612 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
2613 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
2614 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
2615 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
2616 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
2617 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
2618 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
2619 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
2620 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
2624 public readonly static byte [] PrivateKey = {
2625 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
2626 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
2627 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
2628 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
2629 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
2630 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
2631 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
2632 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
2633 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
2634 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
2635 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
2636 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
2637 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
2638 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
2639 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
2640 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
2641 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
2642 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
2643 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
2644 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
2645 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
2646 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
2647 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
2648 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
2649 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
2650 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
2651 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
2652 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
2653 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
2654 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
2655 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
2656 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
2657 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
2658 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
2659 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
2660 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
2661 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
2662 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
2663 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
2669 public void CookieContainerTest ()
2671 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 11002);
2672 string url = "http://" + ep.ToString ();
2674 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (CookieRequestHandler))) {
2677 CookieContainer container = new CookieContainer ();
2678 container.Add(new Uri (url), new Cookie ("foo", "bar"));
2679 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2680 request.CookieContainer = container;
2681 WebHeaderCollection headers = request.Headers;
2682 headers.Add("Cookie", "foo=baz");
2683 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2684 string responseString = null;
2685 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2686 responseString = reader.ReadToEnd ();
2689 Assert.AreEqual (1, response.Cookies.Count, "#01");
2690 Assert.AreEqual ("foo=bar", response.Headers.Get("Set-Cookie"), "#02");
2693 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (CookieRequestHandler))) {
2696 CookieContainer container = new CookieContainer ();
2697 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2698 request.CookieContainer = container;
2699 WebHeaderCollection headers = request.Headers;
2700 headers.Add("Cookie", "foo=baz");
2701 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2702 string responseString = null;
2703 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2704 responseString = reader.ReadToEnd ();
2707 Assert.AreEqual (0, response.Cookies.Count, "#03");
2708 Assert.AreEqual ("", response.Headers.Get("Set-Cookie"), "#04");
2712 internal static byte[] CookieRequestHandler (Socket socket)
2714 MemoryStream ms = new MemoryStream ();
2715 byte[] buffer = new byte[4096];
2716 int bytesReceived = socket.Receive (buffer);
2717 while (bytesReceived > 0) {
2718 ms.Write(buffer, 0, bytesReceived);
2719 // We don't check for Content-Length or anything else here, so we give the client a little time to write
2720 // after sending the headers
2722 if (socket.Available > 0) {
2723 bytesReceived = socket.Receive (buffer);
2730 string cookies = string.Empty;
2731 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
2733 while ((line = sr.ReadLine ()) != null) {
2734 if (line.StartsWith ("Cookie:")) {
2735 cookies = line.Substring ("cookie: ".Length);
2740 StringWriter sw = new StringWriter ();
2741 sw.WriteLine ("HTTP/1.1 200 OK");
2742 sw.WriteLine ("Content-Type: text/xml");
2743 sw.WriteLine ("Set-Cookie: " + cookies);
2744 sw.WriteLine ("Content-Length: " + cookies.Length.ToString (CultureInfo.InvariantCulture));
2749 return Encoding.UTF8.GetBytes (sw.ToString ());
2754 public class HttpRequestStreamTest
2757 public void BeginRead ()
2759 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9124);
2760 string url = "http://" + ep.ToString () + "/test/";
2762 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2765 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2766 req.Method = "POST";
2768 using (Stream rs = req.GetRequestStream ()) {
2769 byte [] buffer = new byte [10];
2771 rs.BeginRead (buffer, 0, buffer.Length, null, null);
2773 } catch (NotSupportedException ex) {
2774 // The stream does not support reading
2775 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2776 Assert.IsNull (ex.InnerException, "#3");
2777 Assert.IsNotNull (ex.Message, "#4");
2786 public void BeginWrite_Request_Aborted ()
2788 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9125);
2789 string url = "http://" + ep.ToString () + "/test/";
2791 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2794 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2795 req.Method = "POST";
2797 using (Stream rs = req.GetRequestStream ()) {
2800 rs.BeginWrite (new byte [] { 0x2a, 0x2f }, 0, 2, null, null);
2802 } catch (WebException ex) {
2803 // The request was aborted: The request was canceled
2804 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
2805 Assert.IsNull (ex.InnerException, "#3");
2806 Assert.IsNotNull (ex.Message, "#4");
2807 Assert.IsNull (ex.Response, "#5");
2808 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
2815 public void CanRead ()
2817 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9126);
2818 string url = "http://" + ep.ToString () + "/test/";
2820 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2823 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2824 req.Method = "POST";
2826 Stream rs = req.GetRequestStream ();
2828 Assert.IsFalse (rs.CanRead, "#1");
2830 Assert.IsFalse (rs.CanRead, "#2");
2839 public void CanSeek ()
2841 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9127);
2842 string url = "http://" + ep.ToString () + "/test/";
2844 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2847 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2848 req.Method = "POST";
2850 Stream rs = req.GetRequestStream ();
2852 Assert.IsFalse (rs.CanSeek, "#1");
2854 Assert.IsFalse (rs.CanSeek, "#2");
2862 [Test] // bug #324182
2864 [Category ("NotWorking")]
2866 public void CanTimeout ()
2868 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9128);
2869 string url = "http://" + ep.ToString () + "/test/";
2871 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2874 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2875 req.Method = "POST";
2877 Stream rs = req.GetRequestStream ();
2879 Assert.IsTrue (rs.CanTimeout, "#1");
2881 Assert.IsTrue (rs.CanTimeout, "#2");
2890 public void CanWrite ()
2892 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9129);
2893 string url = "http://" + ep.ToString () + "/test/";
2895 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2898 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2899 req.Method = "POST";
2901 Stream rs = req.GetRequestStream ();
2903 Assert.IsTrue (rs.CanWrite, "#1");
2905 Assert.IsFalse (rs.CanWrite, "#2");
2916 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9130);
2917 string url = "http://" + ep.ToString () + "/test/";
2919 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2922 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2923 req.Method = "POST";
2925 using (Stream rs = req.GetRequestStream ()) {
2926 byte [] buffer = new byte [10];
2928 rs.Read (buffer, 0, buffer.Length);
2930 } catch (NotSupportedException ex) {
2931 // The stream does not support reading
2932 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2933 Assert.IsNull (ex.InnerException, "#3");
2934 Assert.IsNotNull (ex.Message, "#4");
2943 public void ReadByte ()
2945 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9140);
2946 string url = "http://" + ep.ToString () + "/test/";
2948 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2951 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2952 req.Method = "POST";
2954 using (Stream rs = req.GetRequestStream ()) {
2958 } catch (NotSupportedException ex) {
2959 // The stream does not support reading
2960 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2961 Assert.IsNull (ex.InnerException, "#3");
2962 Assert.IsNotNull (ex.Message, "#4");
2971 public void ReadTimeout ()
2973 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9141);
2974 string url = "http://" + ep.ToString () + "/test/";
2976 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2979 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2980 req.Method = "POST";
2982 Stream rs = req.GetRequestStream ();
2984 Assert.AreEqual (300000, rs.ReadTimeout, "#1");
2986 Assert.AreEqual (300000, rs.ReadTimeout, "#2");
2997 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9142);
2998 string url = "http://" + ep.ToString () + "/test/";
3000 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3003 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3004 req.Method = "POST";
3006 using (Stream rs = req.GetRequestStream ()) {
3008 rs.Seek (0, SeekOrigin.Current);
3010 } catch (NotSupportedException ex) {
3011 // This stream does not support seek operations
3012 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
3013 Assert.IsNull (ex.InnerException, "#3");
3014 Assert.IsNotNull (ex.Message, "#4");
3023 public void Write_Buffer_Null ()
3025 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9143);
3026 string url = "http://" + ep.ToString () + "/test/";
3028 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3031 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3032 req.Method = "POST";
3034 using (Stream rs = req.GetRequestStream ()) {
3036 rs.Write ((byte []) null, -1, -1);
3038 } catch (ArgumentNullException ex) {
3039 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3040 Assert.IsNull (ex.InnerException, "#3");
3041 Assert.IsNotNull (ex.Message, "#4");
3042 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3051 public void Write_Count_Negative ()
3053 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9144);
3054 string url = "http://" + ep.ToString () + "/test/";
3056 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3059 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3060 req.Method = "POST";
3062 using (Stream rs = req.GetRequestStream ()) {
3063 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3065 rs.Write (buffer, 1, -1);
3067 } catch (ArgumentOutOfRangeException ex) {
3068 // Specified argument was out of the range of valid values
3069 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3070 Assert.IsNull (ex.InnerException, "#A3");
3071 Assert.IsNotNull (ex.Message, "#A4");
3072 Assert.AreEqual ("size", ex.ParamName, "#A5");
3081 public void Write_Count_Overflow ()
3083 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9145);
3084 string url = "http://" + ep.ToString () + "/test/";
3086 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3089 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3090 req.Method = "POST";
3092 using (Stream rs = req.GetRequestStream ()) {
3093 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3095 rs.Write (buffer, buffer.Length - 2, 3);
3097 } catch (ArgumentOutOfRangeException ex) {
3098 // Specified argument was out of the range of valid values
3099 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3100 Assert.IsNull (ex.InnerException, "#3");
3101 Assert.IsNotNull (ex.Message, "#4");
3102 Assert.AreEqual ("size", ex.ParamName, "#5");
3111 public void Write_Offset_Negative ()
3113 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9146);
3114 string url = "http://" + ep.ToString () + "/test/";
3116 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3119 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3120 req.Method = "POST";
3122 using (Stream rs = req.GetRequestStream ()) {
3123 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3125 rs.Write (buffer, -1, 0);
3127 } catch (ArgumentOutOfRangeException ex) {
3128 // Specified argument was out of the range of valid values
3129 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3130 Assert.IsNull (ex.InnerException, "#3");
3131 Assert.IsNotNull (ex.Message, "#4");
3132 Assert.AreEqual ("offset", ex.ParamName, "#5");
3141 public void Write_Offset_Overflow ()
3143 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9147);
3144 string url = "http://" + ep.ToString () + "/test/";
3146 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3149 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3150 req.Method = "POST";
3152 using (Stream rs = req.GetRequestStream ()) {
3153 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3155 rs.Write (buffer, buffer.Length + 1, 0);
3157 } catch (ArgumentOutOfRangeException ex) {
3158 // Specified argument was out of the range of valid values
3159 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3160 Assert.IsNull (ex.InnerException, "#3");
3161 Assert.IsNotNull (ex.Message, "#4");
3162 Assert.AreEqual ("offset", ex.ParamName, "#5");
3171 public void Write_Request_Aborted ()
3173 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9148);
3174 string url = "http://" + ep.ToString () + "/test/";
3176 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3179 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3180 req.Method = "POST";
3182 using (Stream rs = req.GetRequestStream ()) {
3185 rs.Write (new byte [0], 0, 0);
3187 } catch (WebException ex) {
3188 // The request was aborted: The request was canceled
3189 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3190 Assert.IsNull (ex.InnerException, "#3");
3191 Assert.IsNotNull (ex.Message, "#4");
3192 Assert.IsNull (ex.Response, "#5");
3193 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
3200 [Category ("NotWorking")]
3201 public void Write_Stream_Closed ()
3203 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9149);
3204 string url = "http://" + ep.ToString () + "/test/";
3206 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3209 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3210 req.Method = "POST";
3212 using (Stream rs = req.GetRequestStream ()) {
3215 rs.Write (new byte [0], 0, 0);
3217 } catch (WebException ex) {
3218 // The request was aborted: The connection was closed unexpectedly
3219 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3220 Assert.IsNull (ex.InnerException, "#3");
3221 Assert.IsNotNull (ex.Message, "#4");
3222 Assert.IsNull (ex.Response, "#5");
3223 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#6");
3230 public void WriteByte_Request_Aborted ()
3232 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9150);
3233 string url = "http://" + ep.ToString () + "/test/";
3235 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3238 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3239 req.Method = "POST";
3241 using (Stream rs = req.GetRequestStream ()) {
3244 rs.WriteByte (0x2a);
3246 } catch (WebException ex) {
3247 // The request was aborted: The request was canceled
3248 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3249 Assert.IsNull (ex.InnerException, "#3");
3250 Assert.IsNotNull (ex.Message, "#4");
3251 Assert.IsNull (ex.Response, "#5");
3252 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
3259 public void WriteTimeout ()
3261 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 9151);
3262 string url = "http://" + ep.ToString () + "/test/";
3264 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3267 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3268 req.Method = "POST";
3270 Stream rs = req.GetRequestStream ();
3272 Assert.AreEqual (300000, rs.WriteTimeout, "#1");
3274 Assert.AreEqual (300000, rs.WriteTimeout, "#2");
3285 // This test is supposed to fail prior to .NET 4.0
3286 public void Post_EmptyRequestStream ()
3288 var wr = HttpWebRequest.Create ("http://google.com");
3290 wr.GetRequestStream ();
3292 var gr = wr.BeginGetResponse (delegate { }, null);
3293 Assert.AreEqual (true, gr.AsyncWaitHandle.WaitOne (5000), "#1");
3298 static class StreamExtensions {
3299 public static int ReadAll(this Stream stream, byte[] buffer, int offset, int count)
3303 while (totalRead < count) {
3304 int bytesRead = stream.Read (buffer, offset + totalRead, count - totalRead);
3308 totalRead += bytesRead;
3315 static class ExceptionAssert {
3317 /// Asserts that the function throws an exception.
3319 /// <param name="f">A function execute that is expected to raise an exception.</param>
3320 /// <typeparam name="T">The type of exception that is expected.</typeparam>
3321 /// <returns>The exception thrown.</returns>
3322 /// <exception cref="AssertFailedException">If the function does not throw an exception
3323 /// or throws a different exception.</exception>
3324 /// <example><![CDATA[
3325 /// ExceptionAssert.Throws(typeof(ArgumentNullException), delegate {
3326 /// myObject.myFunction(null); });
3328 public static T Throws<T> (Action f) where T : Exception {
3329 Exception actualException = null;
3333 } catch (Exception ex) {
3334 actualException = ex;
3337 if (actualException == null)
3338 throw new AssertionException (string.Format (
3339 "No exception thrown. Expected '{0}'",
3340 typeof (T).FullName));
3341 else if (typeof(T) != actualException.GetType())
3342 throw new AssertionException (string.Format (
3343 "Caught exception of type '{0}'. Expected '{1}':{2}",
3344 actualException.GetType().FullName,
3345 typeof (T).FullName,
3346 Environment.NewLine + actualException));
3348 return (T) actualException;