Exemple #1
0
        public static HttpResponse FromString(string value)
        {
            if (null == value)
            {
                throw new ArgumentNullException("value");
            }

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

            var result = new HttpResponse();

            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;
        }
        public void opImplicit_HttpResponse_string()
        {
            var expected = new HttpResponse
                               {
                                   StatusLine = new StatusLine("HTTP/1.1", 200, "OK")
                               };
            HttpResponse actual = "HTTP/1.1 200 OK";

            Assert.Equal(expected, actual);
        }
Exemple #3
0
        private static IHttpResponse Read(Stream stream)
        {
            var result = new HttpResponse();

            using (var reader = new StreamReader(stream))
            {
                result.Read(reader);
            }

            return result;
        }
        public void op_Read_TextReader_when404()
        {
            var response = new HttpResponse();
            StatusLine statusLine = "HTTP/1.1 404 Not Found";
            HttpHeader contentLength = "Content-Length: 4";
            HttpHeader contentType = "Content-Type: text/plain; charset=UTF-8";

            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(statusLine);
                        writer.WriteLine(contentLength);
                        writer.WriteLine(contentType);
                        writer.WriteLine(string.Empty);
                        writer.Write("text");
                        writer.Flush();
                        stream.Position = 0;
                        using (var reader = new StreamReader(stream))
                        {
                            response.Read(reader);
                        }
                    }
                }

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

            Assert.Equal(statusLine, response.StatusLine);

            Assert.Equal(2, response.Headers.Count);
            Assert.True(response.Headers.Contains(contentLength));
            Assert.True(response.Headers.Contains(contentType));

            Assert.Equal("text", ((TextPlain)response.Body).Value);
        }