Пример #1
0
        public static HttpRequest FromString(string value)
        {
            if (null == value)
            {
                throw new ArgumentNullException("value");
            }

            if (0 == value.Length)
            {
                throw new ArgumentOutOfRangeException("value");
            }

            var result = new HttpRequest();

            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(value);
                    writer.Flush();
                    stream.Position = 0;
                    using (var reader = new StreamReader(stream))
                    {
                        result.Read(reader);
                    }
                }
            }

            return result;
        }
Пример #2
0
        public void opImplicit_HttpRequest_string()
        {
            var expected = new HttpRequest
                               {
                                   RequestLine = "GET / HTTP/1.1"
                               };
            HttpRequest actual = "GET / HTTP/1.1";

            Assert.Equal(expected, actual);
        }
Пример #3
0
        public void prop_AbsoluteUri_getFromRelative()
        {
            var expected = new Uri("http://www.example.com/path");

            var obj = new HttpRequest
                          {
                              RequestLine = "GET /path HTTP/1.1", 
                              Headers =
                                  {
                                      (HttpHeader)"Host: www.example.com"
                                  }
                          };

            var actual = obj.AbsoluteUri;

            Assert.Equal(expected, actual);
        }
Пример #4
0
        public void prop_AbsoluteUri_get()
        {
            var expected = new Uri("http://www.example.com/path");

            var obj = new HttpRequest
                          {
                              RequestLine = new RequestLine("GET", expected.AbsoluteUri, "HTTP/1.1")
                          };

            var actual = obj.AbsoluteUri;

            Assert.Equal(expected, actual);
        }
Пример #5
0
        public void op_Read_TextReader_whenPost()
        {
            var request = new HttpRequest();
            RequestLine requestLine = "POST / HTTP/1.1";
            HttpHeader contentLength = "Content-Length: 4";
            HttpHeader contentType = "Content-Type: text/plain; charset=UTF-8";
            HttpHeader host = "Host: www.example.com";
            HttpHeader connection = "Connection: keep-alive";

            try
            {
                var locator = new Mock<IServiceLocator>();
                locator
                    .Setup(e => e.GetInstance<IMediaType>("text/plain"))
                    .Returns(new TextPlain())
                    .Verifiable();

                ServiceLocator.SetLocatorProvider(() => locator.Object);

                using (var stream = new MemoryStream())
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(requestLine);
                        writer.WriteLine(contentLength);
                        writer.WriteLine(contentType);
                        writer.WriteLine(host);
                        writer.WriteLine(connection);
                        writer.WriteLine(string.Empty);
                        writer.Write("text");
                        writer.Flush();
                        stream.Position = 0;
                        using (var reader = new StreamReader(stream))
                        {
                            request.Read(reader);
                        }
                    }
                }

                locator.VerifyAll();
            }
            finally
            {
                ServiceLocator.SetLocatorProvider(null);
            }

            Assert.Equal(requestLine, request.RequestLine);

            Assert.Equal(4, request.Headers.Count);
            Assert.True(request.Headers.Contains(contentLength));
            Assert.True(request.Headers.Contains(contentType));
            Assert.True(request.Headers.Contains(host));
            Assert.True(request.Headers.Contains(connection));

            Assert.Equal("text", ((TextPlain)request.Body).Value);
        }
Пример #6
0
        public void op_Read_TextReader_whenGet()
        {
            var request = new HttpRequest();
            RequestLine requestLine = "GET / HTTP/1.1";
            HttpHeader host = "Host: www.example.com";
            HttpHeader connection = "Connection: close";

            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.WriteLine(requestLine);
                    writer.WriteLine(host);
                    writer.WriteLine(connection);
                    writer.WriteLine(string.Empty);
                    writer.WriteLine("body");
                    writer.Flush();
                    stream.Position = 0;
                    using (var reader = new StreamReader(stream))
                    {
                        request.Read(reader);
                    }
                }
            }

            Assert.Equal(requestLine, request.RequestLine);

            Assert.Equal(2, request.Headers.Count);
            Assert.True(request.Headers.Contains(host));
            Assert.True(request.Headers.Contains(connection));

            Assert.Null(request.Body);
        }
 public void usage(HttpRequest request)
 {
     Assert.NotNull(request);
 }