From 43588136f096f2fcb83966c5edcae065942df909 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 7 Jan 2025 11:02:23 +0300 Subject: [PATCH 1/4] fix: reading of response body content --- CHANGELOG.md | 3 ++ Directory.Build.props | 2 +- ...rosoft.Kiota.Http.HttpClientLibrary.csproj | 2 +- .../Middleware/BodyInspectionHandler.cs | 5 +-- .../Microsoft.Kiota.Serialization.Json.csproj | 2 +- .../Middleware/BodyInspectionHandlerTests.cs | 32 +++++++++++++++++++ 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4146a73..d5dd564 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.16.1] - 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 diff --git a/Directory.Build.props b/Directory.Build.props index f40df4e..c2b3814 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.16.1 + 1.16.2 false diff --git a/src/http/httpClient/Microsoft.Kiota.Http.HttpClientLibrary.csproj b/src/http/httpClient/Microsoft.Kiota.Http.HttpClientLibrary.csproj index 41346a5..ddfc9ed 100644 --- a/src/http/httpClient/Microsoft.Kiota.Http.HttpClientLibrary.csproj +++ b/src/http/httpClient/Microsoft.Kiota.Http.HttpClientLibrary.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/http/httpClient/Middleware/BodyInspectionHandler.cs b/src/http/httpClient/Middleware/BodyInspectionHandler.cs index 59b193c..52b2b35 100644 --- a/src/http/httpClient/Middleware/BodyInspectionHandler.cs +++ b/src/http/httpClient/Middleware/BodyInspectionHandler.cs @@ -85,13 +85,14 @@ static async Task CopyToStreamAsync( CancellationToken cancellationToken ) { - if(httpContent is null or { Headers.ContentLength: 0 }) + if(httpContent is null) { return Stream.Null; } var stream = new MemoryStream(); - + await httpContent.LoadIntoBufferAsync().ConfigureAwait(false); + #if NET5_0_OR_GREATER await httpContent.CopyToAsync(stream, cancellationToken).ConfigureAwait(false); #else diff --git a/src/serialization/json/Microsoft.Kiota.Serialization.Json.csproj b/src/serialization/json/Microsoft.Kiota.Serialization.Json.csproj index 54ed462..b77be8c 100644 --- a/src/serialization/json/Microsoft.Kiota.Serialization.Json.csproj +++ b/src/serialization/json/Microsoft.Kiota.Serialization.Json.csproj @@ -14,7 +14,7 @@ - + diff --git a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs index 176d8a6..9617a06 100644 --- a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs +++ b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs @@ -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; @@ -91,6 +92,37 @@ public async Task BodyInspectionHandlerGetsResponseBodyStream() 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(Skip = "Test can potentially be flaky due to usage limitations on Github. Enable to verify locally.")] + 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() From b0f26e3aea660398dcbad7183daeaeef91c51e44 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 7 Jan 2025 11:03:33 +0300 Subject: [PATCH 2/4] dotnet format --- src/http/httpClient/Middleware/BodyInspectionHandler.cs | 2 +- .../httpClient/Middleware/BodyInspectionHandlerTests.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/http/httpClient/Middleware/BodyInspectionHandler.cs b/src/http/httpClient/Middleware/BodyInspectionHandler.cs index 52b2b35..f83536a 100644 --- a/src/http/httpClient/Middleware/BodyInspectionHandler.cs +++ b/src/http/httpClient/Middleware/BodyInspectionHandler.cs @@ -92,7 +92,7 @@ CancellationToken cancellationToken var stream = new MemoryStream(); await httpContent.LoadIntoBufferAsync().ConfigureAwait(false); - + #if NET5_0_OR_GREATER await httpContent.CopyToAsync(stream, cancellationToken).ConfigureAwait(false); #else diff --git a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs index 9617a06..40414b5 100644 --- a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs +++ b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs @@ -92,17 +92,17 @@ public async Task BodyInspectionHandlerGetsResponseBodyStream() 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(Skip = "Test can potentially be flaky due to usage limitations on Github. Enable to verify locally.")] public async Task BodyInspectionHandlerGetsResponseBodyStreamFromGithub() { - var option = new BodyInspectionHandlerOption { InspectResponseBody = true, InspectRequestBody = true}; - var httpClient = KiotaClientFactory.Create(optionsForHandlers:[option]); + 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) { From f7180cfc729abdfff1d2fa345ec05028324400b5 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 7 Jan 2025 11:04:51 +0300 Subject: [PATCH 3/4] chore: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5dd564..a71da1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [1.16.1] - 2024-01-07 +## [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) From 7ce320d0f3875421700857373c11884407eae15b Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 7 Jan 2025 11:52:59 +0300 Subject: [PATCH 4/4] fix regression from changes. --- src/http/httpClient/Middleware/BodyInspectionHandler.cs | 2 +- tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http/httpClient/Middleware/BodyInspectionHandler.cs b/src/http/httpClient/Middleware/BodyInspectionHandler.cs index f83536a..eed3e54 100644 --- a/src/http/httpClient/Middleware/BodyInspectionHandler.cs +++ b/src/http/httpClient/Middleware/BodyInspectionHandler.cs @@ -85,7 +85,7 @@ static async Task CopyToStreamAsync( CancellationToken cancellationToken ) { - if(httpContent is null) + if(httpContent is null or { Headers.ContentLength: 0 }) { return Stream.Null; } diff --git a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs index 40414b5..de07f3c 100644 --- a/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs +++ b/tests/http/httpClient/Middleware/BodyInspectionHandlerTests.cs @@ -93,7 +93,7 @@ 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 locally.")] + [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 };