Example #1
0
        private void DoSend()
        {
            var buffer = textBoxRequestText.Text.ToAsciiBytes();

            var metadata = HttpRequestMetadata.Parse(buffer, 0);
            var len      = metadata.ToString().Length;
            var content  = buffer.Skip(len).ToArray();

            _client.Send(metadata, content);
        }
        public void Parse_InputIsValid_ParsesCorrectly(string input)
        {
            // Arrange
            var buffer = input.ToAsciiBytes();

            // Act
            var requestMetadata = HttpRequestMetadata.Parse(buffer, 3);

            // Assert
            Assert.That(requestMetadata.Line.Method.ToString(), Is.EqualTo("GET"));
            Assert.That(requestMetadata.Line.RequestUri, Is.EqualTo("/"));
            Assert.That(requestMetadata.Line.Version, Is.EqualTo("HTTP/1.1"));

            Assert.That(requestMetadata.Headers.Count(), Is.EqualTo(6));
            Assert.That(requestMetadata.Headers.ElementAt(0).Name, Is.EqualTo("Accept"));
            Assert.That(requestMetadata.Headers.ElementAt(0).Value, Is.EqualTo("text/html, application/xhtml+xml, image/jxr, */*"));

            Assert.That(requestMetadata.Headers.ElementAt(1).Name, Is.EqualTo("Accept-Language"));
            Assert.That(requestMetadata.Headers.ElementAt(1).Value, Is.EqualTo("ru-RU"));

            Assert.That(requestMetadata.Headers.ElementAt(2).Name, Is.EqualTo("User-Agent"));
            Assert.That(requestMetadata.Headers.ElementAt(2).Value, Is.EqualTo("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299"));

            Assert.That(requestMetadata.Headers.ElementAt(3).Name, Is.EqualTo("Accept-Encoding"));
            Assert.That(requestMetadata.Headers.ElementAt(3).Value, Is.EqualTo("gzip, deflate"));

            Assert.That(requestMetadata.Headers.ElementAt(4).Name, Is.EqualTo("Host"));
            Assert.That(requestMetadata.Headers.ElementAt(4).Value, Is.EqualTo("allitebooks.com"));

            Assert.That(requestMetadata.Headers.ElementAt(5).Name, Is.EqualTo("Connection"));
            Assert.That(requestMetadata.Headers.ElementAt(5).Value, Is.EqualTo("Keep-Alive"));


            var expectedString = @"GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: ru-RU
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
Accept-Encoding: gzip, deflate
Host: allitebooks.com
Connection: Keep-Alive

";
            var toString       = requestMetadata.ToString();

            Assert.That(toString, Is.EqualTo(expectedString));

            var bytes = requestMetadata.Serialize();

            Assert.That(bytes, Is.EquivalentTo(expectedString.ToAsciiBytes()));
        }
        public void Parse_NoCrLfs_ThrowsBadHttpDataException()
        {
            // Arrange
            var input  = @"жжжGET / HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: ru-RU
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
Accept-Encoding: gzip, deflate
Host: allitebooks.com
Connection: Keep-Alive";
            var buffer = input.ToAsciiBytes();

            // Act & Assert
            Assert.Throws <BadHttpDataException>(() => HttpRequestMetadata.Parse(buffer, 3));
        }