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

[Core] Don't throw from Response.Content if memory stream is private #21352

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions sdk/core/Azure.Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Types to represent `GeoJson` primitives.

### Changed

- `Response.Content` no longer throws `InvalidOperationException` when the response is backed by a `MemoryStream` with a non publicly visible buffer.

## 1.14.0 (2021-05-11)

### Features Added
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/src/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public virtual BinaryData Content
}
else
{
throw new InvalidOperationException($"The {nameof(ContentStream)}'s internal buffer cannot be accessed.");
return new BinaryData(memoryContent.ToArray());
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions sdk/core/Azure.Core/tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ public void ContentPropertyThrowsForNonMemoryStream()
Assert.Throws<InvalidOperationException>(() => { BinaryData d = response.Content; });
}

public void ContentPropertyThrowsForNotExposableMemoryStream()
[Test]
public void ContentPropertyWorksForMemoryStreamsWithPrivateBuffers()
{
var response = new MockResponse(200);
response.ContentStream = new MemoryStream(new byte[100], 0, 100, writable: false, publiclyVisible: false);
var responseBody = new byte[100];
response.ContentStream = new MemoryStream(responseBody, 0, responseBody.Length, writable: false, publiclyVisible: false);

Assert.Throws<InvalidOperationException>(() => { BinaryData d = response.Content; });
Assert.DoesNotThrow(() => { BinaryData d = response.Content; });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assert that the arrays have the same sequence?

CollectionAssert.AreEqual(responseBody, response.Content.ToArray());
}

internal class TestPayload
Expand Down