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 using MonoTests.Helpers;
34 namespace MonoTests.System.Net
37 public class HttpWebRequestTest
39 private Random rand = new Random ();
40 private byte [] data64KB = new byte [64 * 1024];
45 ServicePointManager.Expect100Continue = false;
46 rand.NextBytes (data64KB);
50 public void Proxy_Null ()
52 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
53 Assert.IsNotNull (req.Proxy, "#1");
55 Assert.IsNull (req.Proxy, "#2");
59 [Category("InetAccess")]
62 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
63 Assert.IsNotNull (req.IfModifiedSince, "req:If Modified Since: ");
65 req.UserAgent = "MonoClient v1.0";
66 Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
67 Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
69 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
70 Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
71 Assert.AreEqual ("OK", res.StatusDescription, "#B2");
73 Assert.AreEqual ("text/html; charset=ISO-8859-1", res.Headers.Get ("Content-Type"), "#C1");
74 Assert.IsNotNull (res.LastModified, "#C2");
75 Assert.AreEqual (0, res.Cookies.Count, "#C3");
81 public void AddRange ()
83 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
85 req.AddRange (50, 90);
86 req.AddRange ("bytes", 100);
87 req.AddRange ("bytes", 100, 120);
88 Assert.AreEqual ("bytes=10-,50-90,100-,100-120", req.Headers ["Range"], "#1");
90 req.AddRange ("bits", 2000);
92 } catch (InvalidOperationException) {}
96 public void CloseRequestStreamAfterReadingResponse ()
98 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
99 string url = "http://" + ep.ToString () + "/test/";
101 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
104 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
107 req.ReadWriteTimeout = 2000;
109 byte [] data = new byte [128];
110 req.ContentLength = data.Length;
112 Stream rs = req.GetRequestStream ();
113 rs.Write (data, 0, data.Length);
116 HttpWebResponse response = (HttpWebResponse) req.GetResponse ();
126 [Category("InetAccess")]
127 public void Cookies1 ()
129 // The purpose of this test is to ensure that the cookies we get from a request
130 // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer
131 // in HttpWebRequest.
132 // If this URL stops sending *one* and only one cookie, replace it.
133 string url = "http://www.elmundo.es";
134 CookieContainer cookies = new CookieContainer ();
135 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
136 req.KeepAlive = false;
137 req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2";
138 req.CookieContainer = cookies;
139 Assert.AreEqual (0, cookies.Count, "#01");
140 using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) {
141 CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url));
142 Assert.AreEqual (1, coll.Count, "#02");
143 Assert.AreEqual (1, res.Cookies.Count, "#03");
144 Cookie one = coll [0];
145 Cookie two = res.Cookies [0];
146 Assert.AreEqual (true, object.ReferenceEquals (one, two), "#04");
152 [Ignore ("Fails on MS.NET")]
153 public void SslClientBlock ()
155 // This tests that the write request/initread/write body sequence does not hang
157 // If there's a regression for this, the test will hang.
158 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
160 SslHttpServer server = new SslHttpServer ();
163 string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port);
164 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
165 request.Method = "POST";
166 Stream stream = request.GetRequestStream ();
167 byte [] bytes = new byte [100];
168 stream.Write (bytes, 0, bytes.Length);
170 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
171 Assert.AreEqual (200, (int) resp.StatusCode, "StatusCode");
172 StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
177 if (server.Error != null)
180 ServicePointManager.CertificatePolicy = null;
185 public void Missing_ContentEncoding ()
187 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
189 BadChunkedServer server = new BadChunkedServer ();
192 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
193 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
194 request.Method = "GET";
195 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
196 Assert.AreEqual ("", resp.ContentEncoding);
199 if (server.Error != null)
202 ServicePointManager.CertificatePolicy = null;
207 public void BadServer_ChunkedClose ()
209 // The server will send a chunked response without a 'last-chunked' mark
210 // and then shutdown the socket for sending.
211 BadChunkedServer server = new BadChunkedServer ();
213 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
214 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
215 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
218 byte [] bytes = new byte [32];
219 // Using StreamReader+UTF8Encoding here fails on MS runtime
220 Stream stream = resp.GetResponseStream ();
221 int nread = stream.Read (bytes, 0, 32);
222 Assert.AreEqual (16, nread, "#01");
223 x = Encoding.ASCII.GetString (bytes, 0, 16);
229 if (server.Error != null)
232 Assert.AreEqual ("1234567890123456", x);
236 [Ignore ("This test asserts that our code violates RFC 2616")]
237 public void MethodCase ()
239 ListDictionary methods = new ListDictionary ();
240 methods.Add ("post", "POST");
241 methods.Add ("puT", "PUT");
242 methods.Add ("POST", "POST");
243 methods.Add ("whatever", "whatever");
244 methods.Add ("PUT", "PUT");
246 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
247 string url = "http://" + ep.ToString () + "/test/";
249 foreach (DictionaryEntry de in methods) {
250 SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler));
253 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
254 req.Method = (string) de.Key;
256 req.ReadWriteTimeout = 2000;
257 req.KeepAlive = false;
258 Stream rs = req.GetRequestStream ();
260 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
261 StreamReader sr = new StreamReader (resp.GetResponseStream (),
263 string line = sr.ReadLine ();
265 Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
274 public void BeginGetRequestStream_Body_NotAllowed ()
276 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
277 string url = "http://" + ep.ToString () + "/test/";
279 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
282 HttpWebRequest request;
284 request = (HttpWebRequest) WebRequest.Create (url);
285 request.Method = "GET";
288 request.BeginGetRequestStream (null, null);
290 } catch (ProtocolViolationException ex) {
291 // Cannot send a content-body with this
293 Assert.IsNull (ex.InnerException, "#A2");
294 Assert.IsNotNull (ex.Message, "#A3");
297 request = (HttpWebRequest) WebRequest.Create (url);
298 request.Method = "HEAD";
301 request.BeginGetRequestStream (null, null);
303 } catch (ProtocolViolationException ex) {
304 // Cannot send a content-body with this
306 Assert.IsNull (ex.InnerException, "#B2");
307 Assert.IsNotNull (ex.Message, "#B3");
312 [Test] // bug #465613
313 public void BeginGetRequestStream_NoBuffering ()
315 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
316 string url = "http://" + ep.ToString () + "/test/";
318 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
325 req = (HttpWebRequest) WebRequest.Create (url);
327 req.SendChunked = false;
328 req.KeepAlive = false;
329 req.AllowWriteStreamBuffering = false;
331 ar = req.BeginGetRequestStream (null, null);
332 rs = req.EndGetRequestStream (ar);
335 req = (HttpWebRequest) WebRequest.Create (url);
337 req.SendChunked = false;
338 req.KeepAlive = true;
339 req.AllowWriteStreamBuffering = false;
342 req.BeginGetRequestStream (null, null);
344 } catch (ProtocolViolationException ex) {
345 // When performing a write operation with
346 // AllowWriteStreamBuffering set to false,
347 // you must either set ContentLength to a
348 // non-negative number or set SendChunked
350 Assert.IsNull (ex.InnerException, "#A2");
351 Assert.IsNotNull (ex.Message, "#A3");
354 req = (HttpWebRequest) WebRequest.Create (url);
356 req.SendChunked = false;
357 req.KeepAlive = true;
358 req.AllowWriteStreamBuffering = false;
359 req.ContentLength = 0;
361 ar = req.BeginGetRequestStream (null, null);
362 rs = req.EndGetRequestStream (ar);
367 [Test] // bug #508027
368 [Category ("NotWorking")] // #5842
369 public void BeginGetResponse ()
371 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
372 string url = "http://" + ep.ToString () + "/test/";
374 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
379 req = (HttpWebRequest) WebRequest.Create (url);
382 req.SendChunked = false;
383 req.KeepAlive = false;
384 req.AllowWriteStreamBuffering = false;
385 req.BeginGetResponse (null, null);
388 req = (HttpWebRequest) WebRequest.Create (url);
391 req.SendChunked = true;
392 req.KeepAlive = false;
393 req.AllowWriteStreamBuffering = false;
394 req.GetRequestStream ().WriteByte (1);
395 req.BeginGetResponse (null, null);
398 req = (HttpWebRequest) WebRequest.Create (url);
401 req.ContentLength = 5;
402 req.SendChunked = false;
403 req.KeepAlive = false;
404 req.AllowWriteStreamBuffering = false;
405 req.GetRequestStream ().WriteByte (5);
406 req.BeginGetResponse (null, null);
409 req = (HttpWebRequest) WebRequest.Create (url);
412 req.SendChunked = false;
413 req.KeepAlive = true;
414 req.AllowWriteStreamBuffering = false;
416 req.BeginGetResponse (null, null);
419 req = (HttpWebRequest) WebRequest.Create (url);
422 req.SendChunked = false;
423 req.KeepAlive = false;
424 req.AllowWriteStreamBuffering = false;
425 req.ContentLength = 5;
426 req.BeginGetResponse (null, null);
429 req = (HttpWebRequest) WebRequest.Create (url);
432 req.SendChunked = false;
433 req.KeepAlive = true;
434 req.AllowWriteStreamBuffering = false;
435 req.ContentLength = 5;
436 req.BeginGetResponse (null, null);
439 req = (HttpWebRequest) WebRequest.Create (url);
442 req.SendChunked = true;
444 req.BeginGetResponse (null, null);
447 req = (HttpWebRequest) WebRequest.Create (url);
450 req.ContentLength = 5;
452 req.BeginGetResponse (null, null);
455 req = (HttpWebRequest) WebRequest.Create (url);
458 req.ContentLength = 0;
460 req.BeginGetResponse (null, null);
465 [Test] // bug #511851
466 public void BeginGetRequestStream_Request_Aborted ()
468 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
469 string url = "http://" + ep.ToString () + "/test/";
471 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
474 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
479 req.BeginGetRequestStream (null, null);
481 } catch (WebException ex) {
482 // The request was aborted: The request was canceled
483 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
484 Assert.IsNull (ex.InnerException, "#3");
485 Assert.IsNotNull (ex.Message, "#4");
486 Assert.IsNull (ex.Response, "#5");
487 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
492 [Test] // bug #511851
493 public void BeginGetResponse_Request_Aborted ()
495 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
496 string url = "http://" + ep.ToString () + "/test/";
498 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
501 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
506 req.BeginGetResponse (null, null);
508 } catch (WebException ex) {
509 // The request was aborted: The request was canceled
510 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
511 Assert.IsNull (ex.InnerException, "#3");
512 Assert.IsNotNull (ex.Message, "#4");
513 Assert.IsNull (ex.Response, "#5");
514 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
520 public void EndGetRequestStream_AsyncResult_Null ()
522 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
523 string url = "http://" + ep.ToString () + "/test/";
525 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
528 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
530 req.BeginGetRequestStream (null, null);
533 req.EndGetRequestStream (null);
535 } catch (ArgumentNullException ex) {
536 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
537 Assert.IsNull (ex.InnerException, "#3");
538 Assert.IsNotNull (ex.Message, "#4");
539 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
547 [Category ("NotWorking")] // do not get consistent result on MS
548 public void EndGetRequestStream_Request_Aborted ()
550 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
551 string url = "http://" + ep.ToString () + "/test/";
553 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
556 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
558 IAsyncResult ar = req.BeginGetRequestStream (null, null);
563 req.EndGetRequestStream (ar);
565 } catch (WebException ex) {
566 // The request was aborted: The request was canceled
567 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
568 Assert.IsNull (ex.InnerException, "#3");
569 Assert.IsNotNull (ex.Message, "#4");
570 Assert.IsNull (ex.Response, "#5");
571 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
576 [Test] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=471522
577 [Category ("NotWorking")]
578 public void EndGetResponse_AsyncResult_Invalid ()
580 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
581 string url = "http://" + ep.ToString () + "/test/";
583 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
586 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
589 req.ReadWriteTimeout = 2000;
590 IAsyncResult ar = req.BeginGetRequestStream (null, null);
592 // AsyncResult was not returned from call to BeginGetResponse
594 req.EndGetResponse (ar);
596 } catch (InvalidCastException) {
604 public void EndGetResponse_AsyncResult_Null ()
606 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
607 string url = "http://" + ep.ToString () + "/test/";
609 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
612 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
614 req.ReadWriteTimeout = 2000;
616 IAsyncResult ar = req.BeginGetResponse (null, null);
619 req.EndGetResponse (null);
621 } catch (ArgumentNullException ex) {
622 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
623 Assert.IsNull (ex.InnerException, "#3");
624 Assert.IsNotNull (ex.Message, "#4");
625 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
629 using (HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse (ar)) {
636 [Test] // bug #429200
637 public void GetRequestStream ()
639 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
640 string url = "http://" + ep.ToString () + "/test/";
642 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
645 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
648 req.ReadWriteTimeout = 2000;
650 Stream rs1 = req.GetRequestStream ();
651 Stream rs2 = req.GetRequestStream ();
653 Assert.IsNotNull (rs1, "#1");
654 Assert.AreSame (rs1, rs2, "#2");
660 [Test] // bug #511851
661 public void GetRequestStream_Request_Aborted ()
663 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
664 string url = "http://" + ep.ToString () + "/test/";
666 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
669 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
674 req.GetRequestStream ();
676 } catch (WebException ex) {
677 // The request was aborted: The request was canceled
678 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
679 Assert.IsNull (ex.InnerException, "#3");
680 Assert.IsNotNull (ex.Message, "#4");
681 Assert.IsNull (ex.Response, "#5");
682 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
687 [Test] // bug #510661
688 [Category ("NotWorking")] // #5842
689 public void GetRequestStream_Close_NotAllBytesWritten ()
691 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
692 string url = "http://" + ep.ToString () + "/test/";
694 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
700 req = (HttpWebRequest) WebRequest.Create (url);
702 req.ContentLength = 2;
703 rs = req.GetRequestStream ();
707 } catch (WebException ex) {
708 // The request was aborted: The request was canceled
709 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
710 Assert.IsNotNull (ex.Message, "#A3");
711 Assert.IsNull (ex.Response, "#A4");
712 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#A5");
714 // Cannot close stream until all bytes are written
715 Exception inner = ex.InnerException;
716 Assert.IsNotNull (inner, "#A6");
717 Assert.AreEqual (typeof (IOException), inner.GetType (), "#A7");
718 Assert.IsNull (inner.InnerException, "#A8");
719 Assert.IsNotNull (inner.Message, "#A9");
722 req = (HttpWebRequest) WebRequest.Create (url);
724 req.ContentLength = 2;
725 rs = req.GetRequestStream ();
730 } catch (WebException ex) {
731 // The request was aborted: The request was canceled
732 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
733 Assert.IsNotNull (ex.Message, "#B3");
734 Assert.IsNull (ex.Response, "#B4");
735 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#B5");
737 // Cannot close stream until all bytes are written
738 Exception inner = ex.InnerException;
739 Assert.IsNotNull (inner, "#B6");
740 Assert.AreEqual (typeof (IOException), inner.GetType (), "#B7");
741 Assert.IsNull (inner.InnerException, "#B8");
742 Assert.IsNotNull (inner.Message, "#B9");
745 req = (HttpWebRequest) WebRequest.Create (url);
747 req.ContentLength = 2;
748 rs = req.GetRequestStream ();
755 [Test] // bug #510642
756 [Category ("NotWorking")] // #5842
757 public void GetRequestStream_Write_Overflow ()
759 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
760 string url = "http://" + ep.ToString () + "/test/";
762 // buffered, non-chunked
763 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
770 req = (HttpWebRequest) WebRequest.Create (url);
773 req.ReadWriteTimeout = 2000;
774 req.ContentLength = 2;
776 rs = req.GetRequestStream ();
779 buffer = new byte [] { 0x2a, 0x1d };
781 rs.Write (buffer, 0, buffer.Length);
783 } catch (ProtocolViolationException ex) {
784 // Bytes to be written to the stream exceed
785 // Content-Length bytes size specified
786 Assert.IsNull (ex.InnerException, "#A2");
787 Assert.IsNotNull (ex.Message, "#A3");
792 req = (HttpWebRequest) WebRequest.Create (url);
795 req.ReadWriteTimeout = 2000;
796 req.ContentLength = 2;
798 rs = req.GetRequestStream ();
800 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
802 rs.Write (buffer, 0, buffer.Length);
804 } catch (ProtocolViolationException ex) {
805 // Bytes to be written to the stream exceed
806 // Content-Length bytes size specified
807 Assert.IsNull (ex.InnerException, "#B2");
808 Assert.IsNotNull (ex.Message, "#B3");
815 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
823 req = (HttpWebRequest) WebRequest.Create (url);
825 req.SendChunked = true;
827 req.ReadWriteTimeout = 2000;
828 req.ContentLength = 2;
830 rs = req.GetRequestStream ();
833 buffer = new byte [] { 0x2a, 0x1d };
834 rs.Write (buffer, 0, buffer.Length);
838 req = (HttpWebRequest) WebRequest.Create (url);
840 req.SendChunked = true;
842 req.ReadWriteTimeout = 2000;
843 req.ContentLength = 2;
845 rs = req.GetRequestStream ();
847 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
848 rs.Write (buffer, 0, buffer.Length);
852 // non-buffered, non-chunked
853 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
860 req = (HttpWebRequest) WebRequest.Create (url);
861 req.AllowWriteStreamBuffering = false;
864 req.ReadWriteTimeout = 2000;
865 req.ContentLength = 2;
867 rs = req.GetRequestStream ();
870 buffer = new byte [] { 0x2a, 0x1d };
872 rs.Write (buffer, 0, buffer.Length);
874 } catch (ProtocolViolationException ex) {
875 // Bytes to be written to the stream exceed
876 // Content-Length bytes size specified
877 Assert.IsNull (ex.InnerException, "#C2");
878 Assert.IsNotNull (ex.Message, "#3");
883 req = (HttpWebRequest) WebRequest.Create (url);
884 req.AllowWriteStreamBuffering = false;
887 req.ReadWriteTimeout = 2000;
888 req.ContentLength = 2;
890 rs = req.GetRequestStream ();
892 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
894 rs.Write (buffer, 0, buffer.Length);
896 } catch (ProtocolViolationException ex) {
897 // Bytes to be written to the stream exceed
898 // Content-Length bytes size specified
899 Assert.IsNull (ex.InnerException, "#D2");
900 Assert.IsNotNull (ex.Message, "#D3");
906 // non-buffered, chunked
907 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
914 req = (HttpWebRequest) WebRequest.Create (url);
915 req.AllowWriteStreamBuffering = false;
917 req.SendChunked = true;
919 req.ReadWriteTimeout = 2000;
920 req.ContentLength = 2;
922 rs = req.GetRequestStream ();
925 buffer = new byte [] { 0x2a, 0x1d };
926 rs.Write (buffer, 0, buffer.Length);
929 req = (HttpWebRequest) WebRequest.Create (url);
930 req.AllowWriteStreamBuffering = false;
932 req.SendChunked = true;
934 req.ReadWriteTimeout = 2000;
935 req.ContentLength = 2;
937 rs = req.GetRequestStream ();
939 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
940 rs.Write (buffer, 0, buffer.Length);
946 [Ignore ("This test asserts that our code violates RFC 2616")]
947 public void GetRequestStream_Body_NotAllowed ()
949 string [] methods = new string [] { "GET", "HEAD", "CONNECT",
950 "get", "HeAd", "ConNect" };
952 foreach (string method in methods) {
953 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
954 "http://localhost:8000");
957 req.GetRequestStream ();
958 Assert.Fail ("#1:" + method);
959 } catch (ProtocolViolationException ex) {
960 Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
961 Assert.IsNull (ex.InnerException, "#3:" + method);
962 Assert.IsNotNull (ex.Message, "#4:" + method);
967 [Test] // bug #511851
968 public void GetResponse_Request_Aborted ()
970 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
971 string url = "http://" + ep.ToString () + "/test/";
973 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
976 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
983 } catch (WebException ex) {
984 // The request was aborted: The request was canceled
985 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
986 Assert.IsNull (ex.InnerException, "#3");
987 Assert.IsNotNull (ex.Message, "#4");
988 Assert.IsNull (ex.Response, "#5");
989 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
995 [Ignore ("This does not timeout any more. That's how MS works when reading small responses")]
996 public void ReadTimeout ()
998 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
999 string url = "http://" + localEP.ToString () + "/original/";
1001 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1004 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1005 req.Method = "POST";
1006 req.AllowAutoRedirect = false;
1008 req.ReadWriteTimeout = 2000;
1009 req.KeepAlive = false;
1010 Stream rs = req.GetRequestStream ();
1012 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1014 Stream s = resp.GetResponseStream ();
1017 } catch (WebException ex) {
1018 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
1019 Assert.IsNull (ex.InnerException, "#3");
1020 Assert.IsNull (ex.Response, "#4");
1021 Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
1028 [Test] // bug #324300
1029 public void AllowAutoRedirect ()
1031 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8765);
1032 string url = "http://" + localEP.ToString () + "/original/";
1034 // allow autoredirect
1035 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1038 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1039 req.Method = "POST";
1041 req.ReadWriteTimeout = 2000;
1042 req.KeepAlive = false;
1043 Stream rs = req.GetRequestStream ();
1045 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1046 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1048 string body = sr.ReadToEnd ();
1050 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1051 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1052 localEP.ToString () + "/moved/", "#A2");
1053 Assert.AreEqual ("GET", resp.Method, "#A3");
1054 Assert.AreEqual ("LOOKS OK", body, "#A4");
1059 // do not allow autoredirect
1060 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1063 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1064 req.Method = "POST";
1065 req.AllowAutoRedirect = false;
1067 req.ReadWriteTimeout = 1000;
1068 req.KeepAlive = false;
1069 Stream rs = req.GetRequestStream ();
1071 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1072 Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
1073 Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
1074 Assert.AreEqual ("POST", resp.Method, "#B3");
1081 public void PostAndRedirect_NoCL ()
1083 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8769);
1084 string url = "http://" + localEP.ToString () + "/original/";
1086 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1089 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1090 req.Method = "POST";
1092 req.ReadWriteTimeout = 2000;
1093 Stream rs = req.GetRequestStream ();
1096 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1097 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1099 string body = sr.ReadToEnd ();
1101 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1102 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1103 localEP.ToString () + "/moved/", "#A2");
1104 Assert.AreEqual ("GET", resp.Method, "#A3");
1105 Assert.AreEqual ("LOOKS OK", body, "#A4");
1112 public void PostAndRedirect_CL ()
1114 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8770);
1115 string url = "http://" + localEP.ToString () + "/original/";
1117 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1120 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1121 req.Method = "POST";
1123 req.ReadWriteTimeout = 2000;
1124 req.ContentLength = 1;
1125 Stream rs = req.GetRequestStream ();
1127 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1128 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1130 string body = sr.ReadToEnd ();
1132 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1133 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1134 localEP.ToString () + "/moved/", "#A2");
1135 Assert.AreEqual ("GET", resp.Method, "#A3");
1136 Assert.AreEqual ("LOOKS OK", body, "#A4");
1143 public void PostAnd401 ()
1145 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8771);
1146 string url = "http://" + localEP.ToString () + "/original/";
1148 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
1151 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1152 req.Method = "POST";
1154 req.ReadWriteTimeout = 2000;
1155 req.ContentLength = 1;
1156 Stream rs = req.GetRequestStream ();
1158 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1159 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1161 string body = sr.ReadToEnd ();
1163 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1164 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1165 localEP.ToString () + "/moved/", "#A2");
1166 Assert.AreEqual ("GET", resp.Method, "#A3");
1167 Assert.AreEqual ("LOOKS OK", body, "#A4");
1173 [Test] // bug #324347
1174 [Category ("NotWorking")]
1175 public void InternalServerError ()
1177 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8766);
1178 string url = "http://" + localEP.ToString () + "/original/";
1181 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
1184 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1185 req.Method = "POST";
1187 req.ReadWriteTimeout = 2000;
1188 req.KeepAlive = false;
1189 Stream rs = req.GetRequestStream ();
1194 Assert.Fail ("#A1");
1195 } catch (WebException ex) {
1196 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1197 Assert.IsNull (ex.InnerException, "#A3");
1198 Assert.IsNotNull (ex.Message, "#A4");
1199 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
1201 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1202 Assert.IsNotNull (webResponse, "#A6");
1203 Assert.AreEqual ("POST", webResponse.Method, "#A7");
1204 webResponse.Close ();
1211 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
1214 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1217 req.ReadWriteTimeout = 2000;
1218 req.KeepAlive = false;
1222 Assert.Fail ("#B1");
1223 } catch (WebException ex) {
1224 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1225 Assert.IsNull (ex.InnerException, "#B3");
1226 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1228 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1229 Assert.IsNotNull (webResponse, "#B5");
1230 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1231 webResponse.Close ();
1239 [Category ("NotWorking")] // #B3 fails; we get a SocketException: An existing connection was forcibly closed by the remote host
1240 public void NoContentLength ()
1242 IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8767);
1243 string url = "http://" + localEP.ToString () + "/original/";
1246 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
1249 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1250 req.Method = "POST";
1252 req.ReadWriteTimeout = 2000;
1253 req.KeepAlive = false;
1254 Stream rs = req.GetRequestStream ();
1259 Assert.Fail ("#A1");
1260 } catch (WebException ex) {
1261 // The underlying connection was closed:
1262 // An unexpected error occurred on a
1264 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1265 Assert.IsNotNull (ex.InnerException, "#A3");
1266 Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
1267 Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
1269 // Unable to read data from the transport connection:
1270 // A connection attempt failed because the connected party
1271 // did not properly respond after a period of time, or
1272 // established connection failed because connected host has
1273 // failed to respond
1274 IOException ioe = (IOException) ex.InnerException;
1275 Assert.IsNotNull (ioe.InnerException, "#A6");
1276 Assert.IsNotNull (ioe.Message, "#A7");
1277 Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
1279 // An existing connection was forcibly
1280 // closed by the remote host
1281 SocketException soe = (SocketException) ioe.InnerException;
1282 Assert.IsNull (soe.InnerException, "#A9");
1283 Assert.IsNotNull (soe.Message, "#A10");
1285 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1286 Assert.IsNull (webResponse, "#A11");
1293 using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
1296 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1299 req.ReadWriteTimeout = 2000;
1300 req.KeepAlive = false;
1304 Assert.Fail ("#B1");
1305 } catch (WebException ex) {
1306 // The remote server returned an error:
1307 // (500) Internal Server Error
1308 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1309 Assert.IsNull (ex.InnerException, "#B3");
1310 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1312 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1313 Assert.IsNotNull (webResponse, "#B5");
1314 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1315 webResponse.Close ();
1322 [Test] // bug #513087
1323 public void NonStandardVerb ()
1325 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
1326 string url = "http://" + ep.ToString () + "/moved/";
1328 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (VerbEchoHandler))) {
1331 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1332 req.Method = "WhatEver";
1333 req.KeepAlive = false;
1334 req.Timeout = 20000;
1335 req.ReadWriteTimeout = 20000;
1337 Stream rs = req.GetRequestStream ();
1340 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1341 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1343 string body = sr.ReadToEnd ();
1345 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#1");
1346 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1347 ep.ToString () + "/moved/", "#2");
1348 Assert.AreEqual ("WhatEver", resp.Method, "#3");
1349 Assert.AreEqual ("WhatEver", body, "#4");
1357 [Category ("NotWorking")] // Assert #2 fails
1358 public void NotModifiedSince ()
1360 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
1361 string url = "http://" + ep.ToString () + "/test/";
1363 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (NotModifiedSinceHandler))) {
1366 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1368 req.KeepAlive = false;
1369 req.Timeout = 20000;
1370 req.ReadWriteTimeout = 20000;
1371 req.Headers.Add (HttpRequestHeader.IfNoneMatch, "898bbr2347056cc2e096afc66e104653");
1372 req.IfModifiedSince = new DateTime (2010, 01, 04);
1374 DateTime start = DateTime.Now;
1375 HttpWebResponse response = null;
1380 } catch (WebException e) {
1381 response = (HttpWebResponse) e.Response;
1384 Assert.IsNotNull (response, "#2");
1385 using (Stream stream = response.GetResponseStream ()) {
1386 byte [] buffer = new byte [4096];
1387 int bytesRead = stream.Read (buffer, 0, buffer.Length);
1388 Assert.AreEqual (0, bytesRead, "#3");
1391 TimeSpan elapsed = DateTime.Now - start;
1392 Assert.IsTrue (elapsed.TotalMilliseconds < 2000, "#4");
1398 [Test] // bug #353495
1399 [Category ("NotWorking")]
1400 public void LastModifiedKind ()
1402 const string reqURL = "http://coffeefaq.com/site/node/25";
1403 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
1404 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
1405 DateTime lastMod = resp.LastModified;
1406 //string rawLastMod = resp.Headers ["Last-Modified"];
1408 //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
1409 Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
1410 req = (HttpWebRequest) WebRequest.Create (reqURL);
1411 req.IfModifiedSince = lastMod;
1413 resp = (HttpWebResponse) req.GetResponse ();
1415 Assert.Fail ("Should result in 304");
1416 } catch (WebException ex) {
1417 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
1418 Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
1423 #region Timeout_Bug // https://bugzilla.novell.com/show_bug.cgi?id=317553
1425 class TimeoutTestHelper {
1428 internal DateTime? Start { get; private set; }
1429 internal DateTime? End { get; private set; }
1430 internal Exception Exception { get; private set; }
1431 internal string Body { get; private set; }
1432 internal int TimeOutInMilliSeconds { get; private set; }
1434 internal TimeoutTestHelper (string url, int timeoutInMilliseconds)
1437 TimeOutInMilliSeconds = timeoutInMilliseconds;
1440 internal void LaunchWebRequest ()
1442 var req = (HttpWebRequest) WebRequest.Create (url_to_test);
1443 req.Timeout = TimeOutInMilliSeconds;
1445 Start = DateTime.Now;
1447 using (var resp = (HttpWebResponse) req.GetResponse ())
1449 var sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
1450 Body = sr.ReadToEnd ();
1452 } catch (Exception e) {
1459 void TestTimeOut (string url, WebExceptionStatus expectedExceptionStatus)
1461 var timeoutWorker = new TimeoutTestHelper (url, three_seconds_in_milliseconds);
1462 var threadStart = new ThreadStart (timeoutWorker.LaunchWebRequest);
1463 var thread = new Thread (threadStart);
1465 Thread.Sleep (three_seconds_in_milliseconds * 3);
1467 if (timeoutWorker.End == null) {
1469 Assert.Fail ("Thread finished after triple the timeout specified has passed");
1472 if (!String.IsNullOrEmpty (timeoutWorker.Body)) {
1473 if (timeoutWorker.Body == response_of_timeout_handler) {
1474 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve proper body");
1476 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve an incorrect body: " + timeoutWorker.Body);
1479 Assert.IsNotNull (timeoutWorker.Exception, "Exception was not thrown");
1481 var webEx = timeoutWorker.Exception as WebException;
1482 Assert.IsNotNull (webEx, "Exception thrown should be WebException, but was: " +
1483 timeoutWorker.Exception.GetType ().FullName);
1485 Assert.AreEqual (expectedExceptionStatus, webEx.Status,
1486 "WebException was thrown, but with a wrong status (should be " + expectedExceptionStatus + "): " + webEx.Status);
1488 Assert.IsFalse (timeoutWorker.End > (timeoutWorker.Start + TimeSpan.FromMilliseconds (three_seconds_in_milliseconds + 500)),
1489 "Timeout exception should have been thrown shortly after timeout is reached, however it was at least half-second late");
1492 [Test] // 1st possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1493 public void TestTimeoutPropertyWithServerThatExistsAndRespondsButTooLate ()
1495 var ep = new IPEndPoint (IPAddress.Loopback, 8123);
1496 string url = "http://" + ep + "/foobar/";
1498 using (var responder = new SocketResponder (ep, TimeOutHandler))
1502 TestTimeOut (url, WebExceptionStatus.Timeout);
1508 [Test] // 2nd possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1509 public void TestTimeoutWithEndpointThatDoesntExistThrowsConnectFailureBeforeTimeout ()
1511 string url = "http://127.0.0.1:8271/"; // some endpoint that is unlikely to exist
1513 // connecting to a non-existing endpoint should throw a ConnectFailure before the timeout is reached
1514 TestTimeOut (url, WebExceptionStatus.ConnectFailure);
1517 const string response_of_timeout_handler = "RESPONSE_OF_TIMEOUT_HANDLER";
1518 const int three_seconds_in_milliseconds = 3000;
1520 private static byte[] TimeOutHandler (Socket socket)
1522 socket.Receive (new byte[4096]);
1524 Thread.Sleep (three_seconds_in_milliseconds * 2);
1526 var sw = new StringWriter ();
1527 sw.WriteLine ("HTTP/1.1 200 OK");
1528 sw.WriteLine ("Content-Type: text/plain");
1529 sw.WriteLine ("Content-Length: " + response_of_timeout_handler.Length);
1531 sw.Write (response_of_timeout_handler);
1534 return Encoding.UTF8.GetBytes (sw.ToString ());
1539 internal static byte [] EchoRequestHandler (Socket socket)
1541 MemoryStream ms = new MemoryStream ();
1542 byte [] buffer = new byte [4096];
1543 int bytesReceived = socket.Receive (buffer);
1544 while (bytesReceived > 0) {
1545 ms.Write (buffer, 0, bytesReceived);
1546 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1547 // after sending the headers
1549 if (socket.Available > 0) {
1550 bytesReceived = socket.Receive (buffer);
1557 StreamReader sr = new StreamReader (ms, Encoding.UTF8);
1558 string request = sr.ReadToEnd ();
1560 StringWriter sw = new StringWriter ();
1561 sw.WriteLine ("HTTP/1.1 200 OK");
1562 sw.WriteLine ("Content-Type: text/xml");
1563 sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
1568 return Encoding.UTF8.GetBytes (sw.ToString ());
1571 static byte [] RedirectRequestHandler (Socket socket)
1573 MemoryStream ms = new MemoryStream ();
1574 byte [] buffer = new byte [4096];
1575 int bytesReceived = socket.Receive (buffer);
1576 while (bytesReceived > 0) {
1577 ms.Write (buffer, 0, bytesReceived);
1578 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1579 // after sending the headers
1581 if (socket.Available > 0) {
1582 bytesReceived = socket.Receive (buffer);
1589 string statusLine = null;
1590 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1591 statusLine = sr.ReadLine ();
1594 StringWriter sw = new StringWriter ();
1595 if (statusLine.StartsWith ("POST /original/")) {
1596 sw.WriteLine ("HTTP/1.0 302 Found");
1597 EndPoint ep = socket.LocalEndPoint;
1598 sw.WriteLine ("Location: " + "http://" + ep.ToString () + "/moved/");
1601 } else if (statusLine.StartsWith ("GET /moved/")) {
1602 sw.WriteLine ("HTTP/1.0 200 OK");
1603 sw.WriteLine ("Content-Type: text/plain");
1604 sw.WriteLine ("Content-Length: 8");
1606 sw.Write ("LOOKS OK");
1609 sw.WriteLine ("HTTP/1.0 500 Too Lazy");
1614 return Encoding.UTF8.GetBytes (sw.ToString ());
1617 static byte [] InternalErrorHandler (Socket socket)
1619 byte [] buffer = new byte [4096];
1620 int bytesReceived = socket.Receive (buffer);
1621 while (bytesReceived > 0) {
1622 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1623 // after sending the headers
1625 if (socket.Available > 0) {
1626 bytesReceived = socket.Receive (buffer);
1631 StringWriter sw = new StringWriter ();
1632 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1633 sw.WriteLine ("Content-Length: 0");
1637 return Encoding.UTF8.GetBytes (sw.ToString ());
1640 static byte [] NoContentLengthHandler (Socket socket)
1642 StringWriter sw = new StringWriter ();
1643 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1647 return Encoding.UTF8.GetBytes (sw.ToString ());
1650 static byte [] NotModifiedSinceHandler (Socket socket)
1652 StringWriter sw = new StringWriter ();
1653 sw.WriteLine ("HTTP/1.1 304 Not Modified");
1654 sw.WriteLine ("Date: Fri, 06 Feb 2009 12:50:26 GMT");
1655 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");
1656 sw.WriteLine ("Not-Modified-Since: Sun, 08 Feb 2009 08:49:26 GMT");
1657 sw.WriteLine ("ETag: 898bbr2347056cc2e096afc66e104653");
1658 sw.WriteLine ("Connection: close");
1662 return Encoding.UTF8.GetBytes (sw.ToString ());
1665 static byte [] VerbEchoHandler (Socket socket)
1667 MemoryStream ms = new MemoryStream ();
1668 byte [] buffer = new byte [4096];
1669 int bytesReceived = socket.Receive (buffer);
1670 while (bytesReceived > 0) {
1671 ms.Write (buffer, 0, bytesReceived);
1672 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1673 // after sending the headers
1675 if (socket.Available > 0) {
1676 bytesReceived = socket.Receive (buffer);
1683 string statusLine = null;
1684 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1685 statusLine = sr.ReadLine ();
1688 string verb = "DEFAULT";
1689 if (statusLine != null) {
1690 string [] parts = statusLine.Split (' ');
1691 if (parts.Length > 0)
1695 StringWriter sw = new StringWriter ();
1696 sw.WriteLine ("HTTP/1.1 200 OK");
1697 sw.WriteLine ("Content-Type: text/plain");
1698 sw.WriteLine ("Content-Length: " + verb.Length);
1703 return Encoding.UTF8.GetBytes (sw.ToString ());
1706 static byte [] PostAnd401Handler (Socket socket)
1708 MemoryStream ms = new MemoryStream ();
1709 byte [] buffer = new byte [4096];
1710 int bytesReceived = socket.Receive (buffer);
1711 while (bytesReceived > 0) {
1712 ms.Write (buffer, 0, bytesReceived);
1713 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1714 // after sending the headers
1716 if (socket.Available > 0) {
1717 bytesReceived = socket.Receive (buffer);
1724 string statusLine = null;
1725 bool have_auth = false;
1727 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1729 while ((l = sr.ReadLine ()) != null) {
1730 if (statusLine == null) {
1732 } else if (l.StartsWith ("Authorization:")) {
1734 } else if (l.StartsWith ("Content-Length:")) {
1735 cl = Int32.Parse (l.Substring ("content-length: ".Length));
1740 StringWriter sw = new StringWriter ();
1742 sw.WriteLine ("HTTP/1.0 401 Invalid Credentials");
1743 sw.WriteLine ("WWW-Authenticate: basic Yeah");
1746 } else if (cl > 0 && statusLine.StartsWith ("POST ")) {
1747 sw.WriteLine ("HTTP/1.0 200 OK");
1748 sw.WriteLine ("Content-Type: text/plain");
1749 sw.WriteLine ("Content-Length: 8");
1751 sw.Write ("LOOKS OK");
1754 sw.WriteLine ("HTTP/1.0 500 test failed");
1755 sw.WriteLine ("Content-Length: 0");
1760 return Encoding.UTF8.GetBytes (sw.ToString ());
1763 public void NtlmAuthentication ()
1765 NtlmServer server = new NtlmServer ();
1768 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
1769 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
1770 request.Timeout = 5000;
1771 request.Credentials = new NetworkCredential ("user", "password", "domain");
1772 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
1774 using (StreamReader reader = new StreamReader (resp.GetResponseStream ())) {
1775 res = reader.ReadToEnd ();
1779 Assert.AreEqual ("OK", res);
1782 class NtlmServer : HttpServer {
1783 public string Where = "";
1784 protected override void Run ()
1786 Where = "before accept";
1787 Socket client = sock.Accept ();
1788 NetworkStream ns = new NetworkStream (client, false);
1789 StreamReader reader = new StreamReader (ns, Encoding.ASCII);
1791 Where = "first read";
1792 while ((line = reader.ReadLine ()) != null) {
1793 if (line.Trim () == String.Empty) {
1797 Where = "first write";
1798 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1799 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1800 "WWW-Authenticate: NTLM\r\n" +
1801 "Content-Length: 5\r\n\r\nWRONG");
1804 Where = "second read";
1805 while ((line = reader.ReadLine ()) != null) {
1806 if (line.Trim () == String.Empty) {
1810 Where = "second write";
1811 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1812 "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAADgAAAABggAC8GDhqIONH3sAAAAAAAAAAAAAAAA4AAAABQLODgAAAA8=\r\n" +
1813 "Content-Length: 5\r\n\r\nWRONG");
1816 Where = "third read";
1817 while ((line = reader.ReadLine ()) != null) {
1818 if (line.Trim () == String.Empty) {
1822 Where = "third write";
1823 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1824 "Keep-Alive: true\r\n" +
1825 "Content-Length: 2\r\n\r\nOK");
1827 Thread.Sleep (1000);
1834 class BadChunkedServer : HttpServer {
1835 protected override void Run ()
1837 Socket client = sock.Accept ();
1838 NetworkStream ns = new NetworkStream (client, true);
1839 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1840 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1841 "Transfer-Encoding: chunked\r\n" +
1842 "Connection: close\r\n" +
1843 "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
1845 // This body lacks a 'last-chunk' (see RFC 2616)
1846 writer.Write ("10\r\n1234567890123456\r\n");
1848 client.Shutdown (SocketShutdown.Send);
1849 Thread.Sleep (1000);
1854 class AcceptAllPolicy : ICertificatePolicy {
1855 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
1861 abstract class HttpServer
1863 protected Socket sock;
1864 protected Exception error;
1865 protected ManualResetEvent evt;
1867 public HttpServer ()
1869 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
1870 sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
1874 public void Start ()
1876 evt = new ManualResetEvent (false);
1877 Thread th = new Thread (new ThreadStart (Run));
1887 public IPAddress IPAddress {
1888 get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
1892 get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
1895 public Exception Error {
1896 get { return error; }
1899 protected abstract void Run ();
1903 public void BeginGetRequestStream ()
1909 r.ContentLength = 0;
1910 r.BeginGetRequestStream ((a) =>
1912 using (Stream s = r.EndGetRequestStream (a)) { };
1921 public void BeginGetRequestStreamNoClose ()
1926 r.ContentLength = 1;
1927 r.BeginGetRequestStream ((a) =>
1929 r.EndGetRequestStream (a);
1938 public void BeginGetRequestStreamCancelIfNotAllBytesWritten ()
1944 r.ContentLength = 10;
1945 r.BeginGetRequestStream ((a) =>
1947 WebException ex = ExceptionAssert.Throws<WebException> (() =>
1949 using (Stream s = r.EndGetRequestStream (a)) {
1953 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1962 public void GetRequestStream2 ()
1968 r.ContentLength = data64KB.Length;
1969 using (Stream s = r.GetRequestStream ()) {
1970 s.Write (data64KB, 0, data64KB.Length);
1978 public void GetRequestStreamNotAllBytesWritten ()
1984 r.ContentLength = data64KB.Length;
1985 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ().Close ());
1986 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1993 public void GetRequestStreamTimeout ()
1999 r.ContentLength = data64KB.Length;
2001 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ());
2002 Assert.IsTrue (ex.Status == WebExceptionStatus.Timeout || ex.Status == WebExceptionStatus.ConnectFailure);
2008 public void BeginWrite ()
2010 byte[] received = new byte[data64KB.Length];
2016 r.ContentLength = data64KB.Length;
2018 Stream s = r.GetRequestStream ();
2019 s.BeginWrite (data64KB, 0, data64KB.Length,
2024 r.GetResponse ().Close ();
2031 c.Request.InputStream.ReadAll (received, 0, received.Length);
2032 c.Response.StatusCode = 204;
2033 c.Response.Close ();
2036 Assert.AreEqual (data64KB, received);
2040 public void BeginWriteAfterAbort ()
2042 byte [] received = new byte [data64KB.Length];
2048 r.ContentLength = data64KB.Length;
2050 Stream s = r.GetRequestStream ();
2053 WebException ex = ExceptionAssert.Throws<WebException> (() => s.BeginWrite (data64KB, 0, data64KB.Length, null, null));
2054 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2060 //c.Request.InputStream.ReadAll (received, 0, received.Length);
2061 //c.Response.StatusCode = 204;
2062 //c.Response.Close();
2067 public void PrematureStreamCloseAborts ()
2069 byte [] received = new byte [data64KB.Length];
2075 r.ContentLength = data64KB.Length * 2;
2077 Stream s = r.GetRequestStream ();
2078 s.Write (data64KB, 0, data64KB.Length);
2080 WebException ex = ExceptionAssert.Throws<WebException>(() => s.Close());
2081 Assert.AreEqual(ex.Status, WebExceptionStatus.RequestCanceled);
2087 c.Request.InputStream.ReadAll (received, 0, received.Length);
2088 // c.Response.StatusCode = 204;
2089 // c.Response.Close ();
2094 public void Write ()
2096 byte [] received = new byte [data64KB.Length];
2102 r.ContentLength = data64KB.Length;
2104 using (Stream s = r.GetRequestStream ()) {
2105 s.Write (data64KB, 0, data64KB.Length);
2108 r.GetResponse ().Close ();
2113 c.Request.InputStream.ReadAll (received, 0, received.Length);
2114 c.Response.StatusCode = 204;
2115 c.Response.Close ();
2118 Assert.AreEqual(data64KB, received);
2122 Invalid test: it does not work on linux.
2123 [pid 30973] send(9, "POST / HTTP/1.1\r\nContent-Length:"..., 89, 0) = 89
2125 [pid 30970] send(16, "HTTP/1.1 200 OK\r\nServer: Mono-HT"..., 133, 0) = 133
2127 [pid 30970] close(16) = 0
2129 [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
2131 [pid 30966] close(4) = 0
2134 The server sideis closed (FD 16) and the send on the client side (FD 9) succeeds.
2136 [Category("NotWorking")]
2137 public void WriteServerAborts ()
2139 ManualResetEvent abort = new ManualResetEvent (false);
2140 byte [] received = new byte [data64KB.Length];
2146 r.ContentLength = data64KB.Length;
2148 using (Stream s = r.GetRequestStream()) {
2151 IOException ex = ExceptionAssert.Throws<IOException> (() => s.Write(data64KB, 0, data64KB.Length));
2167 byte [] received = new byte [data64KB.Length];
2172 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2173 using (Stream s = x.GetResponseStream()) {
2174 s.ReadAll (received, 0, received.Length);
2181 c.Response.StatusCode = 200;
2182 c.Response.ContentLength64 = data64KB.Length;
2183 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2184 c.Response.OutputStream.Close ();
2185 c.Response.Close ();
2188 Assert.AreEqual (data64KB, received);
2192 public void ReadTimeout2 ()
2194 byte [] received = new byte [data64KB.Length];
2199 r.ReadWriteTimeout = 10;
2200 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2201 using (Stream s = x.GetResponseStream ()) {
2202 WebException ex = ExceptionAssert.Throws<WebException> (() => s.ReadAll (received, 0, received.Length));
2203 Assert.AreEqual (ex.Status, WebExceptionStatus.Timeout);
2210 c.Response.StatusCode = 200;
2211 c.Response.ContentLength64 = data64KB.Length;
2212 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length / 2);
2213 Thread.Sleep (1000);
2214 // c.Response.OutputStream.Write (data64KB, data64KB.Length / 2, data64KB.Length / 2);
2215 c.Response.OutputStream.Close ();
2216 c.Response.Close ();
2221 public void ReadServerAborted ()
2223 byte [] received = new byte [data64KB.Length];
2228 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2229 using (Stream s = x.GetResponseStream ()) {
2230 Assert.AreEqual (1, s.ReadAll (received, 0, received.Length));
2237 c.Response.StatusCode = 200;
2238 c.Response.ContentLength64 = data64KB.Length;
2239 c.Response.OutputStream.Write (data64KB, 0, 1);
2240 c.Response.Abort ();
2245 public void BeginGetResponse2 ()
2247 byte [] received = new byte [data64KB.Length];
2252 r.BeginGetResponse ((a) =>
2254 using (HttpWebResponse x = (HttpWebResponse) r.EndGetResponse (a))
2255 using (Stream s = x.GetResponseStream ()) {
2256 s.ReadAll (received, 0, received.Length);
2264 c.Response.StatusCode = 200;
2265 c.Response.ContentLength64 = data64KB.Length;
2266 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2267 c.Response.OutputStream.Close ();
2268 c.Response.Close ();
2271 Assert.AreEqual (data64KB, received);
2275 public void BeginGetResponseAborts ()
2277 ManualResetEvent aborted = new ManualResetEvent(false);
2282 r.BeginGetResponse((a) =>
2284 WebException ex = ExceptionAssert.Throws<WebException> (() => r.EndGetResponse (a));
2285 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2295 // Thread.Sleep (100);
2296 // c.Response.StatusCode = 200;
2297 // c.Response.ContentLength64 = 0;
2298 // c.Response.Close ();
2304 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request)
2306 int port = NetworkHelpers.FindFreePort ();
2308 ManualResetEvent completed = new ManualResetEvent (false);
2309 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2310 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2312 request (client, completed);
2314 if (!completed.WaitOne (10000))
2315 Assert.Fail ("Test hung");
2318 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request, Action<HttpListenerContext> processor)
2320 int port = NetworkHelpers.FindFreePort ();
2322 ManualResetEvent [] completed = new ManualResetEvent [2];
2323 completed [0] = new ManualResetEvent (false);
2324 completed [1] = new ManualResetEvent (false);
2326 using (ListenerScope scope = new ListenerScope (processor, port, completed [0])) {
2327 ManualResetEvent clientCompleted = new ManualResetEvent (false);
2328 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2329 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2331 ThreadPool.QueueUserWorkItem ((o) => request (client, completed [1]));
2333 if (!WaitHandle.WaitAll (completed, 10000))
2334 Assert.Fail ("Test hung.");
2340 [ExpectedException (typeof (ArgumentNullException))]
2341 public void NullHost ()
2343 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2348 public void NoHost ()
2350 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2351 Assert.AreEqual (req.Host, "go-mono.com");
2355 [ExpectedException (typeof (ArgumentException))]
2356 public void EmptyHost ()
2358 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2363 public void HostAndPort ()
2365 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:80");
2366 Assert.AreEqual ("go-mono.com", req.Host, "#01");
2367 req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:9000");
2368 Assert.AreEqual ("go-mono.com:9000", req.Host, "#02");
2372 public void PortRange ()
2374 for (int i = 0; i < 65536; i++) {
2377 string s = i.ToString ();
2378 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:" + s);
2379 Assert.AreEqual ("go-mono.com:" + s, req.Host, "#" + s);
2384 [ExpectedException (typeof (ArgumentException))]
2385 public void PortBelow ()
2387 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2388 req.Host = "go-mono.com:-1";
2392 [ExpectedException (typeof (ArgumentException))]
2393 public void PortAbove ()
2395 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2396 req.Host = "go-mono.com:65536";
2400 [ExpectedException (typeof (ArgumentException))]
2401 public void HostTooLong ()
2403 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2404 string s = new string ('a', 100);
2405 req.Host = s + "." + s + "." + s + "." + s + "." + s + "." + s; // Over 255 bytes
2409 [Category ("NotWorking")] // #5490
2410 public void InvalidNamesThatWork ()
2412 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2416 req.Host = new string ('a', 64); // Should fail. Max. is 63.
2420 public void NoDate ()
2422 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2423 Assert.AreEqual (DateTime.MinValue, req.Date);
2427 public void UtcDate ()
2429 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2430 req.Date = DateTime.UtcNow;
2431 DateTime date = req.Date;
2432 Assert.AreEqual (DateTimeKind.Local, date.Kind);
2436 public void AddAndRemoveDate ()
2438 // Neil Armstrong set his foot on Moon
2439 var landing = new DateTime (1969, 7, 21, 2, 56, 0, DateTimeKind.Utc);
2440 Assert.AreEqual (621214377600000000, landing.Ticks);
2441 var unspecified = new DateTime (1969, 7, 21, 2, 56, 0);
2442 var local = landing.ToLocalTime ();
2444 var req = (HttpWebRequest)WebRequest.Create ("http://www.mono-project.com/");
2446 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2447 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2448 Assert.AreEqual (local, req.Date);
2450 req.Date = unspecified;
2451 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2452 Assert.AreEqual (unspecified.Ticks, req.Date.Ticks);
2453 Assert.AreEqual (unspecified, req.Date);
2456 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2457 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2458 Assert.AreEqual (local, req.Date);
2460 req.Date = DateTime.MinValue;
2461 Assert.AreEqual (DateTimeKind.Unspecified, DateTime.MinValue.Kind);
2462 Assert.AreEqual (DateTimeKind.Unspecified, req.Date.Kind);
2463 Assert.AreEqual (0, req.Date.Ticks);
2465 Assert.AreEqual (null, req.Headers.Get ("Date"));
2470 public void TestIPv6Host ()
2472 var address = "2001:0000:0000:0001:0001:0001:0157:0000";
2473 var address2 = '[' + address + ']';
2474 var uri = new Uri (string.Format ("http://{0}/test.css", address2));
2475 var hwr = (HttpWebRequest)WebRequest.Create (uri);
2477 hwr.Host = address2;
2478 Assert.AreEqual (address2, hwr.Host, "#1");
2483 [Category ("NotWorking")]
2484 public void TestIPv6Host2 ()
2486 var address = "2001:0000:0000:0001:0001:0001:0157:0000";
2487 var address2 = '[' + address + ']';
2488 var uri = new Uri (string.Format ("http://{0}/test.css", address2));
2489 var hwr = (HttpWebRequest)WebRequest.Create (uri);
2494 } catch (ArgumentException) {
2502 public void AllowReadStreamBuffering ()
2504 var hr = WebRequest.CreateHttp ("http://www.google.com");
2505 Assert.IsFalse (hr.AllowReadStreamBuffering, "#1");
2507 hr.AllowReadStreamBuffering = true;
2509 } catch (InvalidOperationException) {
2514 class ListenerScope : IDisposable {
2515 EventWaitHandle completed;
2516 public HttpListener listener;
2517 Action<HttpListenerContext> processor;
2519 public ListenerScope (Action<HttpListenerContext> processor, int port, EventWaitHandle completed)
2521 this.processor = processor;
2522 this.completed = completed;
2524 this.listener = new HttpListener ();
2525 this.listener.Prefixes.Add (string.Format ("http://localhost:{0}/", port));
2526 this.listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
2527 this.listener.Start ();
2529 this.listener.BeginGetContext (this.RequestHandler, null);
2532 void RequestHandler (IAsyncResult result)
2534 HttpListenerContext context = null;
2537 context = this.listener.EndGetContext (result);
2538 } catch (HttpListenerException ex) {
2539 // check if the thread has been aborted as in the case when we are shutting down.
2540 if (ex.ErrorCode == 995)
2542 } catch (ObjectDisposedException) {
2546 ThreadPool.QueueUserWorkItem ((o) =>
2549 this.processor (context);
2550 } catch (HttpListenerException) {
2554 this.completed.Set ();
2557 public void Dispose ()
2559 this.listener.Stop ();
2564 class SslHttpServer : HttpServer {
2565 X509Certificate _certificate;
2567 protected override void Run ()
2570 Socket client = sock.Accept ();
2571 NetworkStream ns = new NetworkStream (client, true);
2572 SslServerStream s = new SslServerStream (ns, Certificate, false, false);
2573 s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
2575 StreamReader reader = new StreamReader (s);
2576 StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
2579 string hello = "<html><body><h1>Hello World!</h1></body></html>";
2580 string answer = "HTTP/1.0 200\r\n" +
2581 "Connection: close\r\n" +
2582 "Content-Type: text/html\r\n" +
2583 "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
2584 "Content-Length: " + hello.Length + "\r\n" +
2589 line = reader.ReadLine ();
2590 } while (line != "" && line != null && line.Length > 0);
2592 // Now the content. We know it's 100 bytes.
2593 // This makes BeginRead in sslclientstream block.
2594 char [] cs = new char [100];
2595 reader.Read (cs, 0, 100);
2597 writer.Write (answer);
2599 if (evt.WaitOne (5000, false))
2600 error = new Exception ("Timeout when stopping the server");
2601 } catch (Exception e) {
2606 X509Certificate Certificate {
2608 if (_certificate == null)
2609 _certificate = new X509Certificate (CertData.Certificate);
2611 return _certificate;
2615 AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
2617 PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
2623 public readonly static byte [] Certificate = {
2624 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
2625 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
2626 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
2627 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
2628 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
2629 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
2630 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
2631 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
2632 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
2633 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
2634 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
2635 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
2636 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
2637 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
2638 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
2639 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
2640 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
2641 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
2642 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
2643 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
2644 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
2645 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
2646 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
2647 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
2648 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
2649 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
2650 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
2651 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
2655 public readonly static byte [] PrivateKey = {
2656 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
2657 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
2658 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
2659 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
2660 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
2661 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
2662 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
2663 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
2664 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
2665 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
2666 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
2667 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
2668 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
2669 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
2670 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
2671 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
2672 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
2673 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
2674 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
2675 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
2676 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
2677 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
2678 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
2679 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
2680 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
2681 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
2682 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
2683 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
2684 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
2685 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
2686 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
2687 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
2688 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
2689 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
2690 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
2691 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
2692 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
2693 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
2694 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
2700 public void CookieContainerTest ()
2702 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2703 string url = "http://" + ep.ToString ();
2705 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (CookieRequestHandler))) {
2708 CookieContainer container = new CookieContainer ();
2709 container.Add(new Uri (url), new Cookie ("foo", "bar"));
2710 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2711 request.CookieContainer = container;
2712 WebHeaderCollection headers = request.Headers;
2713 headers.Add("Cookie", "foo=baz");
2714 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2715 string responseString = null;
2716 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2717 responseString = reader.ReadToEnd ();
2720 Assert.AreEqual (1, response.Cookies.Count, "#01");
2721 Assert.AreEqual ("foo=bar", response.Headers.Get("Set-Cookie"), "#02");
2724 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (CookieRequestHandler))) {
2727 CookieContainer container = new CookieContainer ();
2728 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2729 request.CookieContainer = container;
2730 WebHeaderCollection headers = request.Headers;
2731 headers.Add("Cookie", "foo=baz");
2732 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2733 string responseString = null;
2734 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2735 responseString = reader.ReadToEnd ();
2738 Assert.AreEqual (0, response.Cookies.Count, "#03");
2739 Assert.AreEqual ("", response.Headers.Get("Set-Cookie"), "#04");
2743 internal static byte[] CookieRequestHandler (Socket socket)
2745 MemoryStream ms = new MemoryStream ();
2746 byte[] buffer = new byte[4096];
2747 int bytesReceived = socket.Receive (buffer);
2748 while (bytesReceived > 0) {
2749 ms.Write(buffer, 0, bytesReceived);
2750 // We don't check for Content-Length or anything else here, so we give the client a little time to write
2751 // after sending the headers
2753 if (socket.Available > 0) {
2754 bytesReceived = socket.Receive (buffer);
2761 string cookies = string.Empty;
2762 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
2764 while ((line = sr.ReadLine ()) != null) {
2765 if (line.StartsWith ("Cookie:")) {
2766 cookies = line.Substring ("cookie: ".Length);
2771 StringWriter sw = new StringWriter ();
2772 sw.WriteLine ("HTTP/1.1 200 OK");
2773 sw.WriteLine ("Content-Type: text/xml");
2774 sw.WriteLine ("Set-Cookie: " + cookies);
2775 sw.WriteLine ("Content-Length: " + cookies.Length.ToString (CultureInfo.InvariantCulture));
2780 return Encoding.UTF8.GetBytes (sw.ToString ());
2785 public class HttpRequestStreamTest
2788 public void BeginRead ()
2790 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2791 string url = "http://" + ep.ToString () + "/test/";
2793 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2796 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2797 req.Method = "POST";
2799 using (Stream rs = req.GetRequestStream ()) {
2800 byte [] buffer = new byte [10];
2802 rs.BeginRead (buffer, 0, buffer.Length, null, null);
2804 } catch (NotSupportedException ex) {
2805 // The stream does not support reading
2806 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2807 Assert.IsNull (ex.InnerException, "#3");
2808 Assert.IsNotNull (ex.Message, "#4");
2817 public void BeginWrite_Request_Aborted ()
2819 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2820 string url = "http://" + ep.ToString () + "/test/";
2822 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2825 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2826 req.Method = "POST";
2828 using (Stream rs = req.GetRequestStream ()) {
2831 rs.BeginWrite (new byte [] { 0x2a, 0x2f }, 0, 2, null, null);
2833 } catch (WebException ex) {
2834 // The request was aborted: The request was canceled
2835 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
2836 Assert.IsNull (ex.InnerException, "#3");
2837 Assert.IsNotNull (ex.Message, "#4");
2838 Assert.IsNull (ex.Response, "#5");
2839 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
2846 public void CanRead ()
2848 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2849 string url = "http://" + ep.ToString () + "/test/";
2851 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2854 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2855 req.Method = "POST";
2857 Stream rs = req.GetRequestStream ();
2859 Assert.IsFalse (rs.CanRead, "#1");
2861 Assert.IsFalse (rs.CanRead, "#2");
2870 public void CanSeek ()
2872 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
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 Stream rs = req.GetRequestStream ();
2883 Assert.IsFalse (rs.CanSeek, "#1");
2885 Assert.IsFalse (rs.CanSeek, "#2");
2893 [Test] // bug #324182
2894 public void CanTimeout ()
2896 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2897 string url = "http://" + ep.ToString () + "/test/";
2899 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2902 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2903 req.Method = "POST";
2905 Stream rs = req.GetRequestStream ();
2907 Assert.IsTrue (rs.CanTimeout, "#1");
2909 Assert.IsTrue (rs.CanTimeout, "#2");
2918 public void CanWrite ()
2920 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2921 string url = "http://" + ep.ToString () + "/test/";
2923 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2926 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2927 req.Method = "POST";
2929 Stream rs = req.GetRequestStream ();
2931 Assert.IsTrue (rs.CanWrite, "#1");
2933 Assert.IsFalse (rs.CanWrite, "#2");
2944 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2945 string url = "http://" + ep.ToString () + "/test/";
2947 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
2950 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2951 req.Method = "POST";
2953 using (Stream rs = req.GetRequestStream ()) {
2954 byte [] buffer = new byte [10];
2956 rs.Read (buffer, 0, buffer.Length);
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 ReadByte ()
2973 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
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 using (Stream rs = req.GetRequestStream ()) {
2986 } catch (NotSupportedException ex) {
2987 // The stream does not support reading
2988 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2989 Assert.IsNull (ex.InnerException, "#3");
2990 Assert.IsNotNull (ex.Message, "#4");
2999 public void ReadTimeout ()
3001 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3002 string url = "http://" + ep.ToString () + "/test/";
3004 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3007 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3008 req.Method = "POST";
3010 Stream rs = req.GetRequestStream ();
3012 Assert.AreEqual (300000, rs.ReadTimeout, "#1");
3014 Assert.AreEqual (300000, rs.ReadTimeout, "#2");
3025 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
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.Seek (0, SeekOrigin.Current);
3038 } catch (NotSupportedException ex) {
3039 // This stream does not support seek operations
3040 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
3041 Assert.IsNull (ex.InnerException, "#3");
3042 Assert.IsNotNull (ex.Message, "#4");
3051 public void Write_Buffer_Null ()
3053 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
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 ()) {
3064 rs.Write ((byte []) null, -1, -1);
3066 } catch (ArgumentNullException ex) {
3067 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3068 Assert.IsNull (ex.InnerException, "#3");
3069 Assert.IsNotNull (ex.Message, "#4");
3070 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3079 public void Write_Count_Negative ()
3081 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3082 string url = "http://" + ep.ToString () + "/test/";
3084 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3087 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3088 req.Method = "POST";
3090 using (Stream rs = req.GetRequestStream ()) {
3091 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3093 rs.Write (buffer, 1, -1);
3095 } catch (ArgumentOutOfRangeException ex) {
3096 // Specified argument was out of the range of valid values
3097 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3098 Assert.IsNull (ex.InnerException, "#A3");
3099 Assert.IsNotNull (ex.Message, "#A4");
3100 Assert.AreEqual ("size", ex.ParamName, "#A5");
3109 public void Write_Count_Overflow ()
3111 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3112 string url = "http://" + ep.ToString () + "/test/";
3114 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3117 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3118 req.Method = "POST";
3120 using (Stream rs = req.GetRequestStream ()) {
3121 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3123 rs.Write (buffer, buffer.Length - 2, 3);
3125 } catch (ArgumentOutOfRangeException ex) {
3126 // Specified argument was out of the range of valid values
3127 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3128 Assert.IsNull (ex.InnerException, "#3");
3129 Assert.IsNotNull (ex.Message, "#4");
3130 Assert.AreEqual ("size", ex.ParamName, "#5");
3139 public void Write_Offset_Negative ()
3141 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3142 string url = "http://" + ep.ToString () + "/test/";
3144 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3147 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3148 req.Method = "POST";
3150 using (Stream rs = req.GetRequestStream ()) {
3151 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3153 rs.Write (buffer, -1, 0);
3155 } catch (ArgumentOutOfRangeException ex) {
3156 // Specified argument was out of the range of valid values
3157 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3158 Assert.IsNull (ex.InnerException, "#3");
3159 Assert.IsNotNull (ex.Message, "#4");
3160 Assert.AreEqual ("offset", ex.ParamName, "#5");
3169 public void Write_Offset_Overflow ()
3171 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3172 string url = "http://" + ep.ToString () + "/test/";
3174 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3177 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3178 req.Method = "POST";
3180 using (Stream rs = req.GetRequestStream ()) {
3181 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3183 rs.Write (buffer, buffer.Length + 1, 0);
3185 } catch (ArgumentOutOfRangeException ex) {
3186 // Specified argument was out of the range of valid values
3187 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3188 Assert.IsNull (ex.InnerException, "#3");
3189 Assert.IsNotNull (ex.Message, "#4");
3190 Assert.AreEqual ("offset", ex.ParamName, "#5");
3199 public void Write_Request_Aborted ()
3201 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3202 string url = "http://" + ep.ToString () + "/test/";
3204 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3207 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3208 req.Method = "POST";
3210 using (Stream rs = req.GetRequestStream ()) {
3213 rs.Write (new byte [0], 0, 0);
3215 } catch (WebException ex) {
3216 // The request was aborted: The request was canceled
3217 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3218 Assert.IsNull (ex.InnerException, "#3");
3219 Assert.IsNotNull (ex.Message, "#4");
3220 Assert.IsNull (ex.Response, "#5");
3221 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
3228 [Category ("NotWorking")]
3229 public void Write_Stream_Closed ()
3231 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3232 string url = "http://" + ep.ToString () + "/test/";
3234 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3237 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3238 req.Method = "POST";
3240 using (Stream rs = req.GetRequestStream ()) {
3243 rs.Write (new byte [0], 0, 0);
3245 } catch (WebException ex) {
3246 // The request was aborted: The connection was closed unexpectedly
3247 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3248 Assert.IsNull (ex.InnerException, "#3");
3249 Assert.IsNotNull (ex.Message, "#4");
3250 Assert.IsNull (ex.Response, "#5");
3251 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#6");
3258 public void WriteByte_Request_Aborted ()
3260 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3261 string url = "http://" + ep.ToString () + "/test/";
3263 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3266 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3267 req.Method = "POST";
3269 using (Stream rs = req.GetRequestStream ()) {
3272 rs.WriteByte (0x2a);
3274 } catch (WebException ex) {
3275 // The request was aborted: The request was canceled
3276 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3277 Assert.IsNull (ex.InnerException, "#3");
3278 Assert.IsNotNull (ex.Message, "#4");
3279 Assert.IsNull (ex.Response, "#5");
3280 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
3287 public void WriteTimeout ()
3289 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3290 string url = "http://" + ep.ToString () + "/test/";
3292 using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (HttpWebRequestTest.EchoRequestHandler))) {
3295 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3296 req.Method = "POST";
3298 Stream rs = req.GetRequestStream ();
3300 Assert.AreEqual (300000, rs.WriteTimeout, "#1");
3302 Assert.AreEqual (300000, rs.WriteTimeout, "#2");
3313 // This test is supposed to fail prior to .NET 4.0
3314 public void Post_EmptyRequestStream ()
3316 var wr = HttpWebRequest.Create ("http://google.com");
3318 wr.GetRequestStream ();
3320 var gr = wr.BeginGetResponse (delegate { }, null);
3321 Assert.AreEqual (true, gr.AsyncWaitHandle.WaitOne (5000), "#1");
3326 static class StreamExtensions {
3327 public static int ReadAll(this Stream stream, byte[] buffer, int offset, int count)
3331 while (totalRead < count) {
3332 int bytesRead = stream.Read (buffer, offset + totalRead, count - totalRead);
3336 totalRead += bytesRead;
3343 static class ExceptionAssert {
3345 /// Asserts that the function throws an exception.
3347 /// <param name="f">A function execute that is expected to raise an exception.</param>
3348 /// <typeparam name="T">The type of exception that is expected.</typeparam>
3349 /// <returns>The exception thrown.</returns>
3350 /// <exception cref="AssertFailedException">If the function does not throw an exception
3351 /// or throws a different exception.</exception>
3352 /// <example><![CDATA[
3353 /// ExceptionAssert.Throws(typeof(ArgumentNullException), delegate {
3354 /// myObject.myFunction(null); });
3356 public static T Throws<T> (Action f) where T : Exception {
3357 Exception actualException = null;
3361 } catch (Exception ex) {
3362 actualException = ex;
3365 if (actualException == null)
3366 throw new AssertionException (string.Format (
3367 "No exception thrown. Expected '{0}'",
3368 typeof (T).FullName));
3369 else if (typeof(T) != actualException.GetType())
3370 throw new AssertionException (string.Format (
3371 "Caught exception of type '{0}'. Expected '{1}':{2}",
3372 actualException.GetType().FullName,
3373 typeof (T).FullName,
3374 Environment.NewLine + actualException));
3376 return (T) actualException;