public void TestSimpleClosed() { string sampleRequest = "HTTP/1.0 200 OK\r\nHost: localhost\r\n\r\n"; ChunkedBuffer buffer = new ChunkedBuffer(pool); buffer.Write(Encoding.ASCII.GetBytes(sampleRequest), 0, Encoding.ASCII.GetByteCount(sampleRequest)); HttpResponse response = new HttpResponse(pool); Assert.IsTrue(response.Parse(buffer.Stream, true)); Assert.AreEqual("HTTP/1.0", response.Version); Assert.AreEqual("200", response.Code); Assert.AreEqual("OK", response.Reason); Assert.AreEqual("HTTP/1.0 200 OK", response.CommandLine); Assert.AreEqual("localhost", response.Header["Host"]); Assert.AreEqual(0, response.BodySize); Assert.AreEqual(0, response.Body.WritePosition); MemoryStream stream = new MemoryStream(); response.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(sampleRequest, reader.ReadToEnd()); } }
public void TestChunked() { string sampleContent = "<test><val>hello</val></test>"; int sampleContentLength = Encoding.UTF8.GetByteCount(sampleContent); string chunk1Content = "<test><val>"; string chunk2Content = "hello</val>"; string chunk3Content = "</test>"; int chunk1ContentLength = Encoding.UTF8.GetByteCount(chunk1Content); int chunk2ContentLength = Encoding.UTF8.GetByteCount(chunk2Content); int chunk3ContentLength = Encoding.UTF8.GetByteCount(chunk3Content); string chunk1Request = "HTTP/1.0 200 OK\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n" + string.Format("{0:X}", chunk1ContentLength) + "\r\n" + chunk1Content + "\r\n"; string chunk2Request = string.Format("{0:X}", chunk2ContentLength) + "\r\n" + chunk2Content + "\r\n"; string chunk3Request = string.Format("{0:X}", chunk3ContentLength) + "\r\n" + chunk3Content + "\r\n"; string chunk4Request = "0\r\n\r\n"; ChunkedBuffer buffer1 = new ChunkedBuffer(pool); buffer1.Write(Encoding.ASCII.GetBytes(chunk1Request), 0, Encoding.ASCII.GetByteCount(chunk1Request)); ChunkedBuffer buffer2 = new ChunkedBuffer(pool); buffer2.Write(Encoding.ASCII.GetBytes(chunk2Request), 0, Encoding.ASCII.GetByteCount(chunk2Request)); ChunkedBuffer buffer3 = new ChunkedBuffer(pool); buffer3.Write(Encoding.ASCII.GetBytes(chunk3Request), 0, Encoding.ASCII.GetByteCount(chunk3Request)); ChunkedBuffer buffer4 = new ChunkedBuffer(pool); buffer4.Write(Encoding.ASCII.GetBytes(chunk4Request), 0, Encoding.ASCII.GetByteCount(chunk4Request)); HttpResponse response = new HttpResponse(pool); Assert.IsFalse(response.IsChunked); Assert.IsFalse(response.Parse(buffer1.Stream, false)); Assert.IsTrue(response.IsChunked); Assert.IsFalse(response.Parse(buffer2.Stream, false)); Assert.IsTrue(response.IsChunked); Assert.IsFalse(response.Parse(buffer3.Stream, false)); Assert.IsTrue(response.IsChunked); Assert.IsTrue(response.Parse(buffer4.Stream, false)); Assert.IsTrue(response.IsChunked); Assert.AreEqual("HTTP/1.0", response.Version); Assert.AreEqual("200", response.Code); Assert.AreEqual("OK", response.Reason); Assert.AreEqual("HTTP/1.0 200 OK", response.CommandLine); Assert.AreEqual("localhost", response.Header["Host"]); Assert.AreEqual(sampleContentLength, response.BodySize); Assert.AreEqual(sampleContentLength, response.Body.WritePosition); MemoryStream stream = new MemoryStream(); response.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual("HTTP/1.0 200 OK\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n" + sampleContent, reader.ReadToEnd()); } }
public void TestContentLengthNotClosed() { string sampleContent = "<test><val>hello</val></test>"; int sampleContentLength = Encoding.UTF8.GetByteCount(sampleContent); string sampleRequest = "HTTP/1.0 200 OK\r\nHost: localhost\r\nContent-Length: " + sampleContentLength + "\r\n\r\n" + sampleContent; ChunkedBuffer buffer = new ChunkedBuffer(pool); buffer.Write(Encoding.ASCII.GetBytes(sampleRequest), 0, Encoding.ASCII.GetByteCount(sampleRequest)); HttpResponse response = new HttpResponse(pool); Assert.IsTrue(response.Parse(buffer.Stream, false)); Assert.AreEqual("HTTP/1.0", response.Version); Assert.AreEqual("200", response.Code); Assert.AreEqual("OK", response.Reason); Assert.AreEqual("HTTP/1.0 200 OK", response.CommandLine); Assert.AreEqual("localhost", response.Header["Host"]); Assert.AreEqual(sampleContentLength, response.BodySize); Assert.AreEqual(sampleContentLength, response.Body.WritePosition); MemoryStream stream = new MemoryStream(); response.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(sampleRequest, reader.ReadToEnd()); } }