Skip to content

Commit

Permalink
Merge pull request #504 from microsoft/andrueastman/fixContentReading
Browse files Browse the repository at this point in the history
Fixed response body inspection
  • Loading branch information
andrueastman authored Jan 7, 2025
2 parents e88a174 + 7ce320d commit 9886fcc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.16.2] - 2024-01-07

- Fixed inspecting of response body when response http content is not buffered. [#501](https://github.com/microsoft/kiota-dotnet/issues/501)
- Fixed a misalignment in return nullability for IParseNode GetObjectValue. [#429](https://github.com/microsoft/kiota-dotnet/issues/429)

## [1.16.1] - 2024-12-18
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- Common default project properties for ALL projects-->
<PropertyGroup>
<VersionPrefix>1.16.1</VersionPrefix>
<VersionPrefix>1.16.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
<!-- This is overidden in test projects by setting to true-->
<IsTestProject>false</IsTestProject>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ItemGroup
Condition="'$(TargetFramework)' == 'net5.0' or '$(TargetFramework)'== 'netStandard2.0' or '$(TargetFramework)' == 'netStandard2.1' or '$(TargetFramework)' == 'net462'">
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="[6.0,)" />
<PackageReference Include="System.Text.Json" Version="[6.0,)" />
<PackageReference Include="System.Text.Json" Version="[6.0.10,)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
Expand Down
1 change: 1 addition & 0 deletions src/http/httpClient/Middleware/BodyInspectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ CancellationToken cancellationToken
}

var stream = new MemoryStream();
await httpContent.LoadIntoBufferAsync().ConfigureAwait(false);

#if NET5_0_OR_GREATER
await httpContent.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- NET 5 target to be removed on next major version-->
<ItemGroup
Condition="'$(TargetFramework)' == 'net5.0' or '$(TargetFramework)'== 'netStandard2.0' or '$(TargetFramework)' == 'netStandard2.1'">
<PackageReference Include="System.Text.Json" Version="[6.0,)" />
<PackageReference Include="System.Text.Json" Version="[6.0.10,)" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 32 additions & 0 deletions tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Text.Json;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;
using Microsoft.Kiota.Http.HttpClientLibrary.Tests.Mocks;
Expand Down Expand Up @@ -92,6 +93,37 @@ public async Task BodyInspectionHandlerGetsResponseBodyStream()
Assert.Equal("response test", await response.Content.ReadAsStringAsync()); // response from option is separate from "normal" response stream
}

[Fact(Skip = "Test can potentially be flaky due to usage limitations on Github. Enable to verify.")]
public async Task BodyInspectionHandlerGetsResponseBodyStreamFromGithub()
{
var option = new BodyInspectionHandlerOption { InspectResponseBody = true, InspectRequestBody = true };
var httpClient = KiotaClientFactory.Create(optionsForHandlers: [option]);

// When
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/microsoft/kiota-dotnet");
var response = await httpClient.SendAsync(request);

// Then
if(response.IsSuccessStatusCode)
{
Assert.NotEqual(Stream.Null, option.ResponseBody);
var jsonFromInspection = await JsonDocument.ParseAsync(option.ResponseBody);
var jsonFromContent = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
Assert.True(jsonFromInspection.RootElement.TryGetProperty("owner", out _));
Assert.True(jsonFromContent.RootElement.TryGetProperty("owner", out _));
}
else if((int)response.StatusCode is 429 or 403)
{
// We've been throttled according to the docs below. No need to fail for now.
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#primary-rate-limit-for-unauthenticated-users
Assert.Fail("Request was throttled");
}
else
{
Assert.Fail("Unexpected response status code in BodyInspectionHandler test");
}
}

[Fact]
public async Task BodyInspectionHandlerGetsNullResponseBodyStreamWhenThereIsNoResponseBody()
{
Expand Down

0 comments on commit 9886fcc

Please sign in to comment.