Skip to content

Commit

Permalink
Use Stream.Null instead of null and fix net462 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinjahn committed Dec 12, 2024
1 parent 5ed13f7 commit 6c67482
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
19 changes: 9 additions & 10 deletions src/http/httpClient/Middleware/BodyInspectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -78,14 +80,12 @@ CancellationToken cancellationToken
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
[return: NotNullIfNotNull(nameof(httpContent))]
#endif
static async Task<Stream?> CopyToStreamAsync(HttpContent? httpContent, CancellationToken cancellationToken)
static async Task<Stream> 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;
}
Expand All @@ -98,7 +98,6 @@ CancellationToken cancellationToken
await httpContent.CopyToAsync(stream).ConfigureAwait(false);
#endif


if(stream.CanSeek)
{
stream.Position = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public class BodyInspectionHandlerOption : IRequestOption

/// <summary>
/// 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.
/// </summary>
public Stream? RequestBody { get; internal set; }
public Stream RequestBody { get; internal set; } = Stream.Null;

/// <summary>
/// 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.
/// </summary>
public Stream? ResponseBody { get; internal set; }
public Stream ResponseBody { get; internal set; } = Stream.Null;
}
10 changes: 3 additions & 7 deletions tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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]
Expand All @@ -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);
Expand All @@ -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() =>
Expand Down

0 comments on commit 6c67482

Please sign in to comment.