internal Response <Stream> Add(string id, Stream twin, CreateOrReplaceDigitalTwinOptions digitalTwinsAddOptions = null, CancellationToken cancellationToken = default) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (twin == null) { throw new ArgumentNullException(nameof(twin)); } using HttpMessage message = CreateAddRequest(id, twin, digitalTwinsAddOptions); _pipeline.Send(message, cancellationToken); switch (message.Response.Status) { case 200: { Stream value = message.ExtractResponseContent(); return(Response.FromValue(value, message.Response)); } case 202: return(Response.FromValue <Stream>(null, message.Response)); default: throw _clientDiagnostics.CreateRequestFailedException(message.Response); } }
private Response <Stream> StartDownload(Uri sourceEndpoint, HttpRange range = default, long startOffset = 0, CancellationToken cancellationToken = default) { HttpRange?pageRange = null; if (range != default || startOffset != 0) { pageRange = new HttpRange( range.Offset + startOffset, range.Length.HasValue ? range.Length.Value - startOffset : null); } HttpMessage message = GetHttpMessage(sourceEndpoint, pageRange); _client._pipeline.Send(message, cancellationToken); switch (message.Response.Status) { case 200: case 206: { Stream value = message.ExtractResponseContent(); return(Response.FromValue(value, message.Response)); } default: throw _client._clientDiagnostics.CreateRequestFailedException(message.Response); } }
internal async Task <Response <Stream> > AddRelationshipAsync( string id, string relationshipId, Stream relationship, CreateOrReplaceRelationshipOptions digitalTwinsAddRelationshipOptions = null, CancellationToken cancellationToken = default) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (relationshipId == null) { throw new ArgumentNullException(nameof(relationshipId)); } if (relationship == null) { throw new ArgumentNullException(nameof(relationship)); } using HttpMessage message = CreateAddRelationshipRequest(id, relationshipId, relationship, digitalTwinsAddRelationshipOptions); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: { Stream value = message.ExtractResponseContent(); return(Response.FromValue(value, message.Response)); } default: throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); } }
public void PreserveReturnsResponseStream() { var mockStream = new Mock <Stream>(); var response = new MockResponse(200); response.ContentStream = mockStream.Object; HttpMessage message = new HttpMessage(new MockRequest(), new ResponseClassifier()); message.Response = response; Stream stream = message.ExtractResponseContent(); Stream stream2 = message.ExtractResponseContent(); Assert.AreSame(mockStream.Object, stream); Assert.AreSame(stream2, stream); }
public async Task NonBufferedFailedResponsesAreDisposedOf() { byte[] buffer = { 0 }; var clientOptions = new TestOptions(); clientOptions.Retry.Delay = TimeSpan.FromMilliseconds(2); clientOptions.Retry.NetworkTimeout = TimeSpan.FromSeconds(5); HttpPipeline httpPipeline = HttpPipelineBuilder.Build(clientOptions); int bodySize = 1000; int reqNum = 0; using TestServer testServer = new TestServer( async context => { if (Interlocked.Increment(ref reqNum) % 2 == 0) { // Respond with 503 to every other request to force a retry context.Response.StatusCode = 503; } for (int i = 0; i < bodySize; i++) { await context.Response.Body.WriteAsync(buffer, 0, 1); } }); // Make sure we dispose things correctly and not exhaust the connection pool var requestCount = 100; for (int i = 0; i < requestCount; i++) { Stream extractedStream; using (HttpMessage message = httpPipeline.CreateMessage()) { message.Request.Uri.Reset(testServer.Address); message.BufferResponse = false; await ExecuteRequest(message, httpPipeline); Assert.AreEqual(message.Response.ContentStream.CanSeek, false); Assert.Throws <InvalidOperationException>(() => { var content = message.Response.Content; }); extractedStream = message.ExtractResponseContent(); } var memoryStream = new MemoryStream(); await extractedStream.CopyToAsync(memoryStream); Assert.AreEqual(memoryStream.Length, bodySize); extractedStream.Dispose(); } Assert.Greater(reqNum, requestCount); }
public void PreserveReturnsNullWhenContentIsNull() { var response = new MockResponse(200); response.ContentStream = null; HttpMessage message = new HttpMessage(new MockRequest(), new ResponseClassifier()); message.Response = response; Stream stream = message.ExtractResponseContent(); Assert.AreSame(null, stream); }
public void PreserveSetsResponseContentToThrowingStream() { var mockStream = new Mock <Stream>(); var response = new MockResponse(200); response.ContentStream = mockStream.Object; HttpMessage message = new HttpMessage(new MockRequest(), new ResponseClassifier()); message.Response = response; Stream stream = message.ExtractResponseContent(); Assert.AreSame(mockStream.Object, stream); InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => response.ContentStream.Read(Array.Empty <byte>(), 0, 0)); Assert.AreEqual("The operation has called ExtractResponseContent and will provide the stream as part of its response type.", exception.Message); }
public void ContentPropertyThrowsResponseIsExtracted() { var memoryStream = new MemoryStream(); var response = new MockResponse(200); response.ContentStream = memoryStream; HttpMessage message = new HttpMessage(new MockRequest(), new ResponseClassifier()); message.Response = response; Assert.AreEqual(memoryStream.ToArray(), message.Response.Content.ToArray()); Stream stream = message.ExtractResponseContent(); Assert.AreSame(memoryStream, stream); Assert.Throws <InvalidOperationException>(() => { var x = response.Content; }); }
internal async Task <Response <Stream> > AddAsync( string id, Stream twin, CreateOrReplaceDigitalTwinOptions digitalTwinsAddOptions = null, CancellationToken cancellationToken = default) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (twin == null) { throw new ArgumentNullException(nameof(twin)); } using DiagnosticScope scope = _clientDiagnostics.CreateScope("DigitalTwinsClient.Add"); scope.Start(); try { using HttpMessage message = CreateAddRequest(id, twin, digitalTwinsAddOptions); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: { Stream value = message.ExtractResponseContent(); return(Response.FromValue(value, message.Response)); } case 202: return(Response.FromValue <Stream>(null, message.Response)); default: throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); } } catch (Exception e) { scope.Failed(e); throw; } }
public async Task NonBufferedExtractedStreamReadableAfterMessageDisposed() { byte[] buffer = { 0 }; HttpPipeline httpPipeline = HttpPipelineBuilder.Build(GetOptions()); TaskCompletionSource <object> blockRequestTsc = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously); using TestServer testServer = new TestServer( async context => { for (int i = 0; i < 1000; i++) { await context.Response.Body.WriteAsync(buffer, 0, 1); } }); // Make sure we dispose things correctly and not exhaust the connection pool for (int i = 0; i < 100; i++) { Stream extractedStream; using (HttpMessage message = httpPipeline.CreateMessage()) { message.Request.Uri.Reset(testServer.Address); message.BufferResponse = false; await ExecuteRequest(message, httpPipeline); Assert.False(message.Response.ContentStream.CanSeek); Assert.Throws <InvalidOperationException>(() => { var content = message.Response.Content; }); extractedStream = message.ExtractResponseContent(); } var memoryStream = new MemoryStream(); await extractedStream.CopyToAsync(memoryStream); Assert.AreEqual(1000, memoryStream.Length); extractedStream.Dispose(); } }
public async Task NonBufferedExtractedStreamReadableAfterMessageDisposed() { byte[] buffer = { 0 }; HttpPipeline httpPipeline = HttpPipelineBuilder.Build(GetOptions()); using TestServer testServer = new TestServer( async context => { for (int i = 0; i < 1000; i++) { await context.Response.Body.WriteAsync(buffer, 0, 1); } }); // Make sure we dispose things correctly and not exhaust the connection pool for (int i = 0; i < 100; i++) { Stream extractedStream; using (HttpMessage message = httpPipeline.CreateMessage()) { message.Request.Uri.Reset(testServer.Address); message.BufferResponse = false; await ExecuteRequest(message, httpPipeline); Assert.AreEqual(message.Response.ContentStream.CanSeek, false); extractedStream = message.ExtractResponseContent(); } var memoryStream = new MemoryStream(); await extractedStream.CopyToAsync(memoryStream); Assert.AreEqual(memoryStream.Length, 1000); extractedStream.Dispose(); } }