Exemple #1
0
        public void Send(HttpRequestMetadata metadata, byte[] content)
        {
            var metadataBuffer = metadata.Serialize();

            _stream.Write(metadataBuffer, 0, metadataBuffer.Length);
            this.RawDataSent?.Invoke(metadataBuffer);

            if (content.Length > 0)
            {
                _stream.Write(content, 0, content.Length);
                this.RawDataSent?.Invoke(content);
            }
        }
        public void Constructor_ValidArguments_RunsOk()
        {
            // Arrange
            var line         = new HttpRequestLine(HttpMethod.Get, "/");
            var headersArray = new[]
            {
                new HttpHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*"),
                new HttpHeader("Accept-Language", "ru-RU"),
                new HttpHeader("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"),
                new HttpHeader("Accept-Encoding", "gzip, deflate"),
                new HttpHeader("Host", "allitebooks.com"),
                new HttpHeader("Connection", "Keep-Alive"),
            };
            var headers = new HttpHeaderCollection(headersArray);

            // Act
            var requestMetadata = new HttpRequestMetadata(line, headers);

            // 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), Is.SameAs(headersArray[0]));
            Assert.That(requestMetadata.Headers.ElementAt(1), Is.SameAs(headersArray[1]));
            Assert.That(requestMetadata.Headers.ElementAt(2), Is.SameAs(headersArray[2]));
            Assert.That(requestMetadata.Headers.ElementAt(3), Is.SameAs(headersArray[3]));
            Assert.That(requestMetadata.Headers.ElementAt(4), Is.SameAs(headersArray[4]));
            Assert.That(requestMetadata.Headers.ElementAt(5), Is.SameAs(headersArray[5]));

            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()));
        }