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 #11 from microsoft/fix/disposed_streams
Browse files Browse the repository at this point in the history
Do not dispose if the return type is a stream
  • Loading branch information
baywet authored Apr 6, 2022
2 parents 8de56f6 + a63393e commit 8f8f759
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.0.0-preview.4] - 2022-04-06

### Changed

- Fix issue with `HttpRequestAdapter` returning disposed streams when the requested return type is a Stream [#10](https://github.com/microsoft/kiota-http-dotnet/issues/10)

## [1.0.0-preview.3] - 2022-03-28

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Abstractions.Store;
using Moq;
using Moq.Protected;
using Xunit;

namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests
Expand Down Expand Up @@ -118,5 +123,28 @@ public void GetRequestMessageFromRequestInformationSetsContentHeaders()
Assert.Equal("application/octet-stream", requestMessage.Content.Headers.ContentType.MediaType);

}

[Fact]
public async void SendStreamReturnsUsableStream() {
var mockHandler = new Mock<HttpMessageHandler>();
var client = new HttpClient(mockHandler.Object);
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("Test")))
});
var adapter = new HttpClientRequestAdapter(_authenticationProvider, httpClient: client);
var requestInfo = new RequestInformation
{
HttpMethod = Method.GET,
UrlTemplate = "https://example.com"
};

var response = await adapter.SendPrimitiveAsync<Stream>(requestInfo);

Assert.True(response.CanRead);
Assert.Equal(4, response.Length);
}
}
}
10 changes: 8 additions & 2 deletions src/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ public async Task<ModelType> SendAsync<ModelType>(RequestInformation requestInfo
var result = rootNode.GetObjectValue<ModelType>(factory);
return result;
} finally {
await DrainAsync(response);
if (typeof(ModelType) != typeof(Stream))
{
await DrainAsync(response);
}
}
}
else
Expand Down Expand Up @@ -217,7 +220,10 @@ public async Task<ModelType> SendPrimitiveAsync<ModelType>(RequestInformation re
return (ModelType)result;
}
} finally {
await DrainAsync(response);
if (typeof(ModelType) != typeof(Stream))
{
await DrainAsync(response);
}
}
}
else
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 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>preview.3</VersionSuffix>
<VersionSuffix>preview.4</VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
<!-- <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion> -->
Expand Down

0 comments on commit 8f8f759

Please sign in to comment.