Ejemplo n.º 1
0
        public async Task TestDeserializeResponse_InvalidHeaderSeparator_ShouldThrow()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length=5\r\n\r\nMessage is longer");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            await TestAssert.ThrowsAsync <HttpRequestException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default));
        }
        public void TestDeserializeResponse_InvalidEndOfHeaders_ShouldThrow()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length: 5\r\n");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            TestAssert.ThrowsAsync <IOException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default)).Wait();
        }
        public void TestDeserializeResponse_InvalidContentLength_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length: 5\r\n\r\nMessage is longer");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            TestAssert.ThrowsAsync <HttpRequestException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken)).Wait();
        }
        public void TestDeserializeResponse_MissingReasonPhrase_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200\r\n");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            TestAssert.ThrowsAsync <HttpRequestException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken)).Wait();
        }
        public void TestDeserializeResponse_InvalidEndOfStream_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("invalid");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            TestAssert.ThrowsAsync <IOException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken)).Wait();
        }
Ejemplo n.º 6
0
        public async Task TestDeserializeResponse_InvalidEndOfStatusMessage_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK \r\n");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            await TestAssert.ThrowsAsync <IOException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken));
        }
        public async Task TestDeserializeResponse_StatusLine_ShouldDeserialize()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\n\r\n");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            HttpResponseMessage response = await HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken);

            Assert.AreEqual(response.Version, Version.Parse("1.1"));
            Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.OK);
            Assert.AreEqual(response.ReasonPhrase, "OK");
        }
        public async Task TestDeserializeResponse_ValidContent_ShouldDeserialize()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length: 4\r\n\r\nTest");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            var response = await HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default);

            Assert.AreEqual(response.Version, Version.Parse("1.1"));
            Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.OK);
            Assert.AreEqual(response.ReasonPhrase, "OK");
#if !NETCOREAPP1_1
            Assert.AreEqual(response.Content.Headers.ContentLength, 4);
#endif
            Assert.AreEqual(await response.Content.ReadAsStringAsync(), "Test");
        }
        public async Task TestDeserializeResponse_InvalidHeader_ShouldDeserialize()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nTest-header: 4\r\n\r\nTest");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            HttpResponseMessage response = await HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default).ConfigureAwait(false);

            Assert.AreEqual(Version.Parse("1.1"), response.Version);
            Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("OK", response.ReasonPhrase);
#if !NETCOREAPP1_1
            Assert.AreEqual(4, response.Content.Headers.ContentLength);
#endif
            Assert.AreEqual("Test", await response.Content.ReadAsStringAsync());
        }