* HttpWebRequestTest.cs: Added tests for bugs #465613, #510661,
authorGert Driesen <drieseng@users.sourceforge.net>
Fri, 5 Jun 2009 23:20:31 +0000 (23:20 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Fri, 5 Jun 2009 23:20:31 +0000 (23:20 -0000)
#510642 and #508027.

svn path=/trunk/mcs/; revision=135573

mcs/class/System/Test/System.Net/ChangeLog
mcs/class/System/Test/System.Net/HttpWebRequestTest.cs

index fe1ab1f713f9ba7f745d6c23630a6a543f24e57c..9188cfe5bea002a9105acc5bfd902033763a13aa 100644 (file)
@@ -1,3 +1,8 @@
+2009-06-06  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * HttpWebRequestTest.cs: Added tests for bugs #465613, #510661,
+       #510642 and #508027.
+
 2009-05-17  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * CookieTest.cs: Added test for Path.
index b5a13f241b8add59f0623bc96a10cc1f719bc02f..a3060eb7cab09374679306874ebcb25bfd4ea6b5 100644 (file)
@@ -128,7 +128,7 @@ namespace MonoTests.System.Net
                }
 
                [Test]
-               [Category("InetAccess")] 
+               [Category("InetAccess")]
                public void Cookies1 ()
                {
                        // The purpose of this test is to ensure that the cookies we get from a request
@@ -286,6 +286,241 @@ namespace MonoTests.System.Net
                        }
                }
 
+               [Test]
+               public void BeginGetRequestStream_Body_NotAllowed ()
+               {
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8000);
+                       string url = "http://" + IPAddress.Loopback.ToString () + ":8000/test/";
+
+                       using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
+                               responder.Start ();
+
+                               HttpWebRequest request;
+
+                               request = (HttpWebRequest) WebRequest.Create (url);
+                               request.Method = "GET";
+
+                               try {
+                                       request.BeginGetRequestStream (null, null);
+                                       Assert.Fail ("#A1");
+                               } catch (ProtocolViolationException ex) {
+                                       // Cannot send a content-body with this
+                                       // verb-type
+                                       Assert.IsNull (ex.InnerException, "#A2");
+                                       Assert.IsNotNull (ex.Message, "#A3");
+                               }
+
+                               request = (HttpWebRequest) WebRequest.Create (url);
+                               request.Method = "HEAD";
+
+                               try {
+                                       request.BeginGetRequestStream (null, null);
+                                       Assert.Fail ("#B1");
+                               } catch (ProtocolViolationException ex) {
+                                       // Cannot send a content-body with this
+                                       // verb-type
+                                       Assert.IsNull (ex.InnerException, "#B2");
+                                       Assert.IsNotNull (ex.Message, "#B3");
+                               }
+                       }
+               }
+
+               [Test] // bug #465613
+               [Category ("NotWorking")]
+               public void BeginGetRequestStream_NoBuffering ()
+               {
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8002);
+                       string url = "http://" + IPAddress.Loopback.ToString () + ":8002/test/";
+
+                       using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
+                               responder.Start ();
+
+                               HttpWebRequest req;
+                               Stream rs;
+                               IAsyncResult ar;
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = false;
+                               req.KeepAlive = false;
+                               req.AllowWriteStreamBuffering = false;
+
+                               ar = req.BeginGetRequestStream (null, null);
+                               rs = req.EndGetRequestStream (ar);
+                               rs.Close ();
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = false;
+                               req.KeepAlive = true;
+                               req.AllowWriteStreamBuffering = false;
+
+                               try {
+                                       req.BeginGetRequestStream (null, null);
+                                       Assert.Fail ("#A1");
+                               } catch (ProtocolViolationException ex) {
+                                       // When performing a write operation with
+                                       // AllowWriteStreamBuffering set to false,
+                                       // you must either set ContentLength to a
+                                       // non-negative number or set SendChunked
+                                       // to true
+                                       Assert.IsNull (ex.InnerException, "#A2");
+                                       Assert.IsNotNull (ex.Message, "#A3");
+                               }
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = false;
+                               req.KeepAlive = true;
+                               req.AllowWriteStreamBuffering = false;
+                               req.ContentLength = 0;
+
+                               ar = req.BeginGetRequestStream (null, null);
+                               rs = req.EndGetRequestStream (ar);
+                               rs.Close ();
+                       }
+               }
+
+               [Test] // bug #508027
+               [Category ("NotWorking")]
+               public void BeginGetResponse ()
+               {
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8003);
+                       string url = "http://" + IPAddress.Loopback.ToString () + ":8003/test/";
+
+                       using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
+                               responder.Start ();
+
+                               HttpWebRequest req;
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = false;
+                               req.KeepAlive = false;
+                               req.AllowWriteStreamBuffering = false;
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = true;
+                               req.KeepAlive = false;
+                               req.AllowWriteStreamBuffering = false;
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.ContentLength = 5;
+                               req.SendChunked = false;
+                               req.KeepAlive = false;
+                               req.AllowWriteStreamBuffering = false;
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = false;
+                               req.KeepAlive = true;
+                               req.AllowWriteStreamBuffering = false;
+#if NET_2_0
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+#else
+                               try {
+                                       req.BeginGetResponse (null, null);
+                               } catch (ProtocolViolationException ex) {
+                                       // Either ContentLength must be set to a non-negative
+                                       // number, or SendChunked set to true in order to perform
+                                       // the write operation when AllowWriteStreamBuffering
+                                       // is disabled
+                                       Assert.IsNull (ex.InnerException, "#A2");
+                                       Assert.IsNotNull (ex.Message, "#A3");
+                               } finally {
+                                       req.Abort ();
+                               }
+#endif
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = false;
+                               req.KeepAlive = false;
+                               req.AllowWriteStreamBuffering = false;
+                               req.ContentLength = 5;
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.SendChunked = false;
+                               req.KeepAlive = true;
+                               req.AllowWriteStreamBuffering = false;
+                               req.ContentLength = 5;
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "GET";
+                               req.SendChunked = true;
+#if NET_2_0
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+#else
+                               try {
+                                       req.BeginGetResponse (null, null);
+                                       Assert.Fail ("#B1");
+                               } catch (ProtocolViolationException ex) {
+                                       // Content-Length cannot be set for a
+                                       // non-write operation
+                                       Assert.IsNull (ex.InnerException, "#B2");
+                                       Assert.IsNotNull (ex.Message, "#B3");
+                               } finally {
+                                       req.Abort ();
+                               }
+#endif
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "GET";
+                               req.ContentLength = 5;
+#if NET_2_0
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+#else
+                               try {
+                                       req.BeginGetResponse (null, null);
+                                       Assert.Fail ("#C1");
+                               } catch (ProtocolViolationException ex) {
+                                       // Content-Length cannot be set for a
+                                       // non-write operation
+                                       Assert.IsNull (ex.InnerException, "#C2");
+                                       Assert.IsNotNull (ex.Message, "#C3");
+                               } finally {
+                                       req.Abort ();
+                               }
+#endif
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "GET";
+                               req.ContentLength = 0;
+#if NET_2_0
+                               req.BeginGetResponse (null, null);
+                               req.Abort ();
+#else
+                               try {
+                                       req.BeginGetResponse (null, null);
+                                       Assert.Fail ("#D1");
+                               } catch (ProtocolViolationException ex) {
+                                       // Content-Length cannot be set for a
+                                       // non-write operation
+                                       Assert.IsNull (ex.InnerException, "#D2");
+                                       Assert.IsNotNull (ex.Message, "#D3");
+                               } finally {
+                                       req.Abort ();
+                               }
+#endif
+                       }
+               }
+
                [Test] // bug #429200
                public void GetRequestStream ()
                {
@@ -307,7 +542,108 @@ namespace MonoTests.System.Net
                                Assert.AreSame (rs1, rs2, "#2");
 
                                rs1.Close ();
-                               responder.Stop ();
+                       }
+               }
+
+               [Test] // bug #510661
+               [Category ("NotWorking")]
+               public void GetRequestStream_Close_NotAllBytesWritten ()
+               {
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8000);
+                       string url = "http://" + IPAddress.Loopback.ToString () + ":8000/test/";
+
+                       using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
+                               responder.Start ();
+
+                               HttpWebRequest req;
+                               Stream rs;
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.ContentLength = 2;
+                               rs = req.GetRequestStream ();
+                               try {
+                                       rs.Close ();
+                                       Assert.Fail ("#A1");
+                               } catch (WebException ex) {
+                                       // The request was aborted: The request was canceled
+                                       Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
+                                       Assert.IsNotNull (ex.Message, "#A3");
+                                       Assert.IsNull (ex.Response, "#A4");
+                                       Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#A5");
+
+                                       // Cannot close stream until all bytes are written
+                                       Exception inner = ex.InnerException;
+                                       Assert.IsNotNull (inner, "#A6");
+                                       Assert.AreEqual (typeof (IOException), inner.GetType (), "#A7");
+                                       Assert.IsNull (inner.InnerException, "#A8");
+                                       Assert.IsNotNull (inner.Message, "#A9");
+                               }
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.ContentLength = 2;
+                               rs = req.GetRequestStream ();
+                               rs.WriteByte (0x0d);
+                               try {
+                                       rs.Close ();
+                                       Assert.Fail ("#B1");
+                               } catch (WebException ex) {
+                                       // The request was aborted: The request was canceled
+                                       Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
+                                       Assert.IsNotNull (ex.Message, "#B3");
+                                       Assert.IsNull (ex.Response, "#B4");
+                                       Assert.AreEqual (WebExceptionStatus.RequestCanceled, ex.Status, "#B5");
+
+                                       // Cannot close stream until all bytes are written
+                                       Exception inner = ex.InnerException;
+                                       Assert.IsNotNull (inner, "#B6");
+                                       Assert.AreEqual (typeof (IOException), inner.GetType (), "#B7");
+                                       Assert.IsNull (inner.InnerException, "#B8");
+                                       Assert.IsNotNull (inner.Message, "#B9");
+                               }
+
+                               req = (HttpWebRequest) WebRequest.Create (url);
+                               req.Method = "POST";
+                               req.ContentLength = 2;
+                               rs = req.GetRequestStream ();
+                               rs.WriteByte (0x0d);
+                               rs.WriteByte (0x0d);
+                               rs.Close ();
+                       }
+               }
+
+               [Test] // bug #510642
+               [Category ("NotWorking")]
+               public void GetRequestStream_Write_Overflow ()
+               {
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8001);
+                       string url = "http://" + IPAddress.Loopback.ToString () + ":8001/test/";
+
+                       using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) {
+                               responder.Start ();
+
+                               HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
+                               req.ProtocolVersion = HttpVersion.Version11;
+                               req.Method = "POST";
+                               req.Timeout = 200;
+                               req.ReadWriteTimeout = 100;
+                               req.ContentLength = 2;
+
+                               Stream rs = req.GetRequestStream ();
+
+                               byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d };
+                               try {
+                                       rs.Write (buffer, 0, 3);
+                                       Assert.Fail ("#1");
+                               } catch (ProtocolViolationException ex) {
+                                       // Bytes to be written to the stream exceed
+                                       // Content-Length bytes size specified
+                                       Assert.IsNull (ex.InnerException, "#2");
+                                       Assert.IsNotNull (ex.Message, "#3");
+                               } finally {
+                                       req.Abort ();
+                               }
                        }
                }