public void CanWrite() { IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint(); string url = "http://" + ep.ToString() + "/test/"; using (SocketResponder responder = new SocketResponder(ep, s => HttpWebResponseTest.FullResponseHandler(s))) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 2000; req.ReadWriteTimeout = 2000; req.KeepAlive = false; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { Stream rs = resp.GetResponseStream(); try { Assert.IsFalse(rs.CanWrite, "#1"); rs.Close(); Assert.IsFalse(rs.CanWrite, "#2"); } finally { rs.Close(); req.Abort(); } } } }
public void Read() { IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint(); string url = "http://" + ep.ToString() + "/test/"; using (SocketResponder responder = new SocketResponder(ep, s => HttpWebResponseTest.FullResponseHandler(s))) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 2000; req.ReadWriteTimeout = 2000; req.KeepAlive = false; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { Stream rs = resp.GetResponseStream(); byte [] buffer = new byte [5]; try { Assert.AreEqual(1, rs.Read(buffer, 4, 1), "#A1"); Assert.AreEqual(new byte [] { 0x00, 0x00, 0x00, 0x00, 0x3c }, buffer, "#A2"); Assert.AreEqual(2, rs.Read(buffer, 0, 2), "#B1"); Assert.AreEqual(new byte [] { 0x64, 0x75, 0x00, 0x00, 0x3c }, buffer, "#B2"); Assert.AreEqual(4, rs.Read(buffer, 1, 4), "#C1"); Assert.AreEqual(new byte [] { 0x64, 0x6d, 0x6d, 0x79, 0x20 }, buffer, "#C2"); Assert.AreEqual(2, rs.Read(buffer, 0, 3), "#D1"); Assert.AreEqual(new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#D2"); Assert.AreEqual(0, rs.Read(buffer, 1, 3), "#E1"); Assert.AreEqual(new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#E2"); Assert.AreEqual(0, rs.Read(buffer, buffer.Length, 0), "#G1"); Assert.AreEqual(new byte [] { 0x2f, 0x3e, 0x6d, 0x79, 0x20 }, buffer, "#G2"); } finally { rs.Close(); req.Abort(); } } } }
public void BeginWrite() { IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint(); string url = "http://" + ep.ToString() + "/test/"; using (SocketResponder responder = new SocketResponder(ep, s => HttpWebResponseTest.FullResponseHandler(s))) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 2000; req.ReadWriteTimeout = 2000; req.KeepAlive = false; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { Stream rs = resp.GetResponseStream(); byte [] buffer = new byte [5]; try { rs.BeginWrite(buffer, 0, buffer.Length, null, null); Assert.Fail("#1"); } catch (NotSupportedException ex) { // The stream does not support writing Assert.AreEqual(typeof(NotSupportedException), ex.GetType(), "#2"); Assert.IsNull(ex.InnerException, "#3"); Assert.IsNotNull(ex.Message, "#4"); } finally { rs.Close(); req.Abort(); } } } }
public void Read_Offset_Overflow() { IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint(); string url = "http://" + ep.ToString() + "/test/"; using (SocketResponder responder = new SocketResponder(ep, s => HttpWebResponseTest.FullResponseHandler(s))) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 2000; req.ReadWriteTimeout = 2000; req.KeepAlive = false; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { Stream rs = resp.GetResponseStream(); byte [] buffer = new byte [5]; try { try { rs.Read(buffer, buffer.Length + 1, 0); Assert.Fail("#A1"); } catch (ArgumentOutOfRangeException ex) { // Specified argument was out of the range of valid values Assert.AreEqual(typeof(ArgumentOutOfRangeException), ex.GetType(), "#A2"); Assert.IsNull(ex.InnerException, "#A3"); Assert.IsNotNull(ex.Message, "#A4"); Assert.AreEqual("offset", ex.ParamName, "#A5"); } // read full response buffer = new byte [24]; Assert.AreEqual(9, rs.Read(buffer, 0, buffer.Length)); try { rs.Read(buffer, buffer.Length + 1, 0); Assert.Fail("#B1"); } catch (ArgumentOutOfRangeException ex) { // Specified argument was out of the range of valid values Assert.AreEqual(typeof(ArgumentOutOfRangeException), ex.GetType(), "#B2"); Assert.IsNull(ex.InnerException, "#B3"); Assert.IsNotNull(ex.Message, "#B4"); Assert.AreEqual("offset", ex.ParamName, "#B5"); } } finally { rs.Close(); req.Abort(); } } } }
public void Read_Stream_Closed() { IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint(); string url = "http://" + ep.ToString() + "/test/"; using (SocketResponder responder = new SocketResponder(ep, s => HttpWebResponseTest.FullResponseHandler(s))) { HttpWebRequest req; req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 2000; req.ReadWriteTimeout = 2000; req.KeepAlive = false; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { Stream rs = resp.GetResponseStream(); rs.Close(); try { rs.Read(new byte [0], 0, 0); Assert.Fail("#A1"); } catch (WebException ex) { // The request was aborted: The connection was closed unexpectedly Assert.AreEqual(typeof(WebException), ex.GetType(), "#A2"); Assert.IsNull(ex.InnerException, "#A3"); Assert.IsNotNull(ex.Message, "#A4"); Assert.IsNull(ex.Response, "#A5"); Assert.AreEqual(WebExceptionStatus.ConnectionClosed, ex.Status, "#A6"); } finally { rs.Close(); req.Abort(); } } req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Timeout = 2000; req.ReadWriteTimeout = 2000; req.KeepAlive = false; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { Stream rs = resp.GetResponseStream(); byte [] buffer = new byte [24]; Assert.AreEqual(9, rs.Read(buffer, 0, buffer.Length)); rs.Close(); try { rs.Read(new byte [0], 0, 0); Assert.Fail("#B1"); } catch (WebException ex) { // The request was aborted: The connection was closed unexpectedly Assert.AreEqual(typeof(WebException), ex.GetType(), "#B2"); Assert.IsNull(ex.InnerException, "#B3"); Assert.IsNotNull(ex.Message, "#B4"); Assert.IsNull(ex.Response, "#B5"); Assert.AreEqual(WebExceptionStatus.ConnectionClosed, ex.Status, "#B6"); } finally { rs.Close(); req.Abort(); } } } }