public void HttpHeaderCollection_ParseRequest() { HttpHeaderCollection headers; byte[] buf; BlockArray blocks; int dataPos; headers = new HttpHeaderCollection(true); buf = Encoding.ASCII.GetBytes("GET /Foo/bar.htm HTTP/1.1\r\n\r\n"); headers.BeginParse(); Assert.IsTrue(headers.Parse(buf, buf.Length)); blocks = headers.EndParse(out dataPos); CollectionAssert.AreEqual(buf, blocks.ToByteArray()); Assert.AreEqual(buf.Length, dataPos); Assert.AreEqual("GET", headers.Method); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("/Foo/bar.htm", headers.RawUri); headers = new HttpHeaderCollection(true); buf = Encoding.ASCII.GetBytes("get /Foo/bar.htm HTTP/1.1\r\nHeader1: Test1\r\nHeader2:\tTest2 \r\nHeader3: Folded \r\n Here \r\n\r\n"); headers.BeginParse(); Assert.IsTrue(headers.Parse(buf, buf.Length)); blocks = headers.EndParse(out dataPos); CollectionAssert.AreEqual(buf, blocks.ToByteArray()); Assert.AreEqual(buf.Length, dataPos); Assert.AreEqual("GET", headers.Method); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("/Foo/bar.htm", headers.RawUri); Assert.AreEqual("Test1", headers["HEADER1"]); Assert.AreEqual("Test2", headers["HEADER2"]); Assert.AreEqual("Folded Here", headers["Header3"]); headers = new HttpHeaderCollection(true); buf = Encoding.ASCII.GetBytes("get /Foo/bar.htm HTTP/1.0\r\nHeader1: Test1\r\nHeader1:\tTest2 \r\nHeader3: Test3\r\n\r\nabcd"); headers.BeginParse(); Assert.IsTrue(headers.Parse(buf, buf.Length)); blocks = headers.EndParse(out dataPos); CollectionAssert.AreEqual(buf, blocks.ToByteArray()); Assert.AreEqual(buf.Length - 4, dataPos); Assert.AreEqual("GET", headers.Method); Assert.AreEqual(HttpStack.Http10, headers.HttpVersion); Assert.AreEqual("/Foo/bar.htm", headers.RawUri); Assert.AreEqual("Test1, Test2", headers["Header1"]); Assert.AreEqual("Test3", headers["Header3"]); }
public void HttpHeaderCollection_ParseResponse() { HttpHeaderCollection headers; byte[] buf; BlockArray blocks; int dataPos; headers = new HttpHeaderCollection(false); buf = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\n\r\n"); headers.BeginParse(); Assert.IsTrue(headers.Parse(buf, buf.Length)); blocks = headers.EndParse(out dataPos); CollectionAssert.AreEqual(buf, blocks.ToByteArray()); Assert.AreEqual(buf.Length, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); headers = new HttpHeaderCollection(false); buf = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nHeader1: Test1\r\nHeader2:\tTest2 \r\nHeader3: Folded \r\n Here \r\n\r\n"); headers.BeginParse(); Assert.IsTrue(headers.Parse(buf, buf.Length)); blocks = headers.EndParse(out dataPos); CollectionAssert.AreEqual(buf, blocks.ToByteArray()); Assert.AreEqual(buf.Length, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); Assert.AreEqual("Test1", headers["Header1"]); Assert.AreEqual("Test2", headers["Header2"]); Assert.AreEqual("Folded Here", headers["Header3"]); headers = new HttpHeaderCollection(false); buf = Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\nHeader1: Test1\r\nHeader1:\tTest2 \r\nHeader3: Test3\r\n\r\nabcd"); headers.BeginParse(); Assert.IsTrue(headers.Parse(buf, buf.Length)); blocks = headers.EndParse(out dataPos); CollectionAssert.AreEqual(buf, blocks.ToByteArray()); Assert.AreEqual(buf.Length - 4, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http10, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); Assert.AreEqual("Test1, Test2", headers["Header1"]); Assert.AreEqual("Test3", headers["Header3"]); }
public void HttpHeaderCollection_CRLF_Boundary() { HttpHeaderCollection headers; byte[] buf1, buf2; BlockArray blocks; int dataPos; headers = new HttpHeaderCollection(false); buf1 = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK"); buf2 = Encoding.ASCII.GetBytes("\r\n\r\nabcd"); headers.BeginParse(); Assert.IsFalse(headers.Parse(buf1, buf1.Length)); Assert.IsTrue(headers.Parse(buf2, buf2.Length)); blocks = headers.EndParse(out dataPos); Assert.AreEqual(buf1.Length + buf2.Length - 4, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); headers = new HttpHeaderCollection(false); buf1 = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r"); buf2 = Encoding.ASCII.GetBytes("\n\r\nabcd"); headers.BeginParse(); Assert.IsFalse(headers.Parse(buf1, buf1.Length)); Assert.IsTrue(headers.Parse(buf2, buf2.Length)); blocks = headers.EndParse(out dataPos); Assert.AreEqual(buf1.Length + buf2.Length - 4, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); headers = new HttpHeaderCollection(false); buf1 = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\n"); buf2 = Encoding.ASCII.GetBytes("\r\nabcd"); headers.BeginParse(); Assert.IsFalse(headers.Parse(buf1, buf1.Length)); Assert.IsTrue(headers.Parse(buf2, buf2.Length)); blocks = headers.EndParse(out dataPos); Assert.AreEqual(buf1.Length + buf2.Length - 4, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); headers = new HttpHeaderCollection(false); buf1 = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\n\r"); buf2 = Encoding.ASCII.GetBytes("\nabcd"); headers.BeginParse(); Assert.IsFalse(headers.Parse(buf1, buf1.Length)); Assert.IsTrue(headers.Parse(buf2, buf2.Length)); blocks = headers.EndParse(out dataPos); Assert.AreEqual(buf1.Length + buf2.Length - 4, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); headers = new HttpHeaderCollection(false); buf1 = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nHeader1: Test1\r\nHeader2 : Test2\r\n\r"); buf2 = Encoding.ASCII.GetBytes("\nabcd"); headers.BeginParse(); Assert.IsFalse(headers.Parse(buf1, buf1.Length)); Assert.IsTrue(headers.Parse(buf2, buf2.Length)); blocks = headers.EndParse(out dataPos); Assert.AreEqual(buf1.Length + buf2.Length - 4, dataPos); Assert.AreEqual(HttpStatus.OK, headers.Status); Assert.AreEqual(HttpStack.Http11, headers.HttpVersion); Assert.AreEqual("OK", headers.Reason); Assert.AreEqual("Test1", headers["HEADER1"]); Assert.AreEqual("Test2", headers["HEADER2"]); }