コード例 #1
0
        public async Task WorksWhenReadAheadBufferIsNotUsed(int testDataSize)
        {
            var testData = MockData.MockNetworkStream(testDataSize);
            var inst     = new RollbackableStream(
                testData,
                TcpSettings.Default
                );
            var result = await ReadResult(testDataSize, inst);

            Assert.True(testData.ToArray().SequenceEqual(result.ToArray()));
        }
コード例 #2
0
        public void ReadMethodWontReadPastContentLength(int contentLength)
        {
            var mockDataStream = MockData.MockNetworkStream(1024 * 12);
            var inst           = new HttpFixedLengthRequestBody(mockDataStream, contentLength);
            var result         = new MemoryStream();

            inst.CopyTo(result, 1024);

            mockDataStream.SetLength(contentLength);
            Assert.Equal(contentLength, result.Length);
            Assert.True(result.ToArray().SequenceEqual(mockDataStream.ToArray()));
        }
コード例 #3
0
        public async Task WorksWhenRollingBack(int bytesReadBeforeRollingback, params int[] rollbackBytes)
        {
            var testData = MockData.MockNetworkStream(4096);
            var inst     = new RollbackableStream(
                testData,
                TcpSettings.Default
                );
            var buffer = new byte[testData.Length];

            // Read minimum 4 bytes
            int bytesRead = 0;

            while (bytesRead < bytesReadBeforeRollingback)
            {
                bytesRead += await inst.ReadAsync(
                    buffer,
                    bytesRead,
                    (int)testData.Length - bytesRead);
            }

            // Rollback
            foreach (var bytesToRollback in rollbackBytes)
            {
                bytesRead -= bytesToRollback;
                inst.TryRollbackFromIndex(
                    src: buffer,
                    srcLength: bytesRead + bytesToRollback,
                    startIndex: bytesRead
                    );
            }

            // Read the rest
            while (bytesRead < testData.Length)
            {
                var t = await inst.ReadAsync(buffer, bytesRead, (int)testData.Length - bytesRead);

                if (t == 0)
                {
                    break;
                }
                bytesRead += t;
            }

            Assert.Equal(testData.Length, bytesRead);
            Assert.True(testData.ToArray().SequenceEqual(buffer));
        }
コード例 #4
0
        public async Task CompleteReadingRequestBeforeWrittingResponse()
        {
            var body    = MockData.MockNetworkStream(1024 * 16 + 88);
            var request = new Mock <IHttpRequest>();

            request.Setup(inst => inst.Body)
            .Returns(new ReadOnlyStreamAdapter(body));
            request.Setup(inst => inst.Header).Returns(new HttpRequestHeader("POST / HTTP/1.1"));

            var response = new HttpResponse(
                request.Object,
                new MemoryStream(),
                TcpSettings.Default,
                HttpSettings.Default
                );

            await response.SendHeaderAsync();

            Assert.Equal(body.Length, body.Position);
        }
コード例 #5
0
        /// <summary>
        /// Generate chunked data for testing.
        /// </summary>
        public static void Generate(
            int[] chunkLengths,
            Dictionary <string, string> trailers,
            out List <MemoryStream> chunks,
            out byte[] httpMessageBodyBlob)
        {
            // Generate chunks
            chunks = new List <MemoryStream>();
            for (var i = 0; i < chunkLengths.Length; i++)
            {
                chunks.Add(MockData.MockNetworkStream(chunkLengths[i], seed: i));
            }

            // Write body
            var httpMessageBodyStream = new MemoryStream();

            foreach (var chunk in chunks)
            {
                httpMessageBodyStream.Write(chunk.Length.ToString("X", CultureInfo.InvariantCulture));
                httpMessageBodyStream.Write(";some extra attributes = 1133");
                httpMessageBodyStream.Write(NewLine);
                httpMessageBodyStream.Write(chunk.ToArray());
                httpMessageBodyStream.Write(NewLine);
            }

            // Always end with a empty chunk
            httpMessageBodyStream.Write($"0{NewLine}");

            // Write trailer
            foreach (var kv in trailers)
            {
                httpMessageBodyStream.Write($"{kv.Key}: {kv.Value}");
                httpMessageBodyStream.Write(NewLine);
            }

            // End the chunk request body with an empty line
            httpMessageBodyStream.Write(NewLine);
            httpMessageBodyBlob = httpMessageBodyStream.ToArray();
        }