diff --git a/src/http/httpClient/Middleware/BodyInspectionHandler.cs b/src/http/httpClient/Middleware/BodyInspectionHandler.cs index 6a8591b..59b193c 100644 --- a/src/http/httpClient/Middleware/BodyInspectionHandler.cs +++ b/src/http/httpClient/Middleware/BodyInspectionHandler.cs @@ -60,12 +60,14 @@ CancellationToken cancellationToken { if(options.InspectRequestBody) { - options.RequestBody = await CopyToStreamAsync(request.Content, cancellationToken).ConfigureAwait(false); + options.RequestBody = await CopyToStreamAsync(request.Content, cancellationToken) + .ConfigureAwait(false); } var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); if(options.InspectResponseBody) { - options.ResponseBody = await CopyToStreamAsync(response.Content, cancellationToken).ConfigureAwait(false); + options.ResponseBody = await CopyToStreamAsync(response.Content, cancellationToken) + .ConfigureAwait(false); } return response; @@ -78,14 +80,12 @@ CancellationToken cancellationToken #if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER [return: NotNullIfNotNull(nameof(httpContent))] #endif - static async Task CopyToStreamAsync(HttpContent? httpContent, CancellationToken cancellationToken) + static async Task CopyToStreamAsync( + HttpContent? httpContent, + CancellationToken cancellationToken + ) { - if(httpContent == null) - { - return null; - } - - if(httpContent.Headers.ContentLength == 0) + if(httpContent is null or { Headers.ContentLength: 0 }) { return Stream.Null; } @@ -98,7 +98,6 @@ CancellationToken cancellationToken await httpContent.CopyToAsync(stream).ConfigureAwait(false); #endif - if(stream.CanSeek) { stream.Position = 0; diff --git a/src/http/httpClient/Middleware/Options/BodyInspectionHandlerOption.cs b/src/http/httpClient/Middleware/Options/BodyInspectionHandlerOption.cs index 3cc8181..0afe71e 100644 --- a/src/http/httpClient/Middleware/Options/BodyInspectionHandlerOption.cs +++ b/src/http/httpClient/Middleware/Options/BodyInspectionHandlerOption.cs @@ -26,19 +26,19 @@ public class BodyInspectionHandlerOption : IRequestOption /// /// Gets the request body stream for the current request. This stream is available - /// only if InspectRequestBody is set to true and the request contains a body. - /// This stream is not disposed of by kiota, you need to take care of that. + /// only if InspectRequestBody is set to true and the request contains a body. Otherwise, + /// it's just Stream.Null. This stream is not disposed of by kiota, you need to take care of that. /// Note that this stream is a copy of the original request body stream, which has /// impact on memory usage. Use adequately. /// - public Stream? RequestBody { get; internal set; } + public Stream RequestBody { get; internal set; } = Stream.Null; /// /// Gets the response body stream for the current request. This stream is available - /// only if InspectResponseBody is set to true. - /// This stream is not disposed of by kiota, you need to take care of that. + /// only if InspectResponseBody is set to true and the response contains a body. Otherwise, + /// it's just Stream.Null. This stream is not disposed of by kiota, you need to take care of that. /// Note that this stream is a copy of the original request body stream, which has /// impact on memory usage. Use adequately. /// - public Stream? ResponseBody { get; internal set; } + public Stream ResponseBody { get; internal set; } = Stream.Null; } diff --git a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs index 093fa3d..176d8a6 100644 --- a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs +++ b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs @@ -31,7 +31,6 @@ public async Task BodyInspectionHandlerGetsRequestBodyStream() var response = await invoker.SendAsync(request, default); // Then - Assert.NotNull(option.RequestBody); Assert.Equal("request test", GetStringFromStream(option.RequestBody!)); Assert.Equal("request test", await request.Content.ReadAsStringAsync()); // response from option is separate from "normal" request stream } @@ -60,7 +59,6 @@ public async Task BodyInspectionHandlerGetsRequestBodyStreamWhenRequestIsOctetSt var response = await invoker.SendAsync(request, default); // Then - Assert.NotNull(option.RequestBody); Assert.Equal("request test", GetStringFromStream(option.RequestBody!)); Assert.Equal("request test", await request.Content.ReadAsStringAsync()); // response from option is separate from "normal" request stream } @@ -76,7 +74,7 @@ public async Task BodyInspectionHandlerGetsNullRequestBodyStreamWhenThereIsNoReq var response = await invoker.SendAsync(request, default); // Then - Assert.Null(option.RequestBody); + Assert.Same(Stream.Null, option.RequestBody); } [Fact] @@ -90,13 +88,12 @@ public async Task BodyInspectionHandlerGetsResponseBodyStream() var response = await invoker.SendAsync(request, default); // Then - Assert.NotNull(option.ResponseBody); Assert.Equal("response test", GetStringFromStream(option.ResponseBody!)); Assert.Equal("response test", await response.Content.ReadAsStringAsync()); // response from option is separate from "normal" response stream } [Fact] - public async Task BodyInspectionHandlerGetsEmptyResponseBodyStreamWhenThereIsNoResponseBody() + public async Task BodyInspectionHandlerGetsNullResponseBodyStreamWhenThereIsNoResponseBody() { var option = new BodyInspectionHandlerOption { InspectResponseBody = true, }; using var invoker = GetMessageInvoker(new HttpResponseMessage(), option); @@ -106,8 +103,7 @@ public async Task BodyInspectionHandlerGetsEmptyResponseBodyStreamWhenThereIsNoR var response = await invoker.SendAsync(request, default); // Then - Assert.NotNull(option.ResponseBody); - Assert.Equal(string.Empty, GetStringFromStream(option.ResponseBody!)); + Assert.Same(Stream.Null, option.ResponseBody); } private static HttpResponseMessage CreateHttpResponseWithBody() =>