Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed response body inspection #504

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
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>
Original file line number Diff line number Diff line change
@@ -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'">
1 change: 1 addition & 0 deletions src/http/httpClient/Middleware/BodyInspectionHandler.cs
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@
#endif
static async Task<Stream> CopyToStreamAsync(
HttpContent? httpContent,
CancellationToken cancellationToken

Check warning on line 85 in src/http/httpClient/Middleware/BodyInspectionHandler.cs

GitHub Actions / Build

Remove this unused method parameter 'cancellationToken'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 85 in src/http/httpClient/Middleware/BodyInspectionHandler.cs

GitHub Actions / Build

Remove this unused method parameter 'cancellationToken'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 85 in src/http/httpClient/Middleware/BodyInspectionHandler.cs

GitHub Actions / Build

Remove this unused method parameter 'cancellationToken'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 85 in src/http/httpClient/Middleware/BodyInspectionHandler.cs

GitHub Actions / Build

Remove this unused method parameter 'cancellationToken'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 85 in src/http/httpClient/Middleware/BodyInspectionHandler.cs

GitHub Actions / Build

Remove this unused method parameter 'cancellationToken'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 85 in src/http/httpClient/Middleware/BodyInspectionHandler.cs

GitHub Actions / Build

Remove this unused method parameter 'cancellationToken'. (https://rules.sonarsource.com/csharp/RSPEC-1172)
)
{
if(httpContent is null or { Headers.ContentLength: 0 })
@@ -91,6 +91,7 @@
}

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

#if NET5_0_OR_GREATER
await httpContent.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);
Original file line number Diff line number Diff line change
@@ -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>
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;
@@ -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()
{