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)
9 // Bogdanov Kirill (bogdanov@macroscop.com)
11 // (C) 2003 Martin Willemoes Hansen
12 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com
13 // Copyright (c) 2013 7digital Media Ltd (http://www.7digital.com)
16 using NUnit.Framework;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.Globalization;
23 using System.Net.Sockets;
24 using System.Security.Cryptography;
25 using System.Security.Cryptography.X509Certificates;
27 using System.Threading;
28 using System.Reflection;
29 using Mono.Security.Authenticode;
31 using Mono.Security.Protocol.Tls;
34 using MonoTests.Helpers;
36 namespace MonoTests.System.Net
39 public class HttpWebRequestTest
41 private Random rand = new Random ();
42 private byte [] data64KB = new byte [64 * 1024];
47 ServicePointManager.Expect100Continue = false;
48 rand.NextBytes (data64KB);
52 public void Proxy_Null ()
54 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
55 Assert.IsNotNull (req.Proxy, "#1");
57 Assert.IsNull (req.Proxy, "#2");
61 [Category("InetAccess")]
64 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
65 Assert.IsNotNull (req.IfModifiedSince, "req:If Modified Since: ");
67 req.UserAgent = "MonoClient v1.0";
68 Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
69 Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
71 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
72 Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
73 Assert.AreEqual ("OK", res.StatusDescription, "#B2");
75 Assert.IsTrue (res.Headers.Get ("Content-Type").StartsWith ("text/html; charset=", StringComparison.OrdinalIgnoreCase), "#C1");
76 Assert.IsNotNull (res.LastModified, "#C2");
77 Assert.AreEqual (0, res.Cookies.Count, "#C3");
83 public void AddRange ()
85 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
87 req.AddRange (50, 90);
88 req.AddRange ("bytes", 100);
89 req.AddRange ("bytes", 100, 120);
90 Assert.AreEqual ("bytes=10-,50-90,100-,100-120", req.Headers ["Range"], "#1");
92 req.AddRange ("bits", 2000);
94 } catch (InvalidOperationException) {}
98 public void CloseRequestStreamAfterReadingResponse ()
100 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
101 string url = "http://" + ep.ToString () + "/test/";
103 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
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 ();
124 //[Category("InetAccess")]
125 [Category ("NotWorking")] // Disabled until a server that meets requirements is found
126 public void Cookies1 ()
128 // The purpose of this test is to ensure that the cookies we get from a request
129 // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer
130 // in HttpWebRequest.
131 // If this URL stops sending *one* and only one cookie, replace it.
132 string url = "http://xamarin.com";
133 CookieContainer cookies = new CookieContainer ();
134 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
135 req.KeepAlive = false;
136 req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2";
137 req.CookieContainer = cookies;
138 Assert.AreEqual (0, cookies.Count, "#01");
139 using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) {
140 CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url));
141 Assert.AreEqual (1, coll.Count, "#02");
142 Assert.AreEqual (1, res.Cookies.Count, "#03");
143 Cookie one = coll [0];
144 Cookie two = res.Cookies [0];
145 Assert.AreEqual (true, object.ReferenceEquals (one, two), "#04");
151 [Ignore ("Fails on MS.NET")]
152 public void SslClientBlock ()
154 // This tests that the write request/initread/write body sequence does not hang
156 // If there's a regression for this, the test will hang.
157 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
159 SslHttpServer server = new SslHttpServer ();
162 string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port);
163 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
164 request.Method = "POST";
165 Stream stream = request.GetRequestStream ();
166 byte [] bytes = new byte [100];
167 stream.Write (bytes, 0, bytes.Length);
169 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
170 Assert.AreEqual (200, (int) resp.StatusCode, "StatusCode");
171 StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
176 if (server.Error != null)
179 ServicePointManager.CertificatePolicy = null;
184 public void Missing_ContentEncoding ()
186 ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
188 BadChunkedServer server = new BadChunkedServer ();
191 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
192 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
193 request.Method = "GET";
194 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
195 Assert.AreEqual ("", resp.ContentEncoding);
198 if (server.Error != null)
201 ServicePointManager.CertificatePolicy = null;
206 public void BadServer_ChunkedClose ()
208 // The server will send a chunked response without a 'last-chunked' mark
209 // and then shutdown the socket for sending.
210 BadChunkedServer server = new BadChunkedServer ();
212 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
213 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
214 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
217 byte [] bytes = new byte [32];
218 // Using StreamReader+UTF8Encoding here fails on MS runtime
219 Stream stream = resp.GetResponseStream ();
220 int nread = stream.Read (bytes, 0, 32);
221 Assert.AreEqual (16, nread, "#01");
222 x = Encoding.ASCII.GetString (bytes, 0, 16);
228 if (server.Error != null)
231 Assert.AreEqual ("1234567890123456", x);
235 [Ignore ("This test asserts that our code violates RFC 2616")]
236 public void MethodCase ()
238 ListDictionary methods = new ListDictionary ();
239 methods.Add ("post", "POST");
240 methods.Add ("puT", "PUT");
241 methods.Add ("POST", "POST");
242 methods.Add ("whatever", "whatever");
243 methods.Add ("PUT", "PUT");
245 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
246 string url = "http://" + ep.ToString () + "/test/";
248 foreach (DictionaryEntry de in methods) {
249 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
250 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
251 req.Method = (string) de.Key;
253 req.ReadWriteTimeout = 2000;
254 req.KeepAlive = false;
255 Stream rs = req.GetRequestStream ();
257 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
258 StreamReader sr = new StreamReader (resp.GetResponseStream (),
260 string line = sr.ReadLine ();
262 Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
271 public void BeginGetRequestStream_Body_NotAllowed ()
273 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
274 string url = "http://" + ep.ToString () + "/test/";
276 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
277 HttpWebRequest request;
279 request = (HttpWebRequest) WebRequest.Create (url);
280 request.Method = "GET";
283 request.BeginGetRequestStream (null, null);
285 } catch (ProtocolViolationException ex) {
286 // Cannot send a content-body with this
288 Assert.IsNull (ex.InnerException, "#A2");
289 Assert.IsNotNull (ex.Message, "#A3");
292 request = (HttpWebRequest) WebRequest.Create (url);
293 request.Method = "HEAD";
296 request.BeginGetRequestStream (null, null);
298 } catch (ProtocolViolationException ex) {
299 // Cannot send a content-body with this
301 Assert.IsNull (ex.InnerException, "#B2");
302 Assert.IsNotNull (ex.Message, "#B3");
307 [Test] // bug #465613
308 public void BeginGetRequestStream_NoBuffering ()
310 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
311 string url = "http://" + ep.ToString () + "/test/";
313 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
318 req = (HttpWebRequest) WebRequest.Create (url);
320 req.SendChunked = false;
321 req.KeepAlive = false;
322 req.AllowWriteStreamBuffering = false;
324 ar = req.BeginGetRequestStream (null, null);
325 rs = req.EndGetRequestStream (ar);
328 req = (HttpWebRequest) WebRequest.Create (url);
330 req.SendChunked = false;
331 req.KeepAlive = true;
332 req.AllowWriteStreamBuffering = false;
335 req.BeginGetRequestStream (null, null);
337 } catch (ProtocolViolationException ex) {
338 // When performing a write operation with
339 // AllowWriteStreamBuffering set to false,
340 // you must either set ContentLength to a
341 // non-negative number or set SendChunked
343 Assert.IsNull (ex.InnerException, "#A2");
344 Assert.IsNotNull (ex.Message, "#A3");
347 req = (HttpWebRequest) WebRequest.Create (url);
349 req.SendChunked = false;
350 req.KeepAlive = true;
351 req.AllowWriteStreamBuffering = false;
352 req.ContentLength = 0;
354 ar = req.BeginGetRequestStream (null, null);
355 rs = req.EndGetRequestStream (ar);
360 [Test] // bug #508027
361 [Category ("NotWorking")] // #5842
362 public void BeginGetResponse ()
364 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
365 string url = "http://" + ep.ToString () + "/test/";
367 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
370 req = (HttpWebRequest) WebRequest.Create (url);
373 req.SendChunked = false;
374 req.KeepAlive = false;
375 req.AllowWriteStreamBuffering = false;
376 req.BeginGetResponse (null, null);
379 req = (HttpWebRequest) WebRequest.Create (url);
382 req.SendChunked = true;
383 req.KeepAlive = false;
384 req.AllowWriteStreamBuffering = false;
385 req.GetRequestStream ().WriteByte (1);
386 req.BeginGetResponse (null, null);
389 req = (HttpWebRequest) WebRequest.Create (url);
392 req.ContentLength = 5;
393 req.SendChunked = false;
394 req.KeepAlive = false;
395 req.AllowWriteStreamBuffering = false;
396 req.GetRequestStream ().WriteByte (5);
397 req.BeginGetResponse (null, null);
400 req = (HttpWebRequest) WebRequest.Create (url);
403 req.SendChunked = false;
404 req.KeepAlive = true;
405 req.AllowWriteStreamBuffering = false;
407 req.BeginGetResponse (null, null);
410 req = (HttpWebRequest) WebRequest.Create (url);
413 req.SendChunked = false;
414 req.KeepAlive = false;
415 req.AllowWriteStreamBuffering = false;
416 req.ContentLength = 5;
417 req.BeginGetResponse (null, null);
420 req = (HttpWebRequest) WebRequest.Create (url);
423 req.SendChunked = false;
424 req.KeepAlive = true;
425 req.AllowWriteStreamBuffering = false;
426 req.ContentLength = 5;
427 req.BeginGetResponse (null, null);
430 req = (HttpWebRequest) WebRequest.Create (url);
433 req.SendChunked = true;
435 req.BeginGetResponse (null, null);
438 req = (HttpWebRequest) WebRequest.Create (url);
441 req.ContentLength = 5;
443 req.BeginGetResponse (null, null);
446 req = (HttpWebRequest) WebRequest.Create (url);
449 req.ContentLength = 0;
451 req.BeginGetResponse (null, null);
456 [Test] // bug #511851
457 public void BeginGetRequestStream_Request_Aborted ()
459 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
460 string url = "http://" + ep.ToString () + "/test/";
462 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
463 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
468 req.BeginGetRequestStream (null, null);
470 } catch (WebException ex) {
471 // The request was aborted: The request was canceled
472 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
473 Assert.IsNull (ex.InnerException, "#3");
474 Assert.IsNotNull (ex.Message, "#4");
475 Assert.IsNull (ex.Response, "#5");
476 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
481 [Test] // bug #511851
482 public void BeginGetResponse_Request_Aborted ()
484 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
485 string url = "http://" + ep.ToString () + "/test/";
487 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
488 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
493 req.BeginGetResponse (null, null);
495 } catch (WebException ex) {
496 // The request was aborted: The request was canceled
497 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
498 Assert.IsNull (ex.InnerException, "#3");
499 Assert.IsNotNull (ex.Message, "#4");
500 Assert.IsNull (ex.Response, "#5");
501 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
507 public void EndGetRequestStream_AsyncResult_Null ()
509 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
510 string url = "http://" + ep.ToString () + "/test/";
512 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
513 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
515 req.BeginGetRequestStream (null, null);
518 req.EndGetRequestStream (null);
520 } catch (ArgumentNullException ex) {
521 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
522 Assert.IsNull (ex.InnerException, "#3");
523 Assert.IsNotNull (ex.Message, "#4");
524 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
532 [Category ("NotWorking")] // do not get consistent result on MS
533 public void EndGetRequestStream_Request_Aborted ()
535 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
536 string url = "http://" + ep.ToString () + "/test/";
538 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
539 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
541 IAsyncResult ar = req.BeginGetRequestStream (null, null);
546 req.EndGetRequestStream (ar);
548 } catch (WebException ex) {
549 // The request was aborted: The request was canceled
550 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
551 Assert.IsNull (ex.InnerException, "#3");
552 Assert.IsNotNull (ex.Message, "#4");
553 Assert.IsNull (ex.Response, "#5");
554 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
559 [Test] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=471522
560 [Category ("NotWorking")]
561 public void EndGetResponse_AsyncResult_Invalid ()
563 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
564 string url = "http://" + ep.ToString () + "/test/";
566 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
567 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
570 req.ReadWriteTimeout = 2000;
571 IAsyncResult ar = req.BeginGetRequestStream (null, null);
573 // AsyncResult was not returned from call to BeginGetResponse
575 req.EndGetResponse (ar);
577 } catch (InvalidCastException) {
585 public void EndGetResponse_AsyncResult_Null ()
587 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
588 string url = "http://" + ep.ToString () + "/test/";
590 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
591 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
593 req.ReadWriteTimeout = 2000;
595 IAsyncResult ar = req.BeginGetResponse (null, null);
598 req.EndGetResponse (null);
600 } catch (ArgumentNullException ex) {
601 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
602 Assert.IsNull (ex.InnerException, "#3");
603 Assert.IsNotNull (ex.Message, "#4");
604 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
608 using (HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse (ar)) {
615 [Test] // bug #429200
616 public void GetRequestStream ()
618 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
619 string url = "http://" + ep.ToString () + "/test/";
621 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
622 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
625 req.ReadWriteTimeout = 2000;
627 Stream rs1 = req.GetRequestStream ();
628 Stream rs2 = req.GetRequestStream ();
630 Assert.IsNotNull (rs1, "#1");
631 Assert.AreSame (rs1, rs2, "#2");
637 [Test] // bug #511851
638 public void GetRequestStream_Request_Aborted ()
640 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
641 string url = "http://" + ep.ToString () + "/test/";
643 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
644 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
649 req.GetRequestStream ();
651 } catch (WebException ex) {
652 // The request was aborted: The request was canceled
653 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
654 Assert.IsNull (ex.InnerException, "#3");
655 Assert.IsNotNull (ex.Message, "#4");
656 Assert.IsNull (ex.Response, "#5");
657 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
662 [Test] // bug #510661
663 [Category ("NotWorking")] // #5842
664 public void GetRequestStream_Close_NotAllBytesWritten ()
666 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
667 string url = "http://" + ep.ToString () + "/test/";
669 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
673 req = (HttpWebRequest) WebRequest.Create (url);
675 req.ContentLength = 2;
676 rs = req.GetRequestStream ();
680 } catch (WebException ex) {
681 // The request was aborted: The request was canceled
682 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
683 Assert.IsNotNull (ex.Message, "#A3");
684 Assert.IsNull (ex.Response, "#A4");
685 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#A5");
687 // Cannot close stream until all bytes are written
688 Exception inner = ex.InnerException;
689 Assert.IsNotNull (inner, "#A6");
690 Assert.AreEqual (typeof (IOException), inner.GetType (), "#A7");
691 Assert.IsNull (inner.InnerException, "#A8");
692 Assert.IsNotNull (inner.Message, "#A9");
695 req = (HttpWebRequest) WebRequest.Create (url);
697 req.ContentLength = 2;
698 rs = req.GetRequestStream ();
703 } catch (WebException ex) {
704 // The request was aborted: The request was canceled
705 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
706 Assert.IsNotNull (ex.Message, "#B3");
707 Assert.IsNull (ex.Response, "#B4");
708 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#B5");
710 // Cannot close stream until all bytes are written
711 Exception inner = ex.InnerException;
712 Assert.IsNotNull (inner, "#B6");
713 Assert.AreEqual (typeof (IOException), inner.GetType (), "#B7");
714 Assert.IsNull (inner.InnerException, "#B8");
715 Assert.IsNotNull (inner.Message, "#B9");
718 req = (HttpWebRequest) WebRequest.Create (url);
720 req.ContentLength = 2;
721 rs = req.GetRequestStream ();
728 [Test] // bug #510642
729 [Category ("NotWorking")] // #5842
730 public void GetRequestStream_Write_Overflow ()
732 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
733 string url = "http://" + ep.ToString () + "/test/";
735 // buffered, non-chunked
736 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
741 req = (HttpWebRequest) WebRequest.Create (url);
744 req.ReadWriteTimeout = 2000;
745 req.ContentLength = 2;
747 rs = req.GetRequestStream ();
750 buffer = new byte [] { 0x2a, 0x1d };
752 rs.Write (buffer, 0, buffer.Length);
754 } catch (ProtocolViolationException ex) {
755 // Bytes to be written to the stream exceed
756 // Content-Length bytes size specified
757 Assert.IsNull (ex.InnerException, "#A2");
758 Assert.IsNotNull (ex.Message, "#A3");
763 req = (HttpWebRequest) WebRequest.Create (url);
766 req.ReadWriteTimeout = 2000;
767 req.ContentLength = 2;
769 rs = req.GetRequestStream ();
771 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
773 rs.Write (buffer, 0, buffer.Length);
775 } catch (ProtocolViolationException ex) {
776 // Bytes to be written to the stream exceed
777 // Content-Length bytes size specified
778 Assert.IsNull (ex.InnerException, "#B2");
779 Assert.IsNotNull (ex.Message, "#B3");
786 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
792 req = (HttpWebRequest) WebRequest.Create (url);
794 req.SendChunked = true;
796 req.ReadWriteTimeout = 2000;
797 req.ContentLength = 2;
799 rs = req.GetRequestStream ();
802 buffer = new byte [] { 0x2a, 0x1d };
803 rs.Write (buffer, 0, buffer.Length);
807 req = (HttpWebRequest) WebRequest.Create (url);
809 req.SendChunked = true;
811 req.ReadWriteTimeout = 2000;
812 req.ContentLength = 2;
814 rs = req.GetRequestStream ();
816 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
817 rs.Write (buffer, 0, buffer.Length);
821 // non-buffered, non-chunked
822 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
827 req = (HttpWebRequest) WebRequest.Create (url);
828 req.AllowWriteStreamBuffering = false;
831 req.ReadWriteTimeout = 2000;
832 req.ContentLength = 2;
834 rs = req.GetRequestStream ();
837 buffer = new byte [] { 0x2a, 0x1d };
839 rs.Write (buffer, 0, buffer.Length);
841 } catch (ProtocolViolationException ex) {
842 // Bytes to be written to the stream exceed
843 // Content-Length bytes size specified
844 Assert.IsNull (ex.InnerException, "#C2");
845 Assert.IsNotNull (ex.Message, "#3");
850 req = (HttpWebRequest) WebRequest.Create (url);
851 req.AllowWriteStreamBuffering = false;
854 req.ReadWriteTimeout = 2000;
855 req.ContentLength = 2;
857 rs = req.GetRequestStream ();
859 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
861 rs.Write (buffer, 0, buffer.Length);
863 } catch (ProtocolViolationException ex) {
864 // Bytes to be written to the stream exceed
865 // Content-Length bytes size specified
866 Assert.IsNull (ex.InnerException, "#D2");
867 Assert.IsNotNull (ex.Message, "#D3");
873 // non-buffered, chunked
874 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
879 req = (HttpWebRequest) WebRequest.Create (url);
880 req.AllowWriteStreamBuffering = false;
882 req.SendChunked = true;
884 req.ReadWriteTimeout = 2000;
885 req.ContentLength = 2;
887 rs = req.GetRequestStream ();
890 buffer = new byte [] { 0x2a, 0x1d };
891 rs.Write (buffer, 0, buffer.Length);
894 req = (HttpWebRequest) WebRequest.Create (url);
895 req.AllowWriteStreamBuffering = false;
897 req.SendChunked = true;
899 req.ReadWriteTimeout = 2000;
900 req.ContentLength = 2;
902 rs = req.GetRequestStream ();
904 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
905 rs.Write (buffer, 0, buffer.Length);
911 [Ignore ("This test asserts that our code violates RFC 2616")]
912 public void GetRequestStream_Body_NotAllowed ()
914 string [] methods = new string [] { "GET", "HEAD", "CONNECT",
915 "get", "HeAd", "ConNect" };
917 foreach (string method in methods) {
918 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
919 "http://localhost:8000");
922 req.GetRequestStream ();
923 Assert.Fail ("#1:" + method);
924 } catch (ProtocolViolationException ex) {
925 Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
926 Assert.IsNull (ex.InnerException, "#3:" + method);
927 Assert.IsNotNull (ex.Message, "#4:" + method);
932 [Test] // bug #511851
933 public void GetResponse_Request_Aborted ()
935 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
936 string url = "http://" + ep.ToString () + "/test/";
938 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
939 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
946 } catch (WebException ex) {
947 // The request was aborted: The request was canceled
948 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
949 Assert.IsNull (ex.InnerException, "#3");
950 Assert.IsNotNull (ex.Message, "#4");
951 Assert.IsNull (ex.Response, "#5");
952 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
958 [Ignore ("This does not timeout any more. That's how MS works when reading small responses")]
959 public void ReadTimeout ()
961 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
962 string url = "http://" + localEP.ToString () + "/original/";
964 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
965 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
967 req.AllowAutoRedirect = false;
969 req.ReadWriteTimeout = 2000;
970 req.KeepAlive = false;
971 Stream rs = req.GetRequestStream ();
973 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
975 Stream s = resp.GetResponseStream ();
978 } catch (WebException ex) {
979 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
980 Assert.IsNull (ex.InnerException, "#3");
981 Assert.IsNull (ex.Response, "#4");
982 Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
988 [Test] // bug #324300
989 public void AllowAutoRedirect ()
991 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
992 string url = "http://" + localEP.ToString () + "/original/";
994 // allow autoredirect
995 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
996 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
999 req.ReadWriteTimeout = 2000;
1000 req.KeepAlive = false;
1001 Stream rs = req.GetRequestStream ();
1003 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1004 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1006 string body = sr.ReadToEnd ();
1008 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1009 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1010 localEP.ToString () + "/moved/", "#A2");
1011 Assert.AreEqual ("GET", resp.Method, "#A3");
1012 Assert.AreEqual ("LOOKS OK", body, "#A4");
1016 // do not allow autoredirect
1017 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1018 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1019 req.Method = "POST";
1020 req.AllowAutoRedirect = false;
1022 req.ReadWriteTimeout = 1000;
1023 req.KeepAlive = false;
1024 Stream rs = req.GetRequestStream ();
1026 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1027 Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
1028 Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
1029 Assert.AreEqual ("POST", resp.Method, "#B3");
1035 public void PostAndRedirect_NoCL ()
1037 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1038 string url = "http://" + localEP.ToString () + "/original/";
1040 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1041 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1042 req.Method = "POST";
1044 req.ReadWriteTimeout = 2000;
1045 Stream rs = req.GetRequestStream ();
1048 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1049 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1051 string body = sr.ReadToEnd ();
1053 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1054 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1055 localEP.ToString () + "/moved/", "#A2");
1056 Assert.AreEqual ("GET", resp.Method, "#A3");
1057 Assert.AreEqual ("LOOKS OK", body, "#A4");
1063 public void PostAndRedirect_CL ()
1065 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1066 string url = "http://" + localEP.ToString () + "/original/";
1068 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1069 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1070 req.Method = "POST";
1072 req.ReadWriteTimeout = 2000;
1073 req.ContentLength = 1;
1074 Stream rs = req.GetRequestStream ();
1076 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1077 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1079 string body = sr.ReadToEnd ();
1081 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1082 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1083 localEP.ToString () + "/moved/", "#A2");
1084 Assert.AreEqual ("GET", resp.Method, "#A3");
1085 Assert.AreEqual ("LOOKS OK", body, "#A4");
1091 public void PostAnd401 ()
1093 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1094 string url = "http://" + localEP.ToString () + "/original/";
1096 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1097 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1098 req.Method = "POST";
1100 req.ReadWriteTimeout = 2000;
1101 req.ContentLength = 1;
1102 Stream rs = req.GetRequestStream ();
1104 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1105 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1107 string body = sr.ReadToEnd ();
1109 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1110 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1111 localEP.ToString () + "/moved/", "#A2");
1112 Assert.AreEqual ("GET", resp.Method, "#A3");
1113 Assert.AreEqual ("LOOKS OK", body, "#A4");
1118 [Test] // bug #324347
1119 [Category ("NotWorking")]
1120 public void InternalServerError ()
1122 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1123 string url = "http://" + localEP.ToString () + "/original/";
1126 using (SocketResponder responder = new SocketResponder (localEP, s => InternalErrorHandler (s))) {
1127 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1128 req.Method = "POST";
1130 req.ReadWriteTimeout = 2000;
1131 req.KeepAlive = false;
1132 Stream rs = req.GetRequestStream ();
1137 Assert.Fail ("#A1");
1138 } catch (WebException ex) {
1139 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1140 Assert.IsNull (ex.InnerException, "#A3");
1141 Assert.IsNotNull (ex.Message, "#A4");
1142 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
1144 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1145 Assert.IsNotNull (webResponse, "#A6");
1146 Assert.AreEqual ("POST", webResponse.Method, "#A7");
1147 webResponse.Close ();
1152 using (SocketResponder responder = new SocketResponder (localEP, s => InternalErrorHandler (s))) {
1153 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1156 req.ReadWriteTimeout = 2000;
1157 req.KeepAlive = false;
1161 Assert.Fail ("#B1");
1162 } catch (WebException ex) {
1163 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1164 Assert.IsNull (ex.InnerException, "#B3");
1165 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1167 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1168 Assert.IsNotNull (webResponse, "#B5");
1169 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1170 webResponse.Close ();
1176 [Category ("NotWorking")] // #B3 fails; we get a SocketException: An existing connection was forcibly closed by the remote host
1177 public void NoContentLength ()
1179 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1180 string url = "http://" + localEP.ToString () + "/original/";
1183 using (SocketResponder responder = new SocketResponder (localEP, s => NoContentLengthHandler (s))) {
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 // The underlying connection was closed:
1197 // An unexpected error occurred on a
1199 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1200 Assert.IsNotNull (ex.InnerException, "#A3");
1201 Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
1202 Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
1204 // Unable to read data from the transport connection:
1205 // A connection attempt failed because the connected party
1206 // did not properly respond after a period of time, or
1207 // established connection failed because connected host has
1208 // failed to respond
1209 IOException ioe = (IOException) ex.InnerException;
1210 Assert.IsNotNull (ioe.InnerException, "#A6");
1211 Assert.IsNotNull (ioe.Message, "#A7");
1212 Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
1214 // An existing connection was forcibly
1215 // closed by the remote host
1216 SocketException soe = (SocketException) ioe.InnerException;
1217 Assert.IsNull (soe.InnerException, "#A9");
1218 Assert.IsNotNull (soe.Message, "#A10");
1220 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1221 Assert.IsNull (webResponse, "#A11");
1226 using (SocketResponder responder = new SocketResponder (localEP, s => NoContentLengthHandler (s))) {
1227 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1230 req.ReadWriteTimeout = 2000;
1231 req.KeepAlive = false;
1235 Assert.Fail ("#B1");
1236 } catch (WebException ex) {
1237 // The remote server returned an error:
1238 // (500) Internal Server Error
1239 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1240 Assert.IsNull (ex.InnerException, "#B3");
1241 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1243 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1244 Assert.IsNotNull (webResponse, "#B5");
1245 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1246 webResponse.Close ();
1251 [Test] // bug #513087
1252 public void NonStandardVerb ()
1254 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
1255 string url = "http://" + ep.ToString () + "/moved/";
1257 using (SocketResponder responder = new SocketResponder (ep, s => VerbEchoHandler (s))) {
1258 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1259 req.Method = "WhatEver";
1260 req.KeepAlive = false;
1261 req.Timeout = 20000;
1262 req.ReadWriteTimeout = 20000;
1264 Stream rs = req.GetRequestStream ();
1267 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1268 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1270 string body = sr.ReadToEnd ();
1272 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#1");
1273 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1274 ep.ToString () + "/moved/", "#2");
1275 Assert.AreEqual ("WhatEver", resp.Method, "#3");
1276 Assert.AreEqual ("WhatEver", body, "#4");
1282 [Category ("NotWorking")] // Assert #2 fails
1283 public void NotModifiedSince ()
1285 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
1286 string url = "http://" + ep.ToString () + "/test/";
1288 using (SocketResponder responder = new SocketResponder (ep, s => NotModifiedSinceHandler (s))) {
1289 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1291 req.KeepAlive = false;
1292 req.Timeout = 20000;
1293 req.ReadWriteTimeout = 20000;
1294 req.Headers.Add (HttpRequestHeader.IfNoneMatch, "898bbr2347056cc2e096afc66e104653");
1295 req.IfModifiedSince = new DateTime (2010, 01, 04);
1297 DateTime start = DateTime.Now;
1298 HttpWebResponse response = null;
1303 } catch (WebException e) {
1304 response = (HttpWebResponse) e.Response;
1307 Assert.IsNotNull (response, "#2");
1308 using (Stream stream = response.GetResponseStream ()) {
1309 byte [] buffer = new byte [4096];
1310 int bytesRead = stream.Read (buffer, 0, buffer.Length);
1311 Assert.AreEqual (0, bytesRead, "#3");
1314 TimeSpan elapsed = DateTime.Now - start;
1315 Assert.IsTrue (elapsed.TotalMilliseconds < 2000, "#4");
1319 [Test] // bug #353495
1320 [Category ("NotWorking")]
1321 public void LastModifiedKind ()
1323 const string reqURL = "http://coffeefaq.com/site/node/25";
1324 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
1325 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
1326 DateTime lastMod = resp.LastModified;
1327 //string rawLastMod = resp.Headers ["Last-Modified"];
1329 //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
1330 Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
1331 req = (HttpWebRequest) WebRequest.Create (reqURL);
1332 req.IfModifiedSince = lastMod;
1334 resp = (HttpWebResponse) req.GetResponse ();
1336 Assert.Fail ("Should result in 304");
1337 } catch (WebException ex) {
1338 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
1339 Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
1344 #region Timeout_Bug // https://bugzilla.novell.com/show_bug.cgi?id=317553
1346 class TimeoutTestHelper {
1349 internal DateTime? Start { get; private set; }
1350 internal DateTime? End { get; private set; }
1351 internal Exception Exception { get; private set; }
1352 internal string Body { get; private set; }
1353 internal int TimeOutInMilliSeconds { get; private set; }
1355 internal TimeoutTestHelper (string url, int timeoutInMilliseconds)
1358 TimeOutInMilliSeconds = timeoutInMilliseconds;
1361 internal void LaunchWebRequest ()
1363 var req = (HttpWebRequest) WebRequest.Create (url_to_test);
1364 req.Timeout = TimeOutInMilliSeconds;
1366 Start = DateTime.Now;
1368 using (var resp = (HttpWebResponse) req.GetResponse ())
1370 var sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
1371 Body = sr.ReadToEnd ();
1373 } catch (Exception e) {
1380 void TestTimeOut (string url, WebExceptionStatus expectedExceptionStatus)
1382 var timeoutWorker = new TimeoutTestHelper (url, three_seconds_in_milliseconds);
1383 var threadStart = new ThreadStart (timeoutWorker.LaunchWebRequest);
1384 var thread = new Thread (threadStart);
1386 Thread.Sleep (three_seconds_in_milliseconds * 3);
1388 if (timeoutWorker.End == null) {
1389 #if MONO_FEATURE_THREAD_ABORT
1392 thread.Interrupt ();
1394 Assert.Fail ("Thread finished after triple the timeout specified has passed");
1397 if (!String.IsNullOrEmpty (timeoutWorker.Body)) {
1398 if (timeoutWorker.Body == response_of_timeout_handler) {
1399 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve proper body");
1401 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve an incorrect body: " + timeoutWorker.Body);
1404 Assert.IsNotNull (timeoutWorker.Exception, "Exception was not thrown");
1406 var webEx = timeoutWorker.Exception as WebException;
1407 Assert.IsNotNull (webEx, "Exception thrown should be WebException, but was: " +
1408 timeoutWorker.Exception.GetType ().FullName);
1410 Assert.AreEqual (expectedExceptionStatus, webEx.Status,
1411 "WebException was thrown, but with a wrong status (should be " + expectedExceptionStatus + "): " + webEx.Status);
1413 Assert.IsFalse (timeoutWorker.End > (timeoutWorker.Start + TimeSpan.FromMilliseconds (three_seconds_in_milliseconds + 500)),
1414 "Timeout exception should have been thrown shortly after timeout is reached, however it was at least half-second late");
1417 [Test] // 1st possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1418 public void TestTimeoutPropertyWithServerThatExistsAndRespondsButTooLate ()
1420 var ep = NetworkHelpers.LocalEphemeralEndPoint ();
1421 string url = "http://" + ep + "/foobar/";
1423 using (var responder = new SocketResponder (ep, TimeOutHandler))
1425 TestTimeOut (url, WebExceptionStatus.Timeout);
1429 [Test] // 2nd possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1430 public void TestTimeoutWithEndpointThatDoesntExistThrowsConnectFailureBeforeTimeout ()
1432 string url = "http://127.0.0.1:8271/"; // some endpoint that is unlikely to exist
1434 // connecting to a non-existing endpoint should throw a ConnectFailure before the timeout is reached
1435 TestTimeOut (url, WebExceptionStatus.ConnectFailure);
1438 const string response_of_timeout_handler = "RESPONSE_OF_TIMEOUT_HANDLER";
1439 const int three_seconds_in_milliseconds = 3000;
1441 private static byte[] TimeOutHandler (Socket socket)
1443 socket.Receive (new byte[4096]);
1445 Thread.Sleep (three_seconds_in_milliseconds * 2);
1447 var sw = new StringWriter ();
1448 sw.WriteLine ("HTTP/1.1 200 OK");
1449 sw.WriteLine ("Content-Type: text/plain");
1450 sw.WriteLine ("Content-Length: " + response_of_timeout_handler.Length);
1452 sw.Write (response_of_timeout_handler);
1455 return Encoding.UTF8.GetBytes (sw.ToString ());
1460 internal static byte [] EchoRequestHandler (Socket socket)
1462 MemoryStream ms = new MemoryStream ();
1463 byte [] buffer = new byte [4096];
1464 int bytesReceived = socket.Receive (buffer);
1465 while (bytesReceived > 0) {
1466 ms.Write (buffer, 0, bytesReceived);
1467 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1468 // after sending the headers
1470 if (socket.Available > 0) {
1471 bytesReceived = socket.Receive (buffer);
1478 StreamReader sr = new StreamReader (ms, Encoding.UTF8);
1479 string request = sr.ReadToEnd ();
1481 StringWriter sw = new StringWriter ();
1482 sw.WriteLine ("HTTP/1.1 200 OK");
1483 sw.WriteLine ("Content-Type: text/xml");
1484 sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
1489 return Encoding.UTF8.GetBytes (sw.ToString ());
1492 static byte [] RedirectRequestHandler (Socket socket)
1494 MemoryStream ms = new MemoryStream ();
1495 byte [] buffer = new byte [4096];
1496 int bytesReceived = socket.Receive (buffer);
1497 while (bytesReceived > 0) {
1498 ms.Write (buffer, 0, bytesReceived);
1499 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1500 // after sending the headers
1502 if (socket.Available > 0) {
1503 bytesReceived = socket.Receive (buffer);
1510 string statusLine = null;
1511 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1512 statusLine = sr.ReadLine ();
1515 StringWriter sw = new StringWriter ();
1516 if (statusLine.StartsWith ("POST /original/")) {
1517 sw.WriteLine ("HTTP/1.0 302 Found");
1518 EndPoint ep = socket.LocalEndPoint;
1519 sw.WriteLine ("Location: " + "http://" + ep.ToString () + "/moved/");
1522 } else if (statusLine.StartsWith ("GET /moved/")) {
1523 sw.WriteLine ("HTTP/1.0 200 OK");
1524 sw.WriteLine ("Content-Type: text/plain");
1525 sw.WriteLine ("Content-Length: 8");
1527 sw.Write ("LOOKS OK");
1530 sw.WriteLine ("HTTP/1.0 500 Too Lazy");
1535 return Encoding.UTF8.GetBytes (sw.ToString ());
1538 static byte [] InternalErrorHandler (Socket socket)
1540 byte [] buffer = new byte [4096];
1541 int bytesReceived = socket.Receive (buffer);
1542 while (bytesReceived > 0) {
1543 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1544 // after sending the headers
1546 if (socket.Available > 0) {
1547 bytesReceived = socket.Receive (buffer);
1552 StringWriter sw = new StringWriter ();
1553 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1554 sw.WriteLine ("Content-Length: 0");
1558 return Encoding.UTF8.GetBytes (sw.ToString ());
1561 static byte [] NoContentLengthHandler (Socket socket)
1563 StringWriter sw = new StringWriter ();
1564 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1568 return Encoding.UTF8.GetBytes (sw.ToString ());
1571 static byte [] NotModifiedSinceHandler (Socket socket)
1573 StringWriter sw = new StringWriter ();
1574 sw.WriteLine ("HTTP/1.1 304 Not Modified");
1575 sw.WriteLine ("Date: Fri, 06 Feb 2009 12:50:26 GMT");
1576 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");
1577 sw.WriteLine ("Not-Modified-Since: Sun, 08 Feb 2009 08:49:26 GMT");
1578 sw.WriteLine ("ETag: 898bbr2347056cc2e096afc66e104653");
1579 sw.WriteLine ("Connection: close");
1583 return Encoding.UTF8.GetBytes (sw.ToString ());
1586 static byte [] VerbEchoHandler (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 string verb = "DEFAULT";
1610 if (statusLine != null) {
1611 string [] parts = statusLine.Split (' ');
1612 if (parts.Length > 0)
1616 StringWriter sw = new StringWriter ();
1617 sw.WriteLine ("HTTP/1.1 200 OK");
1618 sw.WriteLine ("Content-Type: text/plain");
1619 sw.WriteLine ("Content-Length: " + verb.Length);
1624 return Encoding.UTF8.GetBytes (sw.ToString ());
1627 static byte [] PostAnd401Handler (Socket socket)
1629 MemoryStream ms = new MemoryStream ();
1630 byte [] buffer = new byte [4096];
1631 int bytesReceived = socket.Receive (buffer);
1632 while (bytesReceived > 0) {
1633 ms.Write (buffer, 0, bytesReceived);
1634 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1635 // after sending the headers
1637 if (socket.Available > 0) {
1638 bytesReceived = socket.Receive (buffer);
1645 string statusLine = null;
1646 bool have_auth = false;
1648 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1650 while ((l = sr.ReadLine ()) != null) {
1651 if (statusLine == null) {
1653 } else if (l.StartsWith ("Authorization:")) {
1655 } else if (l.StartsWith ("Content-Length:")) {
1656 cl = Int32.Parse (l.Substring ("content-length: ".Length));
1661 StringWriter sw = new StringWriter ();
1663 sw.WriteLine ("HTTP/1.0 401 Invalid Credentials");
1664 sw.WriteLine ("WWW-Authenticate: basic Yeah");
1667 } else if (cl > 0 && statusLine.StartsWith ("POST ")) {
1668 sw.WriteLine ("HTTP/1.0 200 OK");
1669 sw.WriteLine ("Content-Type: text/plain");
1670 sw.WriteLine ("Content-Length: 8");
1672 sw.Write ("LOOKS OK");
1675 sw.WriteLine ("HTTP/1.0 500 test failed");
1676 sw.WriteLine ("Content-Length: 0");
1681 return Encoding.UTF8.GetBytes (sw.ToString ());
1684 public void NtlmAuthentication ()
1686 NtlmServer server = new NtlmServer ();
1689 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
1690 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
1691 request.Timeout = 5000;
1692 request.Credentials = new NetworkCredential ("user", "password", "domain");
1693 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
1695 using (StreamReader reader = new StreamReader (resp.GetResponseStream ())) {
1696 res = reader.ReadToEnd ();
1700 Assert.AreEqual ("OK", res);
1703 class NtlmServer : HttpServer {
1704 public string Where = "";
1705 protected override void Run ()
1707 Where = "before accept";
1708 Socket client = sock.Accept ();
1709 NetworkStream ns = new NetworkStream (client, false);
1710 StreamReader reader = new StreamReader (ns, Encoding.ASCII);
1712 Where = "first read";
1713 while ((line = reader.ReadLine ()) != null) {
1714 if (line.Trim () == String.Empty) {
1718 Where = "first write";
1719 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1720 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1721 "WWW-Authenticate: ignore\r\n" +
1722 "WWW-Authenticate: NTLM\r\n" +
1723 "WWW-Authenticate: ignore,K\r\n" +
1724 "Content-Length: 5\r\n\r\nWRONG");
1727 Where = "second read";
1728 while ((line = reader.ReadLine ()) != null) {
1729 if (line.Trim () == String.Empty) {
1733 Where = "second write";
1734 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1735 "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAADgAAAABggAC8GDhqIONH3sAAAAAAAAAAAAAAAA4AAAABQLODgAAAA8=\r\n" +
1736 "Content-Length: 5\r\n\r\nWRONG");
1739 Where = "third read";
1740 while ((line = reader.ReadLine ()) != null) {
1741 if (line.Trim () == String.Empty) {
1745 Where = "third write";
1746 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1747 "Keep-Alive: true\r\n" +
1748 "Content-Length: 2\r\n\r\nOK");
1750 Thread.Sleep (1000);
1757 class BadChunkedServer : HttpServer {
1758 protected override void Run ()
1760 Socket client = sock.Accept ();
1761 NetworkStream ns = new NetworkStream (client, true);
1762 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1763 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1764 "Transfer-Encoding: chunked\r\n" +
1765 "Connection: close\r\n" +
1766 "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
1768 // This body lacks a 'last-chunk' (see RFC 2616)
1769 writer.Write ("10\r\n1234567890123456\r\n");
1771 client.Shutdown (SocketShutdown.Send);
1772 Thread.Sleep (1000);
1777 class AcceptAllPolicy : ICertificatePolicy {
1778 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
1784 abstract class HttpServer
1786 protected Socket sock;
1787 protected Exception error;
1788 protected ManualResetEvent evt;
1790 public HttpServer ()
1792 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
1793 sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
1797 public void Start ()
1799 evt = new ManualResetEvent (false);
1800 Thread th = new Thread (new ThreadStart (Run));
1810 public IPAddress IPAddress {
1811 get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
1815 get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
1818 public Exception Error {
1819 get { return error; }
1822 protected abstract void Run ();
1826 public void BeginGetRequestStream ()
1832 r.ContentLength = 0;
1833 r.BeginGetRequestStream ((a) =>
1835 using (Stream s = r.EndGetRequestStream (a)) { };
1844 public void BeginGetRequestStreamNoClose ()
1849 r.ContentLength = 1;
1850 r.BeginGetRequestStream ((a) =>
1852 r.EndGetRequestStream (a);
1861 public void BeginGetRequestStreamCancelIfNotAllBytesWritten ()
1867 r.ContentLength = 10;
1868 r.BeginGetRequestStream ((a) =>
1870 WebException ex = ExceptionAssert.Throws<WebException> (() =>
1872 using (Stream s = r.EndGetRequestStream (a)) {
1876 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1885 public void GetRequestStream2 ()
1891 r.ContentLength = data64KB.Length;
1892 using (Stream s = r.GetRequestStream ()) {
1893 s.Write (data64KB, 0, data64KB.Length);
1901 public void GetRequestStreamNotAllBytesWritten ()
1907 r.ContentLength = data64KB.Length;
1908 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ().Close ());
1909 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1916 public void GetRequestStreamTimeout ()
1922 r.ContentLength = data64KB.Length;
1924 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ());
1925 Assert.IsTrue (ex.Status == WebExceptionStatus.Timeout || ex.Status == WebExceptionStatus.ConnectFailure);
1931 public void BeginWrite ()
1933 byte[] received = new byte[data64KB.Length];
1939 r.ContentLength = data64KB.Length;
1941 Stream s = r.GetRequestStream ();
1942 s.BeginWrite (data64KB, 0, data64KB.Length,
1947 r.GetResponse ().Close ();
1954 c.Request.InputStream.ReadAll (received, 0, received.Length);
1955 c.Response.StatusCode = 204;
1956 c.Response.Close ();
1959 Assert.AreEqual (data64KB, received);
1963 public void BeginWriteAfterAbort ()
1965 byte [] received = new byte [data64KB.Length];
1971 r.ContentLength = data64KB.Length;
1973 Stream s = r.GetRequestStream ();
1976 WebException ex = ExceptionAssert.Throws<WebException> (() => s.BeginWrite (data64KB, 0, data64KB.Length, null, null));
1977 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1983 //c.Request.InputStream.ReadAll (received, 0, received.Length);
1984 //c.Response.StatusCode = 204;
1985 //c.Response.Close();
1990 public void PrematureStreamCloseAborts ()
1992 byte [] received = new byte [data64KB.Length];
1998 r.ContentLength = data64KB.Length * 2;
2000 Stream s = r.GetRequestStream ();
2001 s.Write (data64KB, 0, data64KB.Length);
2003 WebException ex = ExceptionAssert.Throws<WebException>(() => s.Close());
2004 Assert.AreEqual(ex.Status, WebExceptionStatus.RequestCanceled);
2010 c.Request.InputStream.ReadAll (received, 0, received.Length);
2011 // c.Response.StatusCode = 204;
2012 // c.Response.Close ();
2017 public void Write ()
2019 byte [] received = new byte [data64KB.Length];
2025 r.ContentLength = data64KB.Length;
2027 using (Stream s = r.GetRequestStream ()) {
2028 s.Write (data64KB, 0, data64KB.Length);
2031 r.GetResponse ().Close ();
2036 c.Request.InputStream.ReadAll (received, 0, received.Length);
2037 c.Response.StatusCode = 204;
2038 c.Response.Close ();
2041 Assert.AreEqual(data64KB, received);
2045 Invalid test: it does not work on linux.
2046 [pid 30973] send(9, "POST / HTTP/1.1\r\nContent-Length:"..., 89, 0) = 89
2048 [pid 30970] send(16, "HTTP/1.1 200 OK\r\nServer: Mono-HT"..., 133, 0) = 133
2050 [pid 30970] close(16) = 0
2052 [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
2054 [pid 30966] close(4) = 0
2057 The server sideis closed (FD 16) and the send on the client side (FD 9) succeeds.
2059 [Category("NotWorking")]
2060 public void WriteServerAborts ()
2062 ManualResetEvent abort = new ManualResetEvent (false);
2063 byte [] received = new byte [data64KB.Length];
2069 r.ContentLength = data64KB.Length;
2071 using (Stream s = r.GetRequestStream()) {
2074 IOException ex = ExceptionAssert.Throws<IOException> (() => s.Write(data64KB, 0, data64KB.Length));
2090 byte [] received = new byte [data64KB.Length];
2095 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2096 using (Stream s = x.GetResponseStream()) {
2097 s.ReadAll (received, 0, received.Length);
2104 c.Response.StatusCode = 200;
2105 c.Response.ContentLength64 = data64KB.Length;
2106 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2107 c.Response.OutputStream.Close ();
2108 c.Response.Close ();
2111 Assert.AreEqual (data64KB, received);
2115 public void ReadTimeout2 ()
2117 byte [] received = new byte [data64KB.Length];
2122 r.ReadWriteTimeout = 10;
2123 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2124 using (Stream s = x.GetResponseStream ()) {
2125 WebException ex = ExceptionAssert.Throws<WebException> (() => s.ReadAll (received, 0, received.Length));
2126 Assert.AreEqual (ex.Status, WebExceptionStatus.Timeout);
2133 c.Response.StatusCode = 200;
2134 c.Response.ContentLength64 = data64KB.Length;
2135 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length / 2);
2136 Thread.Sleep (1000);
2137 // c.Response.OutputStream.Write (data64KB, data64KB.Length / 2, data64KB.Length / 2);
2138 c.Response.OutputStream.Close ();
2139 c.Response.Close ();
2144 public void ReadServerAborted ()
2146 byte [] received = new byte [data64KB.Length];
2151 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2152 using (Stream s = x.GetResponseStream ()) {
2153 Assert.AreEqual (1, s.ReadAll (received, 0, received.Length));
2160 c.Response.StatusCode = 200;
2161 c.Response.ContentLength64 = data64KB.Length;
2162 c.Response.OutputStream.Write (data64KB, 0, 1);
2163 c.Response.Abort ();
2168 public void BeginGetResponse2 ()
2170 byte [] received = new byte [data64KB.Length];
2175 r.BeginGetResponse ((a) =>
2177 using (HttpWebResponse x = (HttpWebResponse) r.EndGetResponse (a))
2178 using (Stream s = x.GetResponseStream ()) {
2179 s.ReadAll (received, 0, received.Length);
2187 c.Response.StatusCode = 200;
2188 c.Response.ContentLength64 = data64KB.Length;
2189 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2190 c.Response.OutputStream.Close ();
2191 c.Response.Close ();
2194 Assert.AreEqual (data64KB, received);
2198 public void BeginGetResponseAborts ()
2200 ManualResetEvent aborted = new ManualResetEvent(false);
2205 r.BeginGetResponse((a) =>
2207 WebException ex = ExceptionAssert.Throws<WebException> (() => r.EndGetResponse (a));
2208 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2218 // Thread.Sleep (100);
2219 // c.Response.StatusCode = 200;
2220 // c.Response.ContentLength64 = 0;
2221 // c.Response.Close ();
2228 public void TestLargeDataReading ()
2230 int near2GBStartPosition = rand.Next (int.MaxValue - 500, int.MaxValue);
2231 AutoResetEvent readyGetLastPortionEvent = new AutoResetEvent (false);
2232 Exception testException = null;
2235 (request, waitHandle) =>
2239 const int timeoutMs = 5000;
2241 request.Timeout = timeoutMs;
2242 request.ReadWriteTimeout = timeoutMs;
2244 WebResponse webResponse = request.GetResponse ();
2245 Stream webResponseStream = webResponse.GetResponseStream ();
2246 Assert.IsNotNull (webResponseStream, null, "#1");
2248 Type webConnectionStreamType = webResponseStream.GetType ();
2249 FieldInfo totalReadField = webConnectionStreamType.GetField ("totalRead", BindingFlags.NonPublic | BindingFlags.Instance);
2250 Assert.IsNotNull (totalReadField, "#2");
2251 totalReadField.SetValue (webResponseStream, near2GBStartPosition);
2253 byte[] readBuffer = new byte[int.MaxValue - near2GBStartPosition];
2254 Assert.AreEqual (webResponseStream.Read (readBuffer, 0, readBuffer.Length), readBuffer.Length, "#3");
2255 readyGetLastPortionEvent.Set ();
2256 Assert.IsTrue (webResponseStream.Read (readBuffer, 0, readBuffer.Length) > 0);
2257 readyGetLastPortionEvent.Set ();
2259 webResponse.Close();
2272 processor.Request.InputStream.Close ();
2274 HttpListenerResponse response = processor.Response;
2275 response.SendChunked = true;
2277 Stream outputStream = response.OutputStream;
2278 var writeBuffer = new byte[int.MaxValue - near2GBStartPosition];
2279 outputStream.Write (writeBuffer, 0, writeBuffer.Length);
2280 readyGetLastPortionEvent.WaitOne ();
2281 outputStream.Write (writeBuffer, 0, writeBuffer.Length);
2282 readyGetLastPortionEvent.WaitOne ();
2287 if (testException != null)
2288 throw testException;
2291 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request)
2293 int port = NetworkHelpers.FindFreePort ();
2295 ManualResetEvent completed = new ManualResetEvent (false);
2296 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2297 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2299 request (client, completed);
2301 if (!completed.WaitOne (10000))
2302 Assert.Fail ("Test hung");
2305 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request, Action<HttpListenerContext> processor)
2307 int port = NetworkHelpers.FindFreePort ();
2309 ManualResetEvent [] completed = new ManualResetEvent [2];
2310 completed [0] = new ManualResetEvent (false);
2311 completed [1] = new ManualResetEvent (false);
2313 using (ListenerScope scope = new ListenerScope (processor, port, completed [0])) {
2314 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2315 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2317 ThreadPool.QueueUserWorkItem ((o) => request (client, completed [1]));
2319 if (!WaitHandle.WaitAll (completed, 10000))
2320 Assert.Fail ("Test hung.");
2325 [ExpectedException (typeof (ArgumentNullException))]
2326 public void NullHost ()
2328 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2333 public void NoHost ()
2335 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2336 Assert.AreEqual (req.Host, "go-mono.com");
2340 [ExpectedException (typeof (ArgumentException))]
2341 public void EmptyHost ()
2343 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2348 public void HostAndPort ()
2350 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:80");
2351 Assert.AreEqual ("go-mono.com", req.Host, "#01");
2352 req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:9000");
2353 Assert.AreEqual ("go-mono.com:9000", req.Host, "#02");
2357 public void PortRange ()
2359 for (int i = 0; i < 65536; i++) {
2362 string s = i.ToString ();
2363 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:" + s);
2364 Assert.AreEqual ("go-mono.com:" + s, req.Host, "#" + s);
2369 [ExpectedException (typeof (ArgumentException))]
2370 public void PortBelow ()
2372 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2373 req.Host = "go-mono.com:-1";
2377 [ExpectedException (typeof (ArgumentException))]
2378 public void PortAbove ()
2380 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2381 req.Host = "go-mono.com:65536";
2385 [ExpectedException (typeof (ArgumentException))]
2386 public void HostTooLong ()
2388 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2389 string s = new string ('a', 100);
2390 req.Host = s + "." + s + "." + s + "." + s + "." + s + "." + s; // Over 255 bytes
2394 [Category ("NotWorking")] // #5490
2395 public void InvalidNamesThatWork ()
2397 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2401 req.Host = new string ('a', 64); // Should fail. Max. is 63.
2405 public void NoDate ()
2407 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2408 Assert.AreEqual (DateTime.MinValue, req.Date);
2412 public void UtcDate ()
2414 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2415 req.Date = DateTime.UtcNow;
2416 DateTime date = req.Date;
2417 Assert.AreEqual (DateTimeKind.Local, date.Kind);
2421 public void AddAndRemoveDate ()
2423 // Neil Armstrong set his foot on Moon
2424 var landing = new DateTime (1969, 7, 21, 2, 56, 0, DateTimeKind.Utc);
2425 Assert.AreEqual (621214377600000000, landing.Ticks);
2426 var unspecified = new DateTime (1969, 7, 21, 2, 56, 0);
2427 var local = landing.ToLocalTime ();
2429 var req = (HttpWebRequest)WebRequest.Create ("http://www.mono-project.com/");
2431 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2432 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2433 Assert.AreEqual (local, req.Date);
2435 req.Date = unspecified;
2436 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2437 Assert.AreEqual (unspecified.Ticks, req.Date.Ticks);
2438 Assert.AreEqual (unspecified, req.Date);
2441 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2442 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2443 Assert.AreEqual (local, req.Date);
2445 req.Date = DateTime.MinValue;
2446 Assert.AreEqual (DateTimeKind.Unspecified, DateTime.MinValue.Kind);
2447 Assert.AreEqual (DateTimeKind.Unspecified, req.Date.Kind);
2448 Assert.AreEqual (0, req.Date.Ticks);
2450 Assert.AreEqual (null, req.Headers.Get ("Date"));
2455 public void TestIPv6Host ()
2457 var address = "2001:0000:0000:0001:0001:0001:0157:0000";
2458 var address2 = '[' + address + ']';
2459 var uri = new Uri (string.Format ("http://{0}/test.css", address2));
2460 var hwr = (HttpWebRequest)WebRequest.Create (uri);
2462 hwr.Host = address2;
2463 Assert.AreEqual (address2, hwr.Host, "#1");
2468 [Category ("NotWorking")]
2469 public void TestIPv6Host2 ()
2471 var address = "2001:0000:0000:0001:0001:0001:0157:0000";
2472 var address2 = '[' + address + ']';
2473 var uri = new Uri (string.Format ("http://{0}/test.css", address2));
2474 var hwr = (HttpWebRequest)WebRequest.Create (uri);
2479 } catch (ArgumentException) {
2486 public void AllowReadStreamBuffering ()
2488 var hr = WebRequest.CreateHttp ("http://www.google.com");
2489 Assert.IsFalse (hr.AllowReadStreamBuffering, "#1");
2491 hr.AllowReadStreamBuffering = true;
2493 } catch (InvalidOperationException) {
2498 class ListenerScope : IDisposable {
2499 EventWaitHandle completed;
2500 public HttpListener listener;
2501 Action<HttpListenerContext> processor;
2503 public ListenerScope (Action<HttpListenerContext> processor, int port, EventWaitHandle completed)
2505 this.processor = processor;
2506 this.completed = completed;
2508 this.listener = new HttpListener ();
2509 this.listener.Prefixes.Add (string.Format ("http://localhost:{0}/", port));
2510 this.listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
2511 this.listener.Start ();
2513 this.listener.BeginGetContext (this.RequestHandler, null);
2516 void RequestHandler (IAsyncResult result)
2518 HttpListenerContext context = null;
2521 context = this.listener.EndGetContext (result);
2522 } catch (HttpListenerException ex) {
2523 // check if the thread has been aborted as in the case when we are shutting down.
2524 if (ex.ErrorCode == 995)
2526 } catch (ObjectDisposedException) {
2530 ThreadPool.QueueUserWorkItem ((o) =>
2533 this.processor (context);
2534 } catch (HttpListenerException) {
2538 this.completed.Set ();
2541 public void Dispose ()
2543 this.listener.Stop ();
2548 class SslHttpServer : HttpServer {
2549 X509Certificate _certificate;
2551 protected override void Run ()
2554 Socket client = sock.Accept ();
2555 NetworkStream ns = new NetworkStream (client, true);
2556 SslServerStream s = new SslServerStream (ns, Certificate, false, false);
2557 s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
2559 StreamReader reader = new StreamReader (s);
2560 StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
2563 string hello = "<html><body><h1>Hello World!</h1></body></html>";
2564 string answer = "HTTP/1.0 200\r\n" +
2565 "Connection: close\r\n" +
2566 "Content-Type: text/html\r\n" +
2567 "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
2568 "Content-Length: " + hello.Length + "\r\n" +
2573 line = reader.ReadLine ();
2574 } while (line != "" && line != null && line.Length > 0);
2576 // Now the content. We know it's 100 bytes.
2577 // This makes BeginRead in sslclientstream block.
2578 char [] cs = new char [100];
2579 reader.Read (cs, 0, 100);
2581 writer.Write (answer);
2583 if (evt.WaitOne (5000, false))
2584 error = new Exception ("Timeout when stopping the server");
2585 } catch (Exception e) {
2590 X509Certificate Certificate {
2592 if (_certificate == null)
2593 _certificate = new X509Certificate (CertData.Certificate);
2595 return _certificate;
2599 AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
2601 PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
2607 public readonly static byte [] Certificate = {
2608 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
2609 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
2610 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
2611 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
2612 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
2613 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
2614 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
2615 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
2616 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
2617 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
2618 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
2619 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
2620 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
2621 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
2622 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
2623 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
2624 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
2625 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
2626 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
2627 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
2628 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
2629 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
2630 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
2631 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
2632 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
2633 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
2634 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
2635 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
2639 public readonly static byte [] PrivateKey = {
2640 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
2641 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
2642 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
2643 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
2644 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
2645 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
2646 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
2647 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
2648 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
2649 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
2650 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
2651 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
2652 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
2653 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
2654 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
2655 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
2656 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
2657 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
2658 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
2659 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
2660 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
2661 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
2662 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
2663 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
2664 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
2665 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
2666 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
2667 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
2668 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
2669 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
2670 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
2671 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
2672 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
2673 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
2674 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
2675 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
2676 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
2677 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
2678 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
2684 public void CookieContainerTest ()
2686 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2687 string url = "http://" + ep.ToString ();
2689 using (SocketResponder responder = new SocketResponder (ep, s => CookieRequestHandler (s))) {
2690 CookieContainer container = new CookieContainer ();
2691 container.Add(new Uri (url), new Cookie ("foo", "bar"));
2692 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2693 request.CookieContainer = container;
2694 WebHeaderCollection headers = request.Headers;
2695 headers.Add("Cookie", "foo=baz");
2696 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2697 string responseString = null;
2698 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2699 responseString = reader.ReadToEnd ();
2702 Assert.AreEqual (1, response.Cookies.Count, "#01");
2703 Assert.AreEqual ("foo=bar", response.Headers.Get("Set-Cookie"), "#02");
2706 using (SocketResponder responder = new SocketResponder (ep, s => CookieRequestHandler (s))) {
2707 CookieContainer container = new CookieContainer ();
2708 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2709 request.CookieContainer = container;
2710 WebHeaderCollection headers = request.Headers;
2711 headers.Add("Cookie", "foo=baz");
2712 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2713 string responseString = null;
2714 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2715 responseString = reader.ReadToEnd ();
2718 Assert.AreEqual (0, response.Cookies.Count, "#03");
2719 Assert.AreEqual ("", response.Headers.Get("Set-Cookie"), "#04");
2723 internal static byte[] CookieRequestHandler (Socket socket)
2725 MemoryStream ms = new MemoryStream ();
2726 byte[] buffer = new byte[4096];
2727 int bytesReceived = socket.Receive (buffer);
2728 while (bytesReceived > 0) {
2729 ms.Write(buffer, 0, bytesReceived);
2730 // We don't check for Content-Length or anything else here, so we give the client a little time to write
2731 // after sending the headers
2733 if (socket.Available > 0) {
2734 bytesReceived = socket.Receive (buffer);
2741 string cookies = string.Empty;
2742 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
2744 while ((line = sr.ReadLine ()) != null) {
2745 if (line.StartsWith ("Cookie:")) {
2746 cookies = line.Substring ("cookie: ".Length);
2751 StringWriter sw = new StringWriter ();
2752 sw.WriteLine ("HTTP/1.1 200 OK");
2753 sw.WriteLine ("Content-Type: text/xml");
2754 sw.WriteLine ("Set-Cookie: " + cookies);
2755 sw.WriteLine ("Content-Length: " + cookies.Length.ToString (CultureInfo.InvariantCulture));
2760 return Encoding.UTF8.GetBytes (sw.ToString ());
2765 public class HttpRequestStreamTest
2768 public void BeginRead ()
2770 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2771 string url = "http://" + ep.ToString () + "/test/";
2773 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2774 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2775 req.Method = "POST";
2777 using (Stream rs = req.GetRequestStream ()) {
2778 byte [] buffer = new byte [10];
2780 rs.BeginRead (buffer, 0, buffer.Length, null, null);
2782 } catch (NotSupportedException ex) {
2783 // The stream does not support reading
2784 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2785 Assert.IsNull (ex.InnerException, "#3");
2786 Assert.IsNotNull (ex.Message, "#4");
2795 [Category("MobileNotWorking")]
2796 public void BeginWrite_Request_Aborted ()
2798 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2799 string url = "http://" + ep.ToString () + "/test/";
2801 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2802 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2803 req.Method = "POST";
2805 using (Stream rs = req.GetRequestStream ()) {
2808 rs.BeginWrite (new byte [] { 0x2a, 0x2f }, 0, 2, null, null);
2810 } catch (WebException ex) {
2811 // The request was aborted: The request was canceled
2812 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
2813 Assert.IsNull (ex.InnerException, "#3");
2814 Assert.IsNotNull (ex.Message, "#4");
2815 Assert.IsNull (ex.Response, "#5");
2816 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
2823 public void CanRead ()
2825 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2826 string url = "http://" + ep.ToString () + "/test/";
2828 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2829 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2830 req.Method = "POST";
2832 Stream rs = req.GetRequestStream ();
2834 Assert.IsFalse (rs.CanRead, "#1");
2836 Assert.IsFalse (rs.CanRead, "#2");
2845 public void CanSeek ()
2847 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2848 string url = "http://" + ep.ToString () + "/test/";
2850 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2851 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2852 req.Method = "POST";
2854 Stream rs = req.GetRequestStream ();
2856 Assert.IsFalse (rs.CanSeek, "#1");
2858 Assert.IsFalse (rs.CanSeek, "#2");
2866 [Test] // bug #324182
2867 public void CanTimeout ()
2869 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2870 string url = "http://" + ep.ToString () + "/test/";
2872 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2873 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2874 req.Method = "POST";
2876 Stream rs = req.GetRequestStream ();
2878 Assert.IsTrue (rs.CanTimeout, "#1");
2880 Assert.IsTrue (rs.CanTimeout, "#2");
2889 public void CanWrite ()
2891 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2892 string url = "http://" + ep.ToString () + "/test/";
2894 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2895 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2896 req.Method = "POST";
2898 Stream rs = req.GetRequestStream ();
2900 Assert.IsTrue (rs.CanWrite, "#1");
2902 Assert.IsFalse (rs.CanWrite, "#2");
2913 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2914 string url = "http://" + ep.ToString () + "/test/";
2916 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2917 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2918 req.Method = "POST";
2920 using (Stream rs = req.GetRequestStream ()) {
2921 byte [] buffer = new byte [10];
2923 rs.Read (buffer, 0, buffer.Length);
2925 } catch (NotSupportedException ex) {
2926 // The stream does not support reading
2927 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2928 Assert.IsNull (ex.InnerException, "#3");
2929 Assert.IsNotNull (ex.Message, "#4");
2938 public void ReadByte ()
2940 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2941 string url = "http://" + ep.ToString () + "/test/";
2943 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2944 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2945 req.Method = "POST";
2947 using (Stream rs = req.GetRequestStream ()) {
2951 } catch (NotSupportedException ex) {
2952 // The stream does not support reading
2953 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
2954 Assert.IsNull (ex.InnerException, "#3");
2955 Assert.IsNotNull (ex.Message, "#4");
2964 public void ReadTimeout ()
2966 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2967 string url = "http://" + ep.ToString () + "/test/";
2969 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2970 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2971 req.Method = "POST";
2973 Stream rs = req.GetRequestStream ();
2975 Assert.AreEqual (300000, rs.ReadTimeout, "#1");
2977 Assert.AreEqual (300000, rs.ReadTimeout, "#2");
2988 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2989 string url = "http://" + ep.ToString () + "/test/";
2991 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
2992 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
2993 req.Method = "POST";
2995 using (Stream rs = req.GetRequestStream ()) {
2997 rs.Seek (0, SeekOrigin.Current);
2999 } catch (NotSupportedException ex) {
3000 // This stream does not support seek operations
3001 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
3002 Assert.IsNull (ex.InnerException, "#3");
3003 Assert.IsNotNull (ex.Message, "#4");
3012 public void Write_Buffer_Null ()
3014 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3015 string url = "http://" + ep.ToString () + "/test/";
3017 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3018 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3019 req.Method = "POST";
3021 using (Stream rs = req.GetRequestStream ()) {
3023 rs.Write ((byte []) null, -1, -1);
3025 } catch (ArgumentNullException ex) {
3026 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3027 Assert.IsNull (ex.InnerException, "#3");
3028 Assert.IsNotNull (ex.Message, "#4");
3029 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3038 public void Write_Count_Negative ()
3040 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3041 string url = "http://" + ep.ToString () + "/test/";
3043 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3044 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3045 req.Method = "POST";
3047 using (Stream rs = req.GetRequestStream ()) {
3048 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3050 rs.Write (buffer, 1, -1);
3052 } catch (ArgumentOutOfRangeException ex) {
3053 // Specified argument was out of the range of valid values
3054 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3055 Assert.IsNull (ex.InnerException, "#A3");
3056 Assert.IsNotNull (ex.Message, "#A4");
3057 Assert.AreEqual ("size", ex.ParamName, "#A5");
3066 public void Write_Count_Overflow ()
3068 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3069 string url = "http://" + ep.ToString () + "/test/";
3071 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3072 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3073 req.Method = "POST";
3075 using (Stream rs = req.GetRequestStream ()) {
3076 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3078 rs.Write (buffer, buffer.Length - 2, 3);
3080 } catch (ArgumentOutOfRangeException ex) {
3081 // Specified argument was out of the range of valid values
3082 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3083 Assert.IsNull (ex.InnerException, "#3");
3084 Assert.IsNotNull (ex.Message, "#4");
3085 Assert.AreEqual ("size", ex.ParamName, "#5");
3094 public void Write_Offset_Negative ()
3096 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3097 string url = "http://" + ep.ToString () + "/test/";
3099 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3100 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3101 req.Method = "POST";
3103 using (Stream rs = req.GetRequestStream ()) {
3104 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3106 rs.Write (buffer, -1, 0);
3108 } catch (ArgumentOutOfRangeException ex) {
3109 // Specified argument was out of the range of valid values
3110 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3111 Assert.IsNull (ex.InnerException, "#3");
3112 Assert.IsNotNull (ex.Message, "#4");
3113 Assert.AreEqual ("offset", ex.ParamName, "#5");
3122 public void Write_Offset_Overflow ()
3124 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3125 string url = "http://" + ep.ToString () + "/test/";
3127 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3128 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3129 req.Method = "POST";
3131 using (Stream rs = req.GetRequestStream ()) {
3132 byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d, 0x00, 0x0f };
3134 rs.Write (buffer, buffer.Length + 1, 0);
3136 } catch (ArgumentOutOfRangeException ex) {
3137 // Specified argument was out of the range of valid values
3138 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
3139 Assert.IsNull (ex.InnerException, "#3");
3140 Assert.IsNotNull (ex.Message, "#4");
3141 Assert.AreEqual ("offset", ex.ParamName, "#5");
3150 public void Write_Request_Aborted ()
3152 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3153 string url = "http://" + ep.ToString () + "/test/";
3155 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3156 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3157 req.Method = "POST";
3159 using (Stream rs = req.GetRequestStream ()) {
3162 rs.Write (new byte [0], 0, 0);
3164 } catch (WebException ex) {
3165 // The request was aborted: The request was canceled
3166 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3167 Assert.IsNull (ex.InnerException, "#3");
3168 Assert.IsNotNull (ex.Message, "#4");
3169 Assert.IsNull (ex.Response, "#5");
3170 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
3177 [Category ("NotWorking")]
3178 public void Write_Stream_Closed ()
3180 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3181 string url = "http://" + ep.ToString () + "/test/";
3183 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3184 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3185 req.Method = "POST";
3187 using (Stream rs = req.GetRequestStream ()) {
3190 rs.Write (new byte [0], 0, 0);
3192 } catch (WebException ex) {
3193 // The request was aborted: The connection was closed unexpectedly
3194 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3195 Assert.IsNull (ex.InnerException, "#3");
3196 Assert.IsNotNull (ex.Message, "#4");
3197 Assert.IsNull (ex.Response, "#5");
3198 Assert.AreEqual (WebExceptionStatus.ConnectionClosed, ex.Status, "#6");
3205 public void WriteByte_Request_Aborted ()
3207 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3208 string url = "http://" + ep.ToString () + "/test/";
3210 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3211 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3212 req.Method = "POST";
3214 using (Stream rs = req.GetRequestStream ()) {
3217 rs.WriteByte (0x2a);
3219 } catch (WebException ex) {
3220 // The request was aborted: The request was canceled
3221 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
3222 Assert.IsNull (ex.InnerException, "#3");
3223 Assert.IsNotNull (ex.Message, "#4");
3224 Assert.IsNull (ex.Response, "#5");
3225 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
3232 public void WriteTimeout ()
3234 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
3235 string url = "http://" + ep.ToString () + "/test/";
3237 using (SocketResponder responder = new SocketResponder (ep, s => HttpWebRequestTest.EchoRequestHandler (s))) {
3238 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
3239 req.Method = "POST";
3241 Stream rs = req.GetRequestStream ();
3243 Assert.AreEqual (300000, rs.WriteTimeout, "#1");
3245 Assert.AreEqual (300000, rs.WriteTimeout, "#2");
3255 // This test is supposed to fail prior to .NET 4.0
3256 public void Post_EmptyRequestStream ()
3258 var wr = HttpWebRequest.Create ("http://google.com");
3260 wr.GetRequestStream ();
3262 var gr = wr.BeginGetResponse (delegate { }, null);
3263 Assert.AreEqual (true, gr.AsyncWaitHandle.WaitOne (5000), "#1");
3267 static class StreamExtensions {
3268 public static int ReadAll(this Stream stream, byte[] buffer, int offset, int count)
3272 while (totalRead < count) {
3273 int bytesRead = stream.Read (buffer, offset + totalRead, count - totalRead);
3277 totalRead += bytesRead;
3284 static class ExceptionAssert {
3286 /// Asserts that the function throws an exception.
3288 /// <param name="f">A function execute that is expected to raise an exception.</param>
3289 /// <typeparam name="T">The type of exception that is expected.</typeparam>
3290 /// <returns>The exception thrown.</returns>
3291 /// <exception cref="AssertFailedException">If the function does not throw an exception
3292 /// or throws a different exception.</exception>
3293 /// <example><![CDATA[
3294 /// ExceptionAssert.Throws(typeof(ArgumentNullException), delegate {
3295 /// myObject.myFunction(null); });
3297 public static T Throws<T> (Action f) where T : Exception {
3298 Exception actualException = null;
3302 } catch (Exception ex) {
3303 actualException = ex;
3306 if (actualException == null)
3307 throw new AssertionException (string.Format (
3308 "No exception thrown. Expected '{0}'",
3309 typeof (T).FullName));
3310 else if (typeof(T) != actualException.GetType())
3311 throw new AssertionException (string.Format (
3312 "Caught exception of type '{0}'. Expected '{1}':{2}",
3313 actualException.GetType().FullName,
3314 typeof (T).FullName,
3315 Environment.NewLine + actualException));
3317 return (T) actualException;