Example #1
0
        public void ParseRequestStream_AllHeaderTypes_AllHeadersParsed()
        {
            var s = new HttpRequestParser();

            var streamedRequest = new[] {
                "GET /api/data HTTP/1.1",
                "Content-Length: 4",
                "Accept: application/json,text/xml",
                "Accept-Charset: utf-7;q=0.2, utf-8;q=0.1,*;q=0",
                "Content-Type: text/xml;charset=utf-8",
                "UnknownHeader: some:value",
                "",
                "data"
            };

            var byteStreamParts = new List<byte[]>();
            byteStreamParts.Add(Encoding.UTF8.GetBytes(string.Join("\r\n", streamedRequest)));

            var request = s.ParseRequestStream(new TestStream(byteStreamParts)).Result;

            Assert.AreEqual(true, request.IsComplete);
            Assert.IsTrue(request.Headers.Any(h => h.Name == "UnknownHeader" && h.RawContent == "some:value"));
            Assert.AreEqual(4, request.ContentLength);
            Assert.AreEqual(Encoding.UTF8, request.RequestContentEncoding);
            Assert.AreEqual(MediaType.XML, request.RequestContentType);
            Assert.AreEqual(Encoding.UTF7, request.ResponseContentEncoding);
            Assert.AreEqual(MediaType.JSON, request.ResponseContentType);
        }
Example #2
0
        public void ParseRequestStream_ContentLengthNumberMissing_RequestIncomplete()
        {
            var s = new HttpRequestParser();

            var streamedRequest = new[] {
                "GET /api/data HTTP/1.1",
                "Content-Length: four",
                "",
                "data"
            };

            var byteStreamParts = new List<byte[]>();
            byteStreamParts.Add(Encoding.UTF8.GetBytes(string.Join("\r\n", streamedRequest)));

            var request = s.ParseRequestStream(new TestStream(byteStreamParts)).Result;

            Assert.AreEqual(false, request.IsComplete);
        }
Example #3
0
        public void ParseRequestStream_TooMuchData_RequestIncomplete()
        {
            var s = new HttpRequestParser();

            var streamedRequest = new[] {
                "GET /api/data HTTP/1.1",
                "Content-Length: 4",
                "",
                "data"
            };

            var extraData = "plusanotherextrafewbytes";
            var byteStreamParts = new List<byte[]>();
            byteStreamParts.Add(Encoding.UTF8.GetBytes(string.Join("\r\n", streamedRequest) + extraData));

            var request = s.ParseRequestStream(new TestStream(byteStreamParts)).Result;

            Assert.AreEqual(false, request.IsComplete);
        }
Example #4
0
        public void ParseRequestStream_ThreeEmptyResponses_EmptyRequestString()
        {
            var s = new HttpRequestParser();

            var streamedRequest = new[] {
                "GET /api/data HTTP/1.1",
                "Content-Length: 4\r\n", //to force double /r/n on string.Join
                ""
            };

            var body = "data";
            var byteStreamParts = new List<byte[]>();
            byteStreamParts.Add(Encoding.UTF8.GetBytes(string.Join("\r\n", streamedRequest)));
            byteStreamParts.Add(new byte[] { });
            byteStreamParts.Add(new byte[] { });
            byteStreamParts.Add(new byte[] { });
            byteStreamParts.Add(new byte[] { });
            byteStreamParts.Add(Encoding.UTF8.GetBytes(body));

            var request = s.ParseRequestStream(new TestStream(byteStreamParts)).Result;

            Assert.AreEqual(false, request.IsComplete);
        }
Example #5
0
        public void ParseRequestStream_WithoutDataAndHeaders_CompleteRequest()
        {
            var s = new HttpRequestParser();

            var streamedRequest = new[] {
                "GET /api/data HTTP/1.1\r\n", //to force double /r/n on string.Join
                ""
            };

            var byteStreamParts = new List<byte[]>();
            byteStreamParts.Add(Encoding.UTF8.GetBytes(string.Join("\r\n", streamedRequest)));

            var request = s.ParseRequestStream(new TestStream(byteStreamParts)).Result;

            Assert.AreEqual(true, request.IsComplete);
        }
Example #6
0
        public void ParseRequestStream_PartedDataWithEmptyReponseInBetween_ValidRequest()
        {
            var s = new HttpRequestParser();

            var streamedRequest = new[] {
                "GET /api/data HTTP/1.1",
                "Content-Length: 4\r\n", //to force double /r/n on string.Join
                ""
            };

            var content = "data";
            var byteStreamParts = new List<byte[]>();
            byteStreamParts.Add(Encoding.UTF8.GetBytes(string.Join("\r\n", streamedRequest)));
            byteStreamParts.Add(new byte[] { });
            byteStreamParts.Add(Encoding.UTF8.GetBytes(content));

            var request = s.ParseRequestStream(new TestStream(byteStreamParts)).Result;

            Assert.AreEqual(true, request.IsComplete);
            Assert.AreEqual(content, request.Content);
        }