public async Task Limits_Asynchronously() { // ARRANGE var innerStream = new MemoryStream(Encoding.ASCII.GetBytes("foobar")); var stream = new LimitedStream(innerStream, 3); // ACT var actual = await new StreamReader(stream, Encoding.ASCII).ReadToEndAsync(); // ASSERT actual.Should().Be("foo"); }
private void ReadResponseBody(ByteStreamReader reader, HttpResponseMessage response) { HttpContent content = null; if (response.Headers.TransferEncodingChunked.GetValueOrDefault(false)) { // read the body with chunked transfer encoding var remainingStream = reader.GetRemainingStream(); var chunkedStream = new ChunkedStream(remainingStream); content = new StreamContent(chunkedStream); } else if (response.Content.Headers.ContentLength.HasValue) { // read the body with a content-length var remainingStream = reader.GetRemainingStream(); var limitedStream = new LimitedStream(remainingStream, response.Content.Headers.ContentLength.Value); content = new StreamContent(limitedStream); } if (content != null) { // copy over the content headers foreach (var header in response.Content.Headers) { content.Headers.TryAddWithoutValidation(header.Key, header.Value); } response.Content = content; } }