public async Task OriginalContentStreamIsReturnedIfNotAsync() { var content = new AsyncContent() { MemoryStream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }) }; var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = content }; var mockHandler = new MockHttpClientHandler(httpRequestMessage => Task.FromResult(httpResponseMessage)); var transport = new HttpClientTransport(new HttpClient(mockHandler)); Request request = transport.CreateRequest(); request.Method = RequestMethod.Get; request.Uri.Reset(new Uri("https://example.com:340")); Response response = await ExecuteRequest(request, transport); content.CreateContentReadStreamAsyncCompletionSource.SetResult(null); Stream stream = response.ContentStream; Assert.AreSame(content.MemoryStream, stream); }
public void Should_close_source_stream() { var source = new MemoryStream(Encoding.UTF8.GetBytes("fark")); var content = new AsyncContent(source, 1024); content.ReadAsStringAsync().Result.ShouldEqual("fark"); source.CanRead.ShouldBeFalse(); }
public async Task ContentStreamIsReturnedSynchronously() { var content = new AsyncContent() { MemoryStream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }) }; var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = content }; var mockHandler = new MockHttpClientHandler(httpRequestMessage => Task.FromResult(httpResponseMessage)); var transport = new HttpClientTransport(new HttpClient(mockHandler)); Request request = transport.CreateRequest(); request.Method = RequestMethod.Get; request.Uri.Reset(new Uri("https://example.com:340")); Response response = await ExecuteRequest(request, transport); byte[] data = new byte[5]; Stream stream = response.ContentStream; Task <int> firstRead = stream.ReadAsync(data, 0, 5); Assert.False(firstRead.IsCompleted); content.CreateContentReadStreamAsyncCompletionSource.SetResult(null); Assert.AreEqual(5, await firstRead); // Exercise stream features stream.Position = 3; stream.Seek(-1, SeekOrigin.Current); var secondReadLength = await stream.ReadAsync(data, 0, 5); Assert.AreEqual(3, secondReadLength); Assert.False(stream.CanWrite); Assert.True(stream.CanSeek); Assert.True(stream.CanRead); Assert.Throws <NotSupportedException>(() => stream.Write(null, 0, 0)); Assert.Throws <NotSupportedException>(() => stream.SetLength(5)); }