Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #175 from microsoft/andrueastman/fixCloneAsync
Browse files Browse the repository at this point in the history
Fixes a bug with `CloneAsync` extension method.
  • Loading branch information
andrueastman authored Nov 27, 2023
2 parents b00f45b + 0f0e191 commit 23d5535
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.3] - 2023-11-28

### Added

- Fixes a bug with internal `CloneAsync` method when using stream content types.

## [1.3.2] - 2023-11-15

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,39 @@ public async Task CloneAsyncWithHttpContent()
HttpMethod = Method.GET,
URI = new Uri("http://localhost")
};
requestInfo.SetStreamContent(new MemoryStream(Encoding.UTF8.GetBytes("contents")), "application/octet-stream");
var originalRequest = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);
originalRequest.Content = new StringContent("contents");

var clonedRequest = await originalRequest.CloneAsync();
var originalContents = await originalRequest.Content.ReadAsStringAsync();
var clonedRequestContents = await clonedRequest.Content?.ReadAsStringAsync();

Assert.NotNull(clonedRequest);
Assert.Equal(originalRequest.Method, clonedRequest.Method);
Assert.Equal(originalRequest.RequestUri, clonedRequest.RequestUri);
Assert.Equal(originalContents, clonedRequestContents);
Assert.Equal(originalRequest.Content?.Headers.ContentType, clonedRequest.Content?.Headers.ContentType);
}

[Fact]
public async Task CloneAsyncWithHttpStreamContent()
{
var requestInfo = new RequestInformation
{
HttpMethod = Method.GET,
URI = new Uri("http://localhost")
};
requestInfo.SetStreamContent(new MemoryStream(Encoding.UTF8.GetBytes("contents")), "application/octet-stream");
var originalRequest = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);

var clonedRequest = await originalRequest.CloneAsync();
var originalContents = await originalRequest.Content.ReadAsStringAsync();
var clonedRequestContents = await clonedRequest.Content?.ReadAsStringAsync();

Assert.NotNull(clonedRequest);
Assert.Equal(originalRequest.Method, clonedRequest.Method);
Assert.Equal(originalRequest.RequestUri, clonedRequest.RequestUri);
Assert.Equal("contents", clonedRequestContents);
Assert.Equal(originalContents, clonedRequestContents);
Assert.Equal(originalRequest.Content?.Headers.ContentType, clonedRequest.Content?.Headers.ContentType);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Extensions/HttpRequestMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ internal static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessag
if(originalRequest.Content != null)
{
// HttpClient doesn't rewind streams and we have to explicitly do so.
var contentStream = new MemoryStream();
#if NET5_0_OR_GREATER
var contentStream = await originalRequest.Content.ReadAsStreamAsync(cancellationToken);
await originalRequest.Content.CopyToAsync(contentStream, cancellationToken);
#else
var contentStream = await originalRequest.Content.ReadAsStreamAsync();
await originalRequest.Content.CopyToAsync(contentStream);
#endif

if(contentStream.CanSeek)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Http.HttpClientLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.3.2</VersionPrefix>
<VersionPrefix>1.3.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
Expand Down

0 comments on commit 23d5535

Please sign in to comment.