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 [Category ("RequiresBSDSockets")]
40 public class HttpWebRequestTest
42 private Random rand = new Random ();
43 private byte [] data64KB = new byte [64 * 1024];
48 ServicePointManager.Expect100Continue = false;
49 rand.NextBytes (data64KB);
53 public void Proxy_Null ()
55 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
56 Assert.IsNotNull (req.Proxy, "#1");
58 Assert.IsNull (req.Proxy, "#2");
62 [Category("InetAccess")]
65 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
66 Assert.IsNotNull (req.IfModifiedSince, "req:If Modified Since: ");
68 req.UserAgent = "MonoClient v1.0";
69 Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
70 Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
72 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
73 Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
74 Assert.AreEqual ("OK", res.StatusDescription, "#B2");
76 Assert.IsTrue (res.Headers.Get ("Content-Type").StartsWith ("text/html; charset=", StringComparison.OrdinalIgnoreCase), "#C1");
77 Assert.IsNotNull (res.LastModified, "#C2");
78 Assert.AreEqual (0, res.Cookies.Count, "#C3");
84 public void AddRange ()
86 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
88 req.AddRange (50, 90);
89 req.AddRange ("bytes", 100);
90 req.AddRange ("bytes", 100, 120);
91 Assert.AreEqual ("bytes=10-,50-90,100-,100-120", req.Headers ["Range"], "#1");
93 req.AddRange ("bits", 2000);
95 } catch (InvalidOperationException) {}
99 public void CloseRequestStreamAfterReadingResponse ()
101 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
102 string url = "http://" + ep.ToString () + "/test/";
104 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
105 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
108 req.ReadWriteTimeout = 2000;
110 byte [] data = new byte [128];
111 req.ContentLength = data.Length;
113 Stream rs = req.GetRequestStream ();
114 rs.Write (data, 0, data.Length);
117 HttpWebResponse response = (HttpWebResponse) req.GetResponse ();
125 //[Category("InetAccess")]
126 [Category ("NotWorking")] // Disabled until a server that meets requirements is found
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://xamarin.com";
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 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
251 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
252 req.Method = (string) de.Key;
254 req.ReadWriteTimeout = 2000;
255 req.KeepAlive = false;
256 Stream rs = req.GetRequestStream ();
258 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
259 StreamReader sr = new StreamReader (resp.GetResponseStream (),
261 string line = sr.ReadLine ();
263 Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
272 public void BeginGetRequestStream_Body_NotAllowed ()
274 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
275 string url = "http://" + ep.ToString () + "/test/";
277 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
278 HttpWebRequest request;
280 request = (HttpWebRequest) WebRequest.Create (url);
281 request.Method = "GET";
284 request.BeginGetRequestStream (null, null);
286 } catch (ProtocolViolationException ex) {
287 // Cannot send a content-body with this
289 Assert.IsNull (ex.InnerException, "#A2");
290 Assert.IsNotNull (ex.Message, "#A3");
293 request = (HttpWebRequest) WebRequest.Create (url);
294 request.Method = "HEAD";
297 request.BeginGetRequestStream (null, null);
299 } catch (ProtocolViolationException ex) {
300 // Cannot send a content-body with this
302 Assert.IsNull (ex.InnerException, "#B2");
303 Assert.IsNotNull (ex.Message, "#B3");
308 [Test] // bug #465613
309 public void BeginGetRequestStream_NoBuffering ()
311 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
312 string url = "http://" + ep.ToString () + "/test/";
314 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
319 req = (HttpWebRequest) WebRequest.Create (url);
321 req.SendChunked = false;
322 req.KeepAlive = false;
323 req.AllowWriteStreamBuffering = false;
325 ar = req.BeginGetRequestStream (null, null);
326 rs = req.EndGetRequestStream (ar);
329 req = (HttpWebRequest) WebRequest.Create (url);
331 req.SendChunked = false;
332 req.KeepAlive = true;
333 req.AllowWriteStreamBuffering = false;
336 req.BeginGetRequestStream (null, null);
338 } catch (ProtocolViolationException ex) {
339 // When performing a write operation with
340 // AllowWriteStreamBuffering set to false,
341 // you must either set ContentLength to a
342 // non-negative number or set SendChunked
344 Assert.IsNull (ex.InnerException, "#A2");
345 Assert.IsNotNull (ex.Message, "#A3");
348 req = (HttpWebRequest) WebRequest.Create (url);
350 req.SendChunked = false;
351 req.KeepAlive = true;
352 req.AllowWriteStreamBuffering = false;
353 req.ContentLength = 0;
355 ar = req.BeginGetRequestStream (null, null);
356 rs = req.EndGetRequestStream (ar);
361 [Test] // bug #508027
362 [Category ("NotWorking")] // #5842
363 public void BeginGetResponse ()
365 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
366 string url = "http://" + ep.ToString () + "/test/";
368 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
371 req = (HttpWebRequest) WebRequest.Create (url);
374 req.SendChunked = false;
375 req.KeepAlive = false;
376 req.AllowWriteStreamBuffering = false;
377 req.BeginGetResponse (null, null);
380 req = (HttpWebRequest) WebRequest.Create (url);
383 req.SendChunked = true;
384 req.KeepAlive = false;
385 req.AllowWriteStreamBuffering = false;
386 req.GetRequestStream ().WriteByte (1);
387 req.BeginGetResponse (null, null);
390 req = (HttpWebRequest) WebRequest.Create (url);
393 req.ContentLength = 5;
394 req.SendChunked = false;
395 req.KeepAlive = false;
396 req.AllowWriteStreamBuffering = false;
397 req.GetRequestStream ().WriteByte (5);
398 req.BeginGetResponse (null, null);
401 req = (HttpWebRequest) WebRequest.Create (url);
404 req.SendChunked = false;
405 req.KeepAlive = true;
406 req.AllowWriteStreamBuffering = false;
408 req.BeginGetResponse (null, null);
411 req = (HttpWebRequest) WebRequest.Create (url);
414 req.SendChunked = false;
415 req.KeepAlive = false;
416 req.AllowWriteStreamBuffering = false;
417 req.ContentLength = 5;
418 req.BeginGetResponse (null, null);
421 req = (HttpWebRequest) WebRequest.Create (url);
424 req.SendChunked = false;
425 req.KeepAlive = true;
426 req.AllowWriteStreamBuffering = false;
427 req.ContentLength = 5;
428 req.BeginGetResponse (null, null);
431 req = (HttpWebRequest) WebRequest.Create (url);
434 req.SendChunked = true;
436 req.BeginGetResponse (null, null);
439 req = (HttpWebRequest) WebRequest.Create (url);
442 req.ContentLength = 5;
444 req.BeginGetResponse (null, null);
447 req = (HttpWebRequest) WebRequest.Create (url);
450 req.ContentLength = 0;
452 req.BeginGetResponse (null, null);
457 [Test] // bug #511851
458 public void BeginGetRequestStream_Request_Aborted ()
460 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
461 string url = "http://" + ep.ToString () + "/test/";
463 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
464 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
469 req.BeginGetRequestStream (null, null);
471 } catch (WebException ex) {
472 // The request was aborted: The request was canceled
473 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
474 Assert.IsNull (ex.InnerException, "#3");
475 Assert.IsNotNull (ex.Message, "#4");
476 Assert.IsNull (ex.Response, "#5");
477 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
482 [Test] // bug #511851
483 public void BeginGetResponse_Request_Aborted ()
485 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
486 string url = "http://" + ep.ToString () + "/test/";
488 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
489 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
494 req.BeginGetResponse (null, null);
496 } catch (WebException ex) {
497 // The request was aborted: The request was canceled
498 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
499 Assert.IsNull (ex.InnerException, "#3");
500 Assert.IsNotNull (ex.Message, "#4");
501 Assert.IsNull (ex.Response, "#5");
502 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
508 public void EndGetRequestStream_AsyncResult_Null ()
510 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
511 string url = "http://" + ep.ToString () + "/test/";
513 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
514 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
516 req.BeginGetRequestStream (null, null);
519 req.EndGetRequestStream (null);
521 } catch (ArgumentNullException ex) {
522 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
523 Assert.IsNull (ex.InnerException, "#3");
524 Assert.IsNotNull (ex.Message, "#4");
525 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
533 [Category ("NotWorking")] // do not get consistent result on MS
534 public void EndGetRequestStream_Request_Aborted ()
536 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
537 string url = "http://" + ep.ToString () + "/test/";
539 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
540 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
542 IAsyncResult ar = req.BeginGetRequestStream (null, null);
547 req.EndGetRequestStream (ar);
549 } catch (WebException ex) {
550 // The request was aborted: The request was canceled
551 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
552 Assert.IsNull (ex.InnerException, "#3");
553 Assert.IsNotNull (ex.Message, "#4");
554 Assert.IsNull (ex.Response, "#5");
555 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
560 [Test] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=471522
561 [Category ("NotWorking")]
562 public void EndGetResponse_AsyncResult_Invalid ()
564 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
565 string url = "http://" + ep.ToString () + "/test/";
567 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
568 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
571 req.ReadWriteTimeout = 2000;
572 IAsyncResult ar = req.BeginGetRequestStream (null, null);
574 // AsyncResult was not returned from call to BeginGetResponse
576 req.EndGetResponse (ar);
578 } catch (InvalidCastException) {
586 public void EndGetResponse_AsyncResult_Null ()
588 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
589 string url = "http://" + ep.ToString () + "/test/";
591 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
592 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
594 req.ReadWriteTimeout = 2000;
596 IAsyncResult ar = req.BeginGetResponse (null, null);
599 req.EndGetResponse (null);
601 } catch (ArgumentNullException ex) {
602 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
603 Assert.IsNull (ex.InnerException, "#3");
604 Assert.IsNotNull (ex.Message, "#4");
605 Assert.AreEqual ("asyncResult", ex.ParamName, "#5");
609 using (HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse (ar)) {
616 [Test] // bug #429200
617 public void GetRequestStream ()
619 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
620 string url = "http://" + ep.ToString () + "/test/";
622 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
623 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
626 req.ReadWriteTimeout = 2000;
628 Stream rs1 = req.GetRequestStream ();
629 Stream rs2 = req.GetRequestStream ();
631 Assert.IsNotNull (rs1, "#1");
632 Assert.AreSame (rs1, rs2, "#2");
638 [Test] // bug #511851
639 public void GetRequestStream_Request_Aborted ()
641 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
642 string url = "http://" + ep.ToString () + "/test/";
644 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
645 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
650 req.GetRequestStream ();
652 } catch (WebException ex) {
653 // The request was aborted: The request was canceled
654 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
655 Assert.IsNull (ex.InnerException, "#3");
656 Assert.IsNotNull (ex.Message, "#4");
657 Assert.IsNull (ex.Response, "#5");
658 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
663 [Test] // bug #510661
664 [Category ("NotWorking")] // #5842
665 public void GetRequestStream_Close_NotAllBytesWritten ()
667 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
668 string url = "http://" + ep.ToString () + "/test/";
670 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
674 req = (HttpWebRequest) WebRequest.Create (url);
676 req.ContentLength = 2;
677 rs = req.GetRequestStream ();
681 } catch (WebException ex) {
682 // The request was aborted: The request was canceled
683 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
684 Assert.IsNotNull (ex.Message, "#A3");
685 Assert.IsNull (ex.Response, "#A4");
686 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#A5");
688 // Cannot close stream until all bytes are written
689 Exception inner = ex.InnerException;
690 Assert.IsNotNull (inner, "#A6");
691 Assert.AreEqual (typeof (IOException), inner.GetType (), "#A7");
692 Assert.IsNull (inner.InnerException, "#A8");
693 Assert.IsNotNull (inner.Message, "#A9");
696 req = (HttpWebRequest) WebRequest.Create (url);
698 req.ContentLength = 2;
699 rs = req.GetRequestStream ();
704 } catch (WebException ex) {
705 // The request was aborted: The request was canceled
706 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
707 Assert.IsNotNull (ex.Message, "#B3");
708 Assert.IsNull (ex.Response, "#B4");
709 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#B5");
711 // Cannot close stream until all bytes are written
712 Exception inner = ex.InnerException;
713 Assert.IsNotNull (inner, "#B6");
714 Assert.AreEqual (typeof (IOException), inner.GetType (), "#B7");
715 Assert.IsNull (inner.InnerException, "#B8");
716 Assert.IsNotNull (inner.Message, "#B9");
719 req = (HttpWebRequest) WebRequest.Create (url);
721 req.ContentLength = 2;
722 rs = req.GetRequestStream ();
729 [Test] // bug #510642
730 [Category ("NotWorking")] // #5842
731 public void GetRequestStream_Write_Overflow ()
733 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
734 string url = "http://" + ep.ToString () + "/test/";
736 // buffered, non-chunked
737 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
742 req = (HttpWebRequest) WebRequest.Create (url);
745 req.ReadWriteTimeout = 2000;
746 req.ContentLength = 2;
748 rs = req.GetRequestStream ();
751 buffer = new byte [] { 0x2a, 0x1d };
753 rs.Write (buffer, 0, buffer.Length);
755 } catch (ProtocolViolationException ex) {
756 // Bytes to be written to the stream exceed
757 // Content-Length bytes size specified
758 Assert.IsNull (ex.InnerException, "#A2");
759 Assert.IsNotNull (ex.Message, "#A3");
764 req = (HttpWebRequest) WebRequest.Create (url);
767 req.ReadWriteTimeout = 2000;
768 req.ContentLength = 2;
770 rs = req.GetRequestStream ();
772 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
774 rs.Write (buffer, 0, buffer.Length);
776 } catch (ProtocolViolationException ex) {
777 // Bytes to be written to the stream exceed
778 // Content-Length bytes size specified
779 Assert.IsNull (ex.InnerException, "#B2");
780 Assert.IsNotNull (ex.Message, "#B3");
787 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
793 req = (HttpWebRequest) WebRequest.Create (url);
795 req.SendChunked = true;
797 req.ReadWriteTimeout = 2000;
798 req.ContentLength = 2;
800 rs = req.GetRequestStream ();
803 buffer = new byte [] { 0x2a, 0x1d };
804 rs.Write (buffer, 0, buffer.Length);
808 req = (HttpWebRequest) WebRequest.Create (url);
810 req.SendChunked = true;
812 req.ReadWriteTimeout = 2000;
813 req.ContentLength = 2;
815 rs = req.GetRequestStream ();
817 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
818 rs.Write (buffer, 0, buffer.Length);
822 // non-buffered, non-chunked
823 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
828 req = (HttpWebRequest) WebRequest.Create (url);
829 req.AllowWriteStreamBuffering = false;
832 req.ReadWriteTimeout = 2000;
833 req.ContentLength = 2;
835 rs = req.GetRequestStream ();
838 buffer = new byte [] { 0x2a, 0x1d };
840 rs.Write (buffer, 0, buffer.Length);
842 } catch (ProtocolViolationException ex) {
843 // Bytes to be written to the stream exceed
844 // Content-Length bytes size specified
845 Assert.IsNull (ex.InnerException, "#C2");
846 Assert.IsNotNull (ex.Message, "#3");
851 req = (HttpWebRequest) WebRequest.Create (url);
852 req.AllowWriteStreamBuffering = false;
855 req.ReadWriteTimeout = 2000;
856 req.ContentLength = 2;
858 rs = req.GetRequestStream ();
860 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
862 rs.Write (buffer, 0, buffer.Length);
864 } catch (ProtocolViolationException ex) {
865 // Bytes to be written to the stream exceed
866 // Content-Length bytes size specified
867 Assert.IsNull (ex.InnerException, "#D2");
868 Assert.IsNotNull (ex.Message, "#D3");
874 // non-buffered, chunked
875 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
880 req = (HttpWebRequest) WebRequest.Create (url);
881 req.AllowWriteStreamBuffering = false;
883 req.SendChunked = true;
885 req.ReadWriteTimeout = 2000;
886 req.ContentLength = 2;
888 rs = req.GetRequestStream ();
891 buffer = new byte [] { 0x2a, 0x1d };
892 rs.Write (buffer, 0, buffer.Length);
895 req = (HttpWebRequest) WebRequest.Create (url);
896 req.AllowWriteStreamBuffering = false;
898 req.SendChunked = true;
900 req.ReadWriteTimeout = 2000;
901 req.ContentLength = 2;
903 rs = req.GetRequestStream ();
905 buffer = new byte [] { 0x2a, 0x2c, 0x1d };
906 rs.Write (buffer, 0, buffer.Length);
912 [Ignore ("This test asserts that our code violates RFC 2616")]
913 public void GetRequestStream_Body_NotAllowed ()
915 string [] methods = new string [] { "GET", "HEAD", "CONNECT",
916 "get", "HeAd", "ConNect" };
918 foreach (string method in methods) {
919 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
920 "http://localhost:8000");
923 req.GetRequestStream ();
924 Assert.Fail ("#1:" + method);
925 } catch (ProtocolViolationException ex) {
926 Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
927 Assert.IsNull (ex.InnerException, "#3:" + method);
928 Assert.IsNotNull (ex.Message, "#4:" + method);
933 [Test] // bug #511851
934 public void GetResponse_Request_Aborted ()
936 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
937 string url = "http://" + ep.ToString () + "/test/";
939 using (SocketResponder responder = new SocketResponder (ep, s => EchoRequestHandler (s))) {
940 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
947 } catch (WebException ex) {
948 // The request was aborted: The request was canceled
949 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
950 Assert.IsNull (ex.InnerException, "#3");
951 Assert.IsNotNull (ex.Message, "#4");
952 Assert.IsNull (ex.Response, "#5");
953 Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#6");
959 [Ignore ("This does not timeout any more. That's how MS works when reading small responses")]
960 public void ReadTimeout ()
962 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
963 string url = "http://" + localEP.ToString () + "/original/";
965 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
966 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
968 req.AllowAutoRedirect = false;
970 req.ReadWriteTimeout = 2000;
971 req.KeepAlive = false;
972 Stream rs = req.GetRequestStream ();
974 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
976 Stream s = resp.GetResponseStream ();
979 } catch (WebException ex) {
980 Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
981 Assert.IsNull (ex.InnerException, "#3");
982 Assert.IsNull (ex.Response, "#4");
983 Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
989 [Test] // bug #324300
990 public void AllowAutoRedirect ()
992 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
993 string url = "http://" + localEP.ToString () + "/original/";
995 // allow autoredirect
996 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
997 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1000 req.ReadWriteTimeout = 2000;
1001 req.KeepAlive = false;
1002 Stream rs = req.GetRequestStream ();
1004 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1005 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1007 string body = sr.ReadToEnd ();
1009 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1010 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1011 localEP.ToString () + "/moved/", "#A2");
1012 Assert.AreEqual ("GET", resp.Method, "#A3");
1013 Assert.AreEqual ("LOOKS OK", body, "#A4");
1017 // do not allow autoredirect
1018 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1019 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1020 req.Method = "POST";
1021 req.AllowAutoRedirect = false;
1023 req.ReadWriteTimeout = 1000;
1024 req.KeepAlive = false;
1025 Stream rs = req.GetRequestStream ();
1027 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1028 Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
1029 Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
1030 Assert.AreEqual ("POST", resp.Method, "#B3");
1036 public void PostAndRedirect_NoCL ()
1038 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1039 string url = "http://" + localEP.ToString () + "/original/";
1041 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1042 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1043 req.Method = "POST";
1045 req.ReadWriteTimeout = 2000;
1046 Stream rs = req.GetRequestStream ();
1049 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1050 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1052 string body = sr.ReadToEnd ();
1054 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1055 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1056 localEP.ToString () + "/moved/", "#A2");
1057 Assert.AreEqual ("GET", resp.Method, "#A3");
1058 Assert.AreEqual ("LOOKS OK", body, "#A4");
1064 public void PostAndRedirect_CL ()
1066 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1067 string url = "http://" + localEP.ToString () + "/original/";
1069 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1070 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1071 req.Method = "POST";
1073 req.ReadWriteTimeout = 2000;
1074 req.ContentLength = 1;
1075 Stream rs = req.GetRequestStream ();
1077 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1078 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1080 string body = sr.ReadToEnd ();
1082 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1083 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1084 localEP.ToString () + "/moved/", "#A2");
1085 Assert.AreEqual ("GET", resp.Method, "#A3");
1086 Assert.AreEqual ("LOOKS OK", body, "#A4");
1092 public void PostAnd401 ()
1094 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1095 string url = "http://" + localEP.ToString () + "/original/";
1097 using (SocketResponder responder = new SocketResponder (localEP, s => RedirectRequestHandler (s))) {
1098 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1099 req.Method = "POST";
1101 req.ReadWriteTimeout = 2000;
1102 req.ContentLength = 1;
1103 Stream rs = req.GetRequestStream ();
1105 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1106 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1108 string body = sr.ReadToEnd ();
1110 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
1111 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1112 localEP.ToString () + "/moved/", "#A2");
1113 Assert.AreEqual ("GET", resp.Method, "#A3");
1114 Assert.AreEqual ("LOOKS OK", body, "#A4");
1119 [Test] // bug #324347
1120 [Category ("NotWorking")]
1121 public void InternalServerError ()
1123 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1124 string url = "http://" + localEP.ToString () + "/original/";
1127 using (SocketResponder responder = new SocketResponder (localEP, s => InternalErrorHandler (s))) {
1128 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1129 req.Method = "POST";
1131 req.ReadWriteTimeout = 2000;
1132 req.KeepAlive = false;
1133 Stream rs = req.GetRequestStream ();
1138 Assert.Fail ("#A1");
1139 } catch (WebException ex) {
1140 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1141 Assert.IsNull (ex.InnerException, "#A3");
1142 Assert.IsNotNull (ex.Message, "#A4");
1143 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
1145 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1146 Assert.IsNotNull (webResponse, "#A6");
1147 Assert.AreEqual ("POST", webResponse.Method, "#A7");
1148 webResponse.Close ();
1153 using (SocketResponder responder = new SocketResponder (localEP, s => InternalErrorHandler (s))) {
1154 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1157 req.ReadWriteTimeout = 2000;
1158 req.KeepAlive = false;
1162 Assert.Fail ("#B1");
1163 } catch (WebException ex) {
1164 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1165 Assert.IsNull (ex.InnerException, "#B3");
1166 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1168 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1169 Assert.IsNotNull (webResponse, "#B5");
1170 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1171 webResponse.Close ();
1177 [Category ("NotWorking")] // #B3 fails; we get a SocketException: An existing connection was forcibly closed by the remote host
1178 public void NoContentLength ()
1180 IPEndPoint localEP = NetworkHelpers.LocalEphemeralEndPoint ();
1181 string url = "http://" + localEP.ToString () + "/original/";
1184 using (SocketResponder responder = new SocketResponder (localEP, s => NoContentLengthHandler (s))) {
1185 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1186 req.Method = "POST";
1188 req.ReadWriteTimeout = 2000;
1189 req.KeepAlive = false;
1190 Stream rs = req.GetRequestStream ();
1195 Assert.Fail ("#A1");
1196 } catch (WebException ex) {
1197 // The underlying connection was closed:
1198 // An unexpected error occurred on a
1200 Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
1201 Assert.IsNotNull (ex.InnerException, "#A3");
1202 Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
1203 Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
1205 // Unable to read data from the transport connection:
1206 // A connection attempt failed because the connected party
1207 // did not properly respond after a period of time, or
1208 // established connection failed because connected host has
1209 // failed to respond
1210 IOException ioe = (IOException) ex.InnerException;
1211 Assert.IsNotNull (ioe.InnerException, "#A6");
1212 Assert.IsNotNull (ioe.Message, "#A7");
1213 Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
1215 // An existing connection was forcibly
1216 // closed by the remote host
1217 SocketException soe = (SocketException) ioe.InnerException;
1218 Assert.IsNull (soe.InnerException, "#A9");
1219 Assert.IsNotNull (soe.Message, "#A10");
1221 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1222 Assert.IsNull (webResponse, "#A11");
1227 using (SocketResponder responder = new SocketResponder (localEP, s => NoContentLengthHandler (s))) {
1228 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1231 req.ReadWriteTimeout = 2000;
1232 req.KeepAlive = false;
1236 Assert.Fail ("#B1");
1237 } catch (WebException ex) {
1238 // The remote server returned an error:
1239 // (500) Internal Server Error
1240 Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
1241 Assert.IsNull (ex.InnerException, "#B3");
1242 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
1244 HttpWebResponse webResponse = ex.Response as HttpWebResponse;
1245 Assert.IsNotNull (webResponse, "#B5");
1246 Assert.AreEqual ("GET", webResponse.Method, "#B6");
1247 webResponse.Close ();
1252 [Test] // bug #513087
1253 public void NonStandardVerb ()
1255 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
1256 string url = "http://" + ep.ToString () + "/moved/";
1258 using (SocketResponder responder = new SocketResponder (ep, s => VerbEchoHandler (s))) {
1259 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1260 req.Method = "WhatEver";
1261 req.KeepAlive = false;
1262 req.Timeout = 20000;
1263 req.ReadWriteTimeout = 20000;
1265 Stream rs = req.GetRequestStream ();
1268 using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
1269 StreamReader sr = new StreamReader (resp.GetResponseStream (),
1271 string body = sr.ReadToEnd ();
1273 Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#1");
1274 Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
1275 ep.ToString () + "/moved/", "#2");
1276 Assert.AreEqual ("WhatEver", resp.Method, "#3");
1277 Assert.AreEqual ("WhatEver", body, "#4");
1283 [Category ("NotWorking")] // Assert #2 fails
1284 public void NotModifiedSince ()
1286 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
1287 string url = "http://" + ep.ToString () + "/test/";
1289 using (SocketResponder responder = new SocketResponder (ep, s => NotModifiedSinceHandler (s))) {
1290 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
1292 req.KeepAlive = false;
1293 req.Timeout = 20000;
1294 req.ReadWriteTimeout = 20000;
1295 req.Headers.Add (HttpRequestHeader.IfNoneMatch, "898bbr2347056cc2e096afc66e104653");
1296 req.IfModifiedSince = new DateTime (2010, 01, 04);
1298 DateTime start = DateTime.Now;
1299 HttpWebResponse response = null;
1304 } catch (WebException e) {
1305 response = (HttpWebResponse) e.Response;
1308 Assert.IsNotNull (response, "#2");
1309 using (Stream stream = response.GetResponseStream ()) {
1310 byte [] buffer = new byte [4096];
1311 int bytesRead = stream.Read (buffer, 0, buffer.Length);
1312 Assert.AreEqual (0, bytesRead, "#3");
1315 TimeSpan elapsed = DateTime.Now - start;
1316 Assert.IsTrue (elapsed.TotalMilliseconds < 2000, "#4");
1320 [Test] // bug #353495
1321 [Category ("NotWorking")]
1322 public void LastModifiedKind ()
1324 const string reqURL = "http://coffeefaq.com/site/node/25";
1325 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (reqURL);
1326 HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
1327 DateTime lastMod = resp.LastModified;
1328 //string rawLastMod = resp.Headers ["Last-Modified"];
1330 //Assert.AreEqual ("Tue, 15 Jan 2008 08:59:59 GMT", rawLastMod, "#1");
1331 Assert.AreEqual (DateTimeKind.Local, lastMod.Kind, "#2");
1332 req = (HttpWebRequest) WebRequest.Create (reqURL);
1333 req.IfModifiedSince = lastMod;
1335 resp = (HttpWebResponse) req.GetResponse ();
1337 Assert.Fail ("Should result in 304");
1338 } catch (WebException ex) {
1339 Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#3");
1340 Assert.AreEqual (((HttpWebResponse) ex.Response).StatusCode, HttpStatusCode.NotModified, "#4");
1345 #region Timeout_Bug // https://bugzilla.novell.com/show_bug.cgi?id=317553
1347 class TimeoutTestHelper {
1350 internal DateTime? Start { get; private set; }
1351 internal DateTime? End { get; private set; }
1352 internal Exception Exception { get; private set; }
1353 internal string Body { get; private set; }
1354 internal int TimeOutInMilliSeconds { get; private set; }
1356 internal TimeoutTestHelper (string url, int timeoutInMilliseconds)
1359 TimeOutInMilliSeconds = timeoutInMilliseconds;
1362 internal void LaunchWebRequest ()
1364 var req = (HttpWebRequest) WebRequest.Create (url_to_test);
1365 req.Timeout = TimeOutInMilliSeconds;
1367 Start = DateTime.Now;
1369 using (var resp = (HttpWebResponse) req.GetResponse ())
1371 var sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
1372 Body = sr.ReadToEnd ();
1374 } catch (Exception e) {
1381 void TestTimeOut (string url, WebExceptionStatus expectedExceptionStatus)
1383 var timeoutWorker = new TimeoutTestHelper (url, three_seconds_in_milliseconds);
1384 var threadStart = new ThreadStart (timeoutWorker.LaunchWebRequest);
1385 var thread = new Thread (threadStart);
1387 Thread.Sleep (three_seconds_in_milliseconds * 3);
1389 if (timeoutWorker.End == null) {
1390 #if MONO_FEATURE_THREAD_ABORT
1393 thread.Interrupt ();
1395 Assert.Fail ("Thread finished after triple the timeout specified has passed");
1398 if (!String.IsNullOrEmpty (timeoutWorker.Body)) {
1399 if (timeoutWorker.Body == response_of_timeout_handler) {
1400 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve proper body");
1402 Assert.Fail ("Should not be reached, timeout exception was not thrown and webrequest managed to retrieve an incorrect body: " + timeoutWorker.Body);
1405 Assert.IsNotNull (timeoutWorker.Exception, "Exception was not thrown");
1407 var webEx = timeoutWorker.Exception as WebException;
1408 Assert.IsNotNull (webEx, "Exception thrown should be WebException, but was: " +
1409 timeoutWorker.Exception.GetType ().FullName);
1411 Assert.AreEqual (expectedExceptionStatus, webEx.Status,
1412 "WebException was thrown, but with a wrong status (should be " + expectedExceptionStatus + "): " + webEx.Status);
1414 Assert.IsFalse (timeoutWorker.End > (timeoutWorker.Start + TimeSpan.FromMilliseconds (three_seconds_in_milliseconds + 500)),
1415 "Timeout exception should have been thrown shortly after timeout is reached, however it was at least half-second late");
1418 [Test] // 1st possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1419 public void TestTimeoutPropertyWithServerThatExistsAndRespondsButTooLate ()
1421 var ep = NetworkHelpers.LocalEphemeralEndPoint ();
1422 string url = "http://" + ep + "/foobar/";
1424 using (var responder = new SocketResponder (ep, TimeOutHandler))
1426 TestTimeOut (url, WebExceptionStatus.Timeout);
1430 [Test] // 2nd possible case of https://bugzilla.novell.com/show_bug.cgi?id=MONO74177
1431 public void TestTimeoutWithEndpointThatDoesntExistThrowsConnectFailureBeforeTimeout ()
1433 string url = "http://127.0.0.1:8271/"; // some endpoint that is unlikely to exist
1435 // connecting to a non-existing endpoint should throw a ConnectFailure before the timeout is reached
1436 TestTimeOut (url, WebExceptionStatus.ConnectFailure);
1439 const string response_of_timeout_handler = "RESPONSE_OF_TIMEOUT_HANDLER";
1440 const int three_seconds_in_milliseconds = 3000;
1442 private static byte[] TimeOutHandler (Socket socket)
1444 socket.Receive (new byte[4096]);
1446 Thread.Sleep (three_seconds_in_milliseconds * 2);
1448 var sw = new StringWriter ();
1449 sw.WriteLine ("HTTP/1.1 200 OK");
1450 sw.WriteLine ("Content-Type: text/plain");
1451 sw.WriteLine ("Content-Length: " + response_of_timeout_handler.Length);
1453 sw.Write (response_of_timeout_handler);
1456 return Encoding.UTF8.GetBytes (sw.ToString ());
1461 internal static byte [] EchoRequestHandler (Socket socket)
1463 MemoryStream ms = new MemoryStream ();
1464 byte [] buffer = new byte [4096];
1465 int bytesReceived = socket.Receive (buffer);
1466 while (bytesReceived > 0) {
1467 ms.Write (buffer, 0, bytesReceived);
1468 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1469 // after sending the headers
1471 if (socket.Available > 0) {
1472 bytesReceived = socket.Receive (buffer);
1479 StreamReader sr = new StreamReader (ms, Encoding.UTF8);
1480 string request = sr.ReadToEnd ();
1482 StringWriter sw = new StringWriter ();
1483 sw.WriteLine ("HTTP/1.1 200 OK");
1484 sw.WriteLine ("Content-Type: text/xml");
1485 sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
1490 return Encoding.UTF8.GetBytes (sw.ToString ());
1493 static byte [] RedirectRequestHandler (Socket socket)
1495 MemoryStream ms = new MemoryStream ();
1496 byte [] buffer = new byte [4096];
1497 int bytesReceived = socket.Receive (buffer);
1498 while (bytesReceived > 0) {
1499 ms.Write (buffer, 0, bytesReceived);
1500 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1501 // after sending the headers
1503 if (socket.Available > 0) {
1504 bytesReceived = socket.Receive (buffer);
1511 string statusLine = null;
1512 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1513 statusLine = sr.ReadLine ();
1516 StringWriter sw = new StringWriter ();
1517 if (statusLine.StartsWith ("POST /original/")) {
1518 sw.WriteLine ("HTTP/1.0 302 Found");
1519 EndPoint ep = socket.LocalEndPoint;
1520 sw.WriteLine ("Location: " + "http://" + ep.ToString () + "/moved/");
1523 } else if (statusLine.StartsWith ("GET /moved/")) {
1524 sw.WriteLine ("HTTP/1.0 200 OK");
1525 sw.WriteLine ("Content-Type: text/plain");
1526 sw.WriteLine ("Content-Length: 8");
1528 sw.Write ("LOOKS OK");
1531 sw.WriteLine ("HTTP/1.0 500 Too Lazy");
1536 return Encoding.UTF8.GetBytes (sw.ToString ());
1539 static byte [] InternalErrorHandler (Socket socket)
1541 byte [] buffer = new byte [4096];
1542 int bytesReceived = socket.Receive (buffer);
1543 while (bytesReceived > 0) {
1544 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1545 // after sending the headers
1547 if (socket.Available > 0) {
1548 bytesReceived = socket.Receive (buffer);
1553 StringWriter sw = new StringWriter ();
1554 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1555 sw.WriteLine ("Content-Length: 0");
1559 return Encoding.UTF8.GetBytes (sw.ToString ());
1562 static byte [] NoContentLengthHandler (Socket socket)
1564 StringWriter sw = new StringWriter ();
1565 sw.WriteLine ("HTTP/1.1 500 Too Lazy");
1569 return Encoding.UTF8.GetBytes (sw.ToString ());
1572 static byte [] NotModifiedSinceHandler (Socket socket)
1574 StringWriter sw = new StringWriter ();
1575 sw.WriteLine ("HTTP/1.1 304 Not Modified");
1576 sw.WriteLine ("Date: Fri, 06 Feb 2009 12:50:26 GMT");
1577 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");
1578 sw.WriteLine ("Not-Modified-Since: Sun, 08 Feb 2009 08:49:26 GMT");
1579 sw.WriteLine ("ETag: 898bbr2347056cc2e096afc66e104653");
1580 sw.WriteLine ("Connection: close");
1584 return Encoding.UTF8.GetBytes (sw.ToString ());
1587 static byte [] VerbEchoHandler (Socket socket)
1589 MemoryStream ms = new MemoryStream ();
1590 byte [] buffer = new byte [4096];
1591 int bytesReceived = socket.Receive (buffer);
1592 while (bytesReceived > 0) {
1593 ms.Write (buffer, 0, bytesReceived);
1594 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1595 // after sending the headers
1597 if (socket.Available > 0) {
1598 bytesReceived = socket.Receive (buffer);
1605 string statusLine = null;
1606 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1607 statusLine = sr.ReadLine ();
1610 string verb = "DEFAULT";
1611 if (statusLine != null) {
1612 string [] parts = statusLine.Split (' ');
1613 if (parts.Length > 0)
1617 StringWriter sw = new StringWriter ();
1618 sw.WriteLine ("HTTP/1.1 200 OK");
1619 sw.WriteLine ("Content-Type: text/plain");
1620 sw.WriteLine ("Content-Length: " + verb.Length);
1625 return Encoding.UTF8.GetBytes (sw.ToString ());
1628 static byte [] PostAnd401Handler (Socket socket)
1630 MemoryStream ms = new MemoryStream ();
1631 byte [] buffer = new byte [4096];
1632 int bytesReceived = socket.Receive (buffer);
1633 while (bytesReceived > 0) {
1634 ms.Write (buffer, 0, bytesReceived);
1635 // We don't check for Content-Length or anything else here, so we give the client a little time to write
1636 // after sending the headers
1638 if (socket.Available > 0) {
1639 bytesReceived = socket.Receive (buffer);
1646 string statusLine = null;
1647 bool have_auth = false;
1649 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
1651 while ((l = sr.ReadLine ()) != null) {
1652 if (statusLine == null) {
1654 } else if (l.StartsWith ("Authorization:")) {
1656 } else if (l.StartsWith ("Content-Length:")) {
1657 cl = Int32.Parse (l.Substring ("content-length: ".Length));
1662 StringWriter sw = new StringWriter ();
1664 sw.WriteLine ("HTTP/1.0 401 Invalid Credentials");
1665 sw.WriteLine ("WWW-Authenticate: basic Yeah");
1668 } else if (cl > 0 && statusLine.StartsWith ("POST ")) {
1669 sw.WriteLine ("HTTP/1.0 200 OK");
1670 sw.WriteLine ("Content-Type: text/plain");
1671 sw.WriteLine ("Content-Length: 8");
1673 sw.Write ("LOOKS OK");
1676 sw.WriteLine ("HTTP/1.0 500 test failed");
1677 sw.WriteLine ("Content-Length: 0");
1682 return Encoding.UTF8.GetBytes (sw.ToString ());
1685 public void NtlmAuthentication ()
1687 NtlmServer server = new NtlmServer ();
1690 string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
1691 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
1692 request.Timeout = 5000;
1693 request.Credentials = new NetworkCredential ("user", "password", "domain");
1694 HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
1696 using (StreamReader reader = new StreamReader (resp.GetResponseStream ())) {
1697 res = reader.ReadToEnd ();
1701 Assert.AreEqual ("OK", res);
1704 class NtlmServer : HttpServer {
1705 public string Where = "";
1706 protected override void Run ()
1708 Where = "before accept";
1709 Socket client = sock.Accept ();
1710 NetworkStream ns = new NetworkStream (client, false);
1711 StreamReader reader = new StreamReader (ns, Encoding.ASCII);
1713 Where = "first read";
1714 while ((line = reader.ReadLine ()) != null) {
1715 if (line.Trim () == String.Empty) {
1719 Where = "first write";
1720 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1721 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1722 "WWW-Authenticate: ignore\r\n" +
1723 "WWW-Authenticate: NTLM\r\n" +
1724 "WWW-Authenticate: ignore,K\r\n" +
1725 "Content-Length: 5\r\n\r\nWRONG");
1728 Where = "second read";
1729 while ((line = reader.ReadLine ()) != null) {
1730 if (line.Trim () == String.Empty) {
1734 Where = "second write";
1735 writer.Write ( "HTTP/1.1 401 Unauthorized\r\n" +
1736 "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAADgAAAABggAC8GDhqIONH3sAAAAAAAAAAAAAAAA4AAAABQLODgAAAA8=\r\n" +
1737 "Content-Length: 5\r\n\r\nWRONG");
1740 Where = "third read";
1741 while ((line = reader.ReadLine ()) != null) {
1742 if (line.Trim () == String.Empty) {
1746 Where = "third write";
1747 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1748 "Keep-Alive: true\r\n" +
1749 "Content-Length: 2\r\n\r\nOK");
1751 Thread.Sleep (1000);
1758 class BadChunkedServer : HttpServer {
1759 protected override void Run ()
1761 Socket client = sock.Accept ();
1762 NetworkStream ns = new NetworkStream (client, true);
1763 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
1764 writer.Write ( "HTTP/1.1 200 OK\r\n" +
1765 "Transfer-Encoding: chunked\r\n" +
1766 "Connection: close\r\n" +
1767 "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
1769 // This body lacks a 'last-chunk' (see RFC 2616)
1770 writer.Write ("10\r\n1234567890123456\r\n");
1772 client.Shutdown (SocketShutdown.Send);
1773 Thread.Sleep (1000);
1778 class AcceptAllPolicy : ICertificatePolicy {
1779 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
1785 abstract class HttpServer
1787 protected Socket sock;
1788 protected Exception error;
1789 protected ManualResetEvent evt;
1791 public HttpServer ()
1793 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
1794 sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
1798 public void Start ()
1800 evt = new ManualResetEvent (false);
1801 Thread th = new Thread (new ThreadStart (Run));
1811 public IPAddress IPAddress {
1812 get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
1816 get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
1819 public Exception Error {
1820 get { return error; }
1823 protected abstract void Run ();
1827 public void BeginGetRequestStream ()
1833 r.ContentLength = 0;
1834 r.BeginGetRequestStream ((a) =>
1836 using (Stream s = r.EndGetRequestStream (a)) { };
1845 public void BeginGetRequestStreamNoClose ()
1850 r.ContentLength = 1;
1851 r.BeginGetRequestStream ((a) =>
1853 r.EndGetRequestStream (a);
1862 public void BeginGetRequestStreamCancelIfNotAllBytesWritten ()
1868 r.ContentLength = 10;
1869 r.BeginGetRequestStream ((a) =>
1871 WebException ex = ExceptionAssert.Throws<WebException> (() =>
1873 using (Stream s = r.EndGetRequestStream (a)) {
1877 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1886 public void GetRequestStream2 ()
1892 r.ContentLength = data64KB.Length;
1893 using (Stream s = r.GetRequestStream ()) {
1894 s.Write (data64KB, 0, data64KB.Length);
1902 public void GetRequestStreamNotAllBytesWritten ()
1908 r.ContentLength = data64KB.Length;
1909 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ().Close ());
1910 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1917 public void GetRequestStreamTimeout ()
1923 r.ContentLength = data64KB.Length;
1925 WebException ex = ExceptionAssert.Throws<WebException> (() => r.GetRequestStream ());
1926 Assert.IsTrue (ex.Status == WebExceptionStatus.Timeout || ex.Status == WebExceptionStatus.ConnectFailure);
1932 public void BeginWrite ()
1934 byte[] received = new byte[data64KB.Length];
1940 r.ContentLength = data64KB.Length;
1942 Stream s = r.GetRequestStream ();
1943 s.BeginWrite (data64KB, 0, data64KB.Length,
1948 r.GetResponse ().Close ();
1955 c.Request.InputStream.ReadAll (received, 0, received.Length);
1956 c.Response.StatusCode = 204;
1957 c.Response.Close ();
1960 Assert.AreEqual (data64KB, received);
1964 public void BeginWriteAfterAbort ()
1966 byte [] received = new byte [data64KB.Length];
1972 r.ContentLength = data64KB.Length;
1974 Stream s = r.GetRequestStream ();
1977 WebException ex = ExceptionAssert.Throws<WebException> (() => s.BeginWrite (data64KB, 0, data64KB.Length, null, null));
1978 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
1984 //c.Request.InputStream.ReadAll (received, 0, received.Length);
1985 //c.Response.StatusCode = 204;
1986 //c.Response.Close();
1991 public void PrematureStreamCloseAborts ()
1993 byte [] received = new byte [data64KB.Length];
1999 r.ContentLength = data64KB.Length * 2;
2001 Stream s = r.GetRequestStream ();
2002 s.Write (data64KB, 0, data64KB.Length);
2004 WebException ex = ExceptionAssert.Throws<WebException>(() => s.Close());
2005 Assert.AreEqual(ex.Status, WebExceptionStatus.RequestCanceled);
2011 c.Request.InputStream.ReadAll (received, 0, received.Length);
2012 // c.Response.StatusCode = 204;
2013 // c.Response.Close ();
2018 public void Write ()
2020 byte [] received = new byte [data64KB.Length];
2026 r.ContentLength = data64KB.Length;
2028 using (Stream s = r.GetRequestStream ()) {
2029 s.Write (data64KB, 0, data64KB.Length);
2032 r.GetResponse ().Close ();
2037 c.Request.InputStream.ReadAll (received, 0, received.Length);
2038 c.Response.StatusCode = 204;
2039 c.Response.Close ();
2042 Assert.AreEqual(data64KB, received);
2046 Invalid test: it does not work on linux.
2047 [pid 30973] send(9, "POST / HTTP/1.1\r\nContent-Length:"..., 89, 0) = 89
2049 [pid 30970] send(16, "HTTP/1.1 200 OK\r\nServer: Mono-HT"..., 133, 0) = 133
2051 [pid 30970] close(16) = 0
2053 [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
2055 [pid 30966] close(4) = 0
2058 The server sideis closed (FD 16) and the send on the client side (FD 9) succeeds.
2060 [Category("NotWorking")]
2061 public void WriteServerAborts ()
2063 ManualResetEvent abort = new ManualResetEvent (false);
2064 byte [] received = new byte [data64KB.Length];
2070 r.ContentLength = data64KB.Length;
2072 using (Stream s = r.GetRequestStream()) {
2075 IOException ex = ExceptionAssert.Throws<IOException> (() => s.Write(data64KB, 0, data64KB.Length));
2091 byte [] received = new byte [data64KB.Length];
2096 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2097 using (Stream s = x.GetResponseStream()) {
2098 s.ReadAll (received, 0, received.Length);
2105 c.Response.StatusCode = 200;
2106 c.Response.ContentLength64 = data64KB.Length;
2107 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2108 c.Response.OutputStream.Close ();
2109 c.Response.Close ();
2112 Assert.AreEqual (data64KB, received);
2116 public void ReadTimeout2 ()
2118 byte [] received = new byte [data64KB.Length];
2123 r.ReadWriteTimeout = 10;
2124 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2125 using (Stream s = x.GetResponseStream ()) {
2126 WebException ex = ExceptionAssert.Throws<WebException> (() => s.ReadAll (received, 0, received.Length));
2127 Assert.AreEqual (ex.Status, WebExceptionStatus.Timeout);
2134 c.Response.StatusCode = 200;
2135 c.Response.ContentLength64 = data64KB.Length;
2136 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length / 2);
2137 Thread.Sleep (1000);
2138 // c.Response.OutputStream.Write (data64KB, data64KB.Length / 2, data64KB.Length / 2);
2139 c.Response.OutputStream.Close ();
2140 c.Response.Close ();
2145 public void ReadServerAborted ()
2147 byte [] received = new byte [data64KB.Length];
2152 using (HttpWebResponse x = (HttpWebResponse) r.GetResponse ())
2153 using (Stream s = x.GetResponseStream ()) {
2154 Assert.AreEqual (1, s.ReadAll (received, 0, received.Length));
2161 c.Response.StatusCode = 200;
2162 c.Response.ContentLength64 = data64KB.Length;
2163 c.Response.OutputStream.Write (data64KB, 0, 1);
2164 c.Response.Abort ();
2169 public void BeginGetResponse2 ()
2171 byte [] received = new byte [data64KB.Length];
2176 r.BeginGetResponse ((a) =>
2178 using (HttpWebResponse x = (HttpWebResponse) r.EndGetResponse (a))
2179 using (Stream s = x.GetResponseStream ()) {
2180 s.ReadAll (received, 0, received.Length);
2188 c.Response.StatusCode = 200;
2189 c.Response.ContentLength64 = data64KB.Length;
2190 c.Response.OutputStream.Write (data64KB, 0, data64KB.Length);
2191 c.Response.OutputStream.Close ();
2192 c.Response.Close ();
2195 Assert.AreEqual (data64KB, received);
2199 public void BeginGetResponseAborts ()
2201 ManualResetEvent aborted = new ManualResetEvent(false);
2206 r.BeginGetResponse((a) =>
2208 WebException ex = ExceptionAssert.Throws<WebException> (() => r.EndGetResponse (a));
2209 Assert.AreEqual (ex.Status, WebExceptionStatus.RequestCanceled);
2219 // Thread.Sleep (100);
2220 // c.Response.StatusCode = 200;
2221 // c.Response.ContentLength64 = 0;
2222 // c.Response.Close ();
2229 public void TestLargeDataReading ()
2231 int near2GBStartPosition = rand.Next (int.MaxValue - 500, int.MaxValue);
2232 AutoResetEvent readyGetLastPortionEvent = new AutoResetEvent (false);
2233 Exception testException = null;
2236 (request, waitHandle) =>
2240 const int timeoutMs = 5000;
2242 request.Timeout = timeoutMs;
2243 request.ReadWriteTimeout = timeoutMs;
2245 WebResponse webResponse = request.GetResponse ();
2246 Stream webResponseStream = webResponse.GetResponseStream ();
2247 Assert.IsNotNull (webResponseStream, null, "#1");
2249 Type webConnectionStreamType = webResponseStream.GetType ();
2250 FieldInfo totalReadField = webConnectionStreamType.GetField ("totalRead", BindingFlags.NonPublic | BindingFlags.Instance);
2251 Assert.IsNotNull (totalReadField, "#2");
2252 totalReadField.SetValue (webResponseStream, near2GBStartPosition);
2254 byte[] readBuffer = new byte[int.MaxValue - near2GBStartPosition];
2255 Assert.AreEqual (webResponseStream.Read (readBuffer, 0, readBuffer.Length), readBuffer.Length, "#3");
2256 readyGetLastPortionEvent.Set ();
2257 Assert.IsTrue (webResponseStream.Read (readBuffer, 0, readBuffer.Length) > 0);
2258 readyGetLastPortionEvent.Set ();
2260 webResponse.Close();
2273 processor.Request.InputStream.Close ();
2275 HttpListenerResponse response = processor.Response;
2276 response.SendChunked = true;
2278 Stream outputStream = response.OutputStream;
2279 var writeBuffer = new byte[int.MaxValue - near2GBStartPosition];
2280 outputStream.Write (writeBuffer, 0, writeBuffer.Length);
2281 readyGetLastPortionEvent.WaitOne ();
2282 outputStream.Write (writeBuffer, 0, writeBuffer.Length);
2283 readyGetLastPortionEvent.WaitOne ();
2288 if (testException != null)
2289 throw testException;
2292 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request)
2294 int port = NetworkHelpers.FindFreePort ();
2296 ManualResetEvent completed = new ManualResetEvent (false);
2297 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2298 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2300 request (client, completed);
2302 if (!completed.WaitOne (10000))
2303 Assert.Fail ("Test hung");
2306 void DoRequest (Action<HttpWebRequest, EventWaitHandle> request, Action<HttpListenerContext> processor)
2308 int port = NetworkHelpers.FindFreePort ();
2310 ManualResetEvent [] completed = new ManualResetEvent [2];
2311 completed [0] = new ManualResetEvent (false);
2312 completed [1] = new ManualResetEvent (false);
2314 using (ListenerScope scope = new ListenerScope (processor, port, completed [0])) {
2315 Uri address = new Uri (string.Format ("http://localhost:{0}", port));
2316 HttpWebRequest client = (HttpWebRequest) WebRequest.Create (address);
2318 ThreadPool.QueueUserWorkItem ((o) => request (client, completed [1]));
2320 if (!WaitHandle.WaitAll (completed, 10000))
2321 Assert.Fail ("Test hung.");
2326 [ExpectedException (typeof (ArgumentNullException))]
2327 public void NullHost ()
2329 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2334 public void NoHost ()
2336 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2337 Assert.AreEqual (req.Host, "go-mono.com");
2341 [ExpectedException (typeof (ArgumentException))]
2342 public void EmptyHost ()
2344 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2349 public void HostAndPort ()
2351 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:80");
2352 Assert.AreEqual ("go-mono.com", req.Host, "#01");
2353 req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:9000");
2354 Assert.AreEqual ("go-mono.com:9000", req.Host, "#02");
2358 public void PortRange ()
2360 for (int i = 0; i < 65536; i++) {
2363 string s = i.ToString ();
2364 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com:" + s);
2365 Assert.AreEqual ("go-mono.com:" + s, req.Host, "#" + s);
2370 [ExpectedException (typeof (ArgumentException))]
2371 public void PortBelow ()
2373 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2374 req.Host = "go-mono.com:-1";
2378 [ExpectedException (typeof (ArgumentException))]
2379 public void PortAbove ()
2381 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2382 req.Host = "go-mono.com:65536";
2386 [ExpectedException (typeof (ArgumentException))]
2387 public void HostTooLong ()
2389 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2390 string s = new string ('a', 100);
2391 req.Host = s + "." + s + "." + s + "." + s + "." + s + "." + s; // Over 255 bytes
2395 [Category ("NotWorking")] // #5490
2396 public void InvalidNamesThatWork ()
2398 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2402 req.Host = new string ('a', 64); // Should fail. Max. is 63.
2406 public void NoDate ()
2408 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2409 Assert.AreEqual (DateTime.MinValue, req.Date);
2413 public void UtcDate ()
2415 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://go-mono.com");
2416 req.Date = DateTime.UtcNow;
2417 DateTime date = req.Date;
2418 Assert.AreEqual (DateTimeKind.Local, date.Kind);
2422 public void AddAndRemoveDate ()
2424 // Neil Armstrong set his foot on Moon
2425 var landing = new DateTime (1969, 7, 21, 2, 56, 0, DateTimeKind.Utc);
2426 Assert.AreEqual (621214377600000000, landing.Ticks);
2427 var unspecified = new DateTime (1969, 7, 21, 2, 56, 0);
2428 var local = landing.ToLocalTime ();
2430 var req = (HttpWebRequest)WebRequest.Create ("http://www.mono-project.com/");
2432 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2433 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2434 Assert.AreEqual (local, req.Date);
2436 req.Date = unspecified;
2437 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2438 Assert.AreEqual (unspecified.Ticks, req.Date.Ticks);
2439 Assert.AreEqual (unspecified, req.Date);
2442 Assert.AreEqual (DateTimeKind.Local, req.Date.Kind);
2443 Assert.AreEqual (local.Ticks, req.Date.Ticks);
2444 Assert.AreEqual (local, req.Date);
2446 req.Date = DateTime.MinValue;
2447 Assert.AreEqual (DateTimeKind.Unspecified, DateTime.MinValue.Kind);
2448 Assert.AreEqual (DateTimeKind.Unspecified, req.Date.Kind);
2449 Assert.AreEqual (0, req.Date.Ticks);
2451 Assert.AreEqual (null, req.Headers.Get ("Date"));
2456 public void TestIPv6Host ()
2458 var address = "2001:0000:0000:0001:0001:0001:0157:0000";
2459 var address2 = '[' + address + ']';
2460 var uri = new Uri (string.Format ("http://{0}/test.css", address2));
2461 var hwr = (HttpWebRequest)WebRequest.Create (uri);
2463 hwr.Host = address2;
2464 Assert.AreEqual (address2, hwr.Host, "#1");
2469 [Category ("NotWorking")]
2470 public void TestIPv6Host2 ()
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);
2480 } 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) {
2497 class ListenerScope : IDisposable {
2498 EventWaitHandle completed;
2499 public HttpListener listener;
2500 Action<HttpListenerContext> processor;
2502 public ListenerScope (Action<HttpListenerContext> processor, int port, EventWaitHandle completed)
2504 this.processor = processor;
2505 this.completed = completed;
2507 this.listener = new HttpListener ();
2508 this.listener.Prefixes.Add (string.Format ("http://localhost:{0}/", port));
2509 this.listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
2510 this.listener.Start ();
2512 this.listener.BeginGetContext (this.RequestHandler, null);
2515 void RequestHandler (IAsyncResult result)
2517 HttpListenerContext context = null;
2520 context = this.listener.EndGetContext (result);
2521 } catch (HttpListenerException ex) {
2522 // check if the thread has been aborted as in the case when we are shutting down.
2523 if (ex.ErrorCode == 995)
2525 } catch (ObjectDisposedException) {
2529 ThreadPool.QueueUserWorkItem ((o) =>
2532 this.processor (context);
2533 } catch (HttpListenerException) {
2537 this.completed.Set ();
2540 public void Dispose ()
2542 this.listener.Stop ();
2547 class SslHttpServer : HttpServer {
2548 X509Certificate _certificate;
2550 protected override void Run ()
2553 Socket client = sock.Accept ();
2554 NetworkStream ns = new NetworkStream (client, true);
2555 SslServerStream s = new SslServerStream (ns, Certificate, false, false);
2556 s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
2558 StreamReader reader = new StreamReader (s);
2559 StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
2562 string hello = "<html><body><h1>Hello World!</h1></body></html>";
2563 string answer = "HTTP/1.0 200\r\n" +
2564 "Connection: close\r\n" +
2565 "Content-Type: text/html\r\n" +
2566 "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
2567 "Content-Length: " + hello.Length + "\r\n" +
2572 line = reader.ReadLine ();
2573 } while (line != "" && line != null && line.Length > 0);
2575 // Now the content. We know it's 100 bytes.
2576 // This makes BeginRead in sslclientstream block.
2577 char [] cs = new char [100];
2578 reader.Read (cs, 0, 100);
2580 writer.Write (answer);
2582 if (evt.WaitOne (5000, false))
2583 error = new Exception ("Timeout when stopping the server");
2584 } catch (Exception e) {
2589 X509Certificate Certificate {
2591 if (_certificate == null)
2592 _certificate = new X509Certificate (CertData.Certificate);
2594 return _certificate;
2598 AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
2600 PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
2606 public readonly static byte [] Certificate = {
2607 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
2608 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
2609 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
2610 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
2611 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
2612 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
2613 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
2614 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
2615 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
2616 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
2617 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
2618 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
2619 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
2620 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
2621 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
2622 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
2623 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
2624 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
2625 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
2626 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
2627 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
2628 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
2629 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
2630 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
2631 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
2632 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
2633 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
2634 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
2638 public readonly static byte [] PrivateKey = {
2639 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
2640 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
2641 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
2642 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
2643 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
2644 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
2645 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
2646 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
2647 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
2648 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
2649 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
2650 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
2651 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
2652 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
2653 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
2654 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
2655 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
2656 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
2657 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
2658 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
2659 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
2660 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
2661 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
2662 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
2663 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
2664 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
2665 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
2666 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
2667 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
2668 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
2669 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
2670 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
2671 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
2672 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
2673 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
2674 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
2675 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
2676 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
2677 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
2683 public void CookieContainerTest ()
2685 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
2686 string url = "http://" + ep.ToString ();
2688 using (SocketResponder responder = new SocketResponder (ep, s => CookieRequestHandler (s))) {
2689 CookieContainer container = new CookieContainer ();
2690 container.Add(new Uri (url), new Cookie ("foo", "bar"));
2691 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2692 request.CookieContainer = container;
2693 WebHeaderCollection headers = request.Headers;
2694 headers.Add("Cookie", "foo=baz");
2695 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2696 string responseString = null;
2697 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2698 responseString = reader.ReadToEnd ();
2701 Assert.AreEqual (1, response.Cookies.Count, "#01");
2702 Assert.AreEqual ("foo=bar", response.Headers.Get("Set-Cookie"), "#02");
2705 using (SocketResponder responder = new SocketResponder (ep, s => CookieRequestHandler (s))) {
2706 CookieContainer container = new CookieContainer ();
2707 HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
2708 request.CookieContainer = container;
2709 WebHeaderCollection headers = request.Headers;
2710 headers.Add("Cookie", "foo=baz");
2711 HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
2712 string responseString = null;
2713 using (StreamReader reader = new StreamReader (response.GetResponseStream ())) {
2714 responseString = reader.ReadToEnd ();
2717 Assert.AreEqual (0, response.Cookies.Count, "#03");
2718 Assert.AreEqual ("", response.Headers.Get("Set-Cookie"), "#04");
2722 internal static byte[] CookieRequestHandler (Socket socket)
2724 MemoryStream ms = new MemoryStream ();
2725 byte[] buffer = new byte[4096];
2726 int bytesReceived = socket.Receive (buffer);
2727 while (bytesReceived > 0) {
2728 ms.Write(buffer, 0, bytesReceived);
2729 // We don't check for Content-Length or anything else here, so we give the client a little time to write
2730 // after sending the headers
2732 if (socket.Available > 0) {
2733 bytesReceived = socket.Receive (buffer);
2740 string cookies = string.Empty;
2741 using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
2743 while ((line = sr.ReadLine ()) != null) {
2744 if (line.StartsWith ("Cookie:")) {
2745 cookies = line.Substring ("cookie: ".Length);
2750 StringWriter sw = new StringWriter ();
2751 sw.WriteLine ("HTTP/1.1 200 OK");
2752 sw.WriteLine ("Content-Type: text/xml");
2753 sw.WriteLine ("Set-Cookie: " + cookies);
2754 sw.WriteLine ("Content-Length: " + cookies.Length.ToString (CultureInfo.InvariantCulture));
2759 return Encoding.UTF8.GetBytes (sw.ToString ());
2764 [Category ("RequiresBSDSockets")]
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;