示例#1
0
        public void Should_read_response_body_without_content_length()
        {
            var content = ThreadSafeRandom.NextBytes(500 * Constants.Kilobytes);

            using (var server = TestServer.StartNew(
                       ctx =>
            {
                ctx.Response.StatusCode = 200;
                ctx.Response.OutputStream.Write(content, 0, content.Length);
            }))
            {
                var response = Send(Request.Put(server.Url));

                response.Content.ToArraySegment().Should().Equal(content);
            }
        }
        public void Should_read_response_body_greater_than_64k_with_non_successful_code()
        {
            var content = ThreadSafeRandom.NextBytes(100.Kilobytes());

            using (var server = TestServer.StartNew(
                       ctx =>
            {
                ctx.Response.StatusCode = 409;
                ctx.Response.OutputStream.Write(content, 0, content.Length);
            }))
            {
                var response = Send(Request.Put(server.Url));

                response.Code.Should().Be(ResponseCode.Conflict);
                response.Content.ToArraySegment().Should().Equal(content);
            }
        }
        public void Should_be_able_to_receive_content_of_given_size(int size)
        {
            var content = ThreadSafeRandom.NextBytes(size.Bytes());

            using (var server = TestServer.StartNew(
                       ctx =>
            {
                ctx.Response.StatusCode = 200;
                ctx.Response.ContentLength64 = content.Length;
                ctx.Response.OutputStream.Write(content, 0, content.Length);
            }))
            {
                var response = Send(Request.Put(server.Url));

                response.Content.ToArraySegment().Should().Equal(content);
            }
        }
        public void Should_return_http_517_when_response_body_size_is_larger_than_configured_limit_when_content_length_is_unknown()
        {
            settings.MaxResponseBodySize = 1024;

            var content = ThreadSafeRandom.NextBytes(settings.MaxResponseBodySize.Value + 1);

            using (var server = TestServer.StartNew(
                       ctx =>
            {
                ctx.Response.StatusCode = 200;
                ctx.Response.OutputStream.Write(content, 0, content.Length);
            }))
            {
                var response = Send(Request.Put(server.Url));

                response.Code.Should().Be(ResponseCode.InsufficientStorage);
                response.Content.Length.Should().Be(0);
            }
        }
示例#5
0
        public void Should_be_able_to_send_stream_content_of_given_size_with_known_length(int size)
        {
            using (var server = TestServer.StartNew(ctx => ctx.Response.StatusCode = 200))
            {
                var content = ThreadSafeRandom.NextBytes(size);

                var contentStream = new MemoryStream();

                contentStream.Write(content, 0, content.Length);

                var guid = Guid.NewGuid().ToByteArray();
                contentStream.Write(guid, 0, guid.Length);
                contentStream.Position = 0;

                var request = Request.Put(server.Url).WithContent(contentStream, size);

                Send(request);

                server.LastRequest.Body.Should().Equal(content);
            }
        }
示例#6
0
        public virtual void Should_return_response_with_correct_content_length_when_buffer_factory_is_overridden()
        {
            SetSettings(s => s.BufferFactory = size => new byte[size * 2]);
            var content = ThreadSafeRandom.NextBytes(1234);

            using (var server = TestServer.StartNew(
                       ctx =>
            {
                ctx.Response.StatusCode = 200;
                ctx.Response.ContentLength64 = content.Length;
                ctx.Response.OutputStream.Write(content, 0, content.Length);
            }))
            {
                var response = Send(Request.Put(server.Url));

                response.Code.Should().Be(ResponseCode.Ok);
                response.Content.Buffer.Length.Should().Be(2468);
                response.Content.Offset.Should().Be(0);
                response.Content.Length.Should().Be(1234);
            }
        }
示例#7
0
        protected static Task <bool>[] InsertAndReadStreamsAndCheckMatch(RelationalStorageForTesting sut, int streamSize, int countOfStreams, CancellationToken cancellationToken)
        {
            Skip.If(sut == null, "Database was not initialized correctly");

            //Stream in and steam out three binary streams in parallel.
            var streamChecks = new Task <bool> [countOfStreams];

            for (int i = 0; i < countOfStreams; ++i)
            {
                int streamId = i;
                streamChecks[i] = Task.Run(async() =>
                {
                    var rb = new byte[streamSize];
                    ThreadSafeRandom.NextBytes(rb);
                    await InsertIntoDatabaseUsingStream(sut, streamId, rb, cancellationToken);
                    var dataStreamFromTheDb = await ReadFromDatabaseUsingAsyncStream(sut, streamId, cancellationToken);
                    return(dataStreamFromTheDb.StreamData.SequenceEqual(rb));
                });
            }

            return(streamChecks);
        }
        public void Should_be_able_to_transform_response_to_clear_response_without_partial_received_headers_and_body_if_network_error_happens()
        {
            const string customHeader = "MyCustomHeader";
            var          content      = ThreadSafeRandom.NextBytes(1024);

            using (var server = TestServer.StartNew(
                       ctx =>
            {
                ctx.Response.StatusCode = 200;
                ctx.Response.ContentLength64 = content.Length;
                ctx.Response.Headers.Add(customHeader, "HeaderValue");
                ctx.Response.OutputStream.Write(content, 0, content.Length / 2);
                ctx.Response.Abort();
            }))
            {
                var response = Send(Request.Get(server.Url));

                response.Code.Should().Be(ResponseCode.ReceiveFailure);
                response.Headers.Any(h => h.Name == customHeader).Should().Be(false);
                response.HasContent.Should().BeFalse();
                response.HasStream.Should().BeFalse();
            }
        }
        public void Should_return_SendFailure_code_when_socket_closed_while_sending_large_body()
        {
            if (!RuntimeDetector.IsDotNetCore21AndNewer)
            {
                return;
            }

            var contentLargerThanTcpPacket = ThreadSafeRandom.NextBytes(64 * 1024 + 10);

            using (var stream = new BlockingStream(contentLargerThanTcpPacket))
            {
                using (var server = SocketTestServer.StartNew("",
                                                              // ReSharper disable once AccessToDisposedClosure
                                                              onBeforeRequestReading: c => CloseConnectionAndUnblockStream(c, stream)))
                {
                    var request = Request
                                  .Put(server.Url)
                                  .WithContent(stream);
                    var response = Send(request);

                    response.Code.Should().Be(ResponseCode.SendFailure);
                }
            }
        }
示例#10
0
 // From http://stackoverflow.com/a/6651661/29995
 public static long Long()
 {
     byte[] buffer = new byte[8];
     ThreadSafeRandom.NextBytes(buffer);
     return(BitConverter.ToInt64(buffer, 0));
 }
        public void Should_correctly_read_and_write_random_byte_arrays()
        {
            var item = ThreadSafeRandom.NextBytes(ThreadSafeRandom.Next(100));

            Test(item, (value, writer) => writer.Write(value), reader => reader.ReadByteArray());
        }
 public void Should_correctly_read_and_write_random_strings()
 {
     Test(Encoding.UTF8.GetString(ThreadSafeRandom.NextBytes(4.Kilobytes())), (value, writer) => writer.Write(value), reader => reader.ReadString());
 }