diff --git a/CHANGELOG.md b/CHANGELOG.md index 4146a73..a71da1e 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.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 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..eed3e54 100644 --- a/src/http/httpClient/Middleware/BodyInspectionHandler.cs +++ b/src/http/httpClient/Middleware/BodyInspectionHandler.cs @@ -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); 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..de07f3c 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; @@ -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() {