-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Fix IAsyncEnumerable controller methods to allow setting headers #57924
Open
BrennanConroy
wants to merge
3
commits into
release/9.0-rc2
Choose a base branch
from
brecon/startasync
base: release/9.0-rc2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+302
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,13 @@ | ||||||||||
// Licensed to the .NET Foundation under one or more agreements. | ||||||||||
// The .NET Foundation licenses this file to you under the MIT license. | ||||||||||
|
||||||||||
using System.IO.Pipelines; | ||||||||||
using System.Runtime.CompilerServices; | ||||||||||
using System.Text; | ||||||||||
using System.Text.Json; | ||||||||||
using System.Text.Json.Serialization; | ||||||||||
using System.Text.Json.Serialization.Metadata; | ||||||||||
using Microsoft.AspNetCore.InternalTesting; | ||||||||||
using Microsoft.AspNetCore.Http.Features; | ||||||||||
|
||||||||||
#nullable enable | ||||||||||
|
||||||||||
|
@@ -481,6 +482,83 @@ public async Task WriteAsJsonAsync_NullValue_WithJsonTypeInfo_JsonResponse() | |||||||||
Assert.Equal("null", data); | ||||||||||
} | ||||||||||
|
||||||||||
[Fact] | ||||||||||
public async Task WriteAsJsonAsyncGeneric_AsyncEnumerableStartAsyncNotCalled() | ||||||||||
{ | ||||||||||
// Arrange | ||||||||||
var body = new MemoryStream(); | ||||||||||
var context = new DefaultHttpContext(); | ||||||||||
context.Response.Body = body; | ||||||||||
var responseBodyFeature = new TestHttpResponseBodyFeature(context.Features.GetRequiredFeature<IHttpResponseBodyFeature>()); | ||||||||||
context.Features.Set<IHttpResponseBodyFeature>(responseBodyFeature); | ||||||||||
|
||||||||||
// Act | ||||||||||
await context.Response.WriteAsJsonAsync(AsyncEnumerable()); | ||||||||||
|
||||||||||
// Assert | ||||||||||
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType); | ||||||||||
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode); | ||||||||||
|
||||||||||
Assert.Equal("[1,2]", Encoding.UTF8.GetString(body.ToArray())); | ||||||||||
|
||||||||||
async IAsyncEnumerable<int> AsyncEnumerable() | ||||||||||
{ | ||||||||||
Assert.False(responseBodyFeature.StartCalled); | ||||||||||
await Task.Yield(); | ||||||||||
Comment on lines
+506
to
+507
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be better to test this after yielding?
Suggested change
I wonder if we should add an end-to-end test where we try adding a header after yielding. |
||||||||||
yield return 1; | ||||||||||
yield return 2; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
[Fact] | ||||||||||
public async Task WriteAsJsonAsync_AsyncEnumerableStartAsyncNotCalled() | ||||||||||
{ | ||||||||||
// Arrange | ||||||||||
var body = new MemoryStream(); | ||||||||||
var context = new DefaultHttpContext(); | ||||||||||
context.Response.Body = body; | ||||||||||
var responseBodyFeature = new TestHttpResponseBodyFeature(context.Features.GetRequiredFeature<IHttpResponseBodyFeature>()); | ||||||||||
context.Features.Set<IHttpResponseBodyFeature>(responseBodyFeature); | ||||||||||
|
||||||||||
// Act | ||||||||||
await context.Response.WriteAsJsonAsync(AsyncEnumerable(), typeof(IAsyncEnumerable<int>)); | ||||||||||
|
||||||||||
// Assert | ||||||||||
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType); | ||||||||||
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode); | ||||||||||
|
||||||||||
Assert.Equal("[1,2]", Encoding.UTF8.GetString(body.ToArray())); | ||||||||||
|
||||||||||
async IAsyncEnumerable<int> AsyncEnumerable() | ||||||||||
{ | ||||||||||
Assert.False(responseBodyFeature.StartCalled); | ||||||||||
await Task.Yield(); | ||||||||||
yield return 1; | ||||||||||
yield return 2; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
[Fact] | ||||||||||
public async Task WriteAsJsonAsync_StartAsyncCalled() | ||||||||||
{ | ||||||||||
// Arrange | ||||||||||
var body = new MemoryStream(); | ||||||||||
var context = new DefaultHttpContext(); | ||||||||||
context.Response.Body = body; | ||||||||||
var responseBodyFeature = new TestHttpResponseBodyFeature(context.Features.GetRequiredFeature<IHttpResponseBodyFeature>()); | ||||||||||
context.Features.Set<IHttpResponseBodyFeature>(responseBodyFeature); | ||||||||||
|
||||||||||
// Act | ||||||||||
await context.Response.WriteAsJsonAsync(new int[] {1, 2}, typeof(int[])); | ||||||||||
|
||||||||||
// Assert | ||||||||||
Assert.Equal(ContentTypeConstants.JsonContentTypeWithCharset, context.Response.ContentType); | ||||||||||
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode); | ||||||||||
|
||||||||||
Assert.Equal("[1,2]", Encoding.UTF8.GetString(body.ToArray())); | ||||||||||
Assert.True(responseBodyFeature.StartCalled); | ||||||||||
} | ||||||||||
|
||||||||||
public class TestObject | ||||||||||
{ | ||||||||||
public string? StringProperty { get; set; } | ||||||||||
|
@@ -530,4 +608,41 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo | |||||||||
return new ValueTask(tcs.Task); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
public class TestHttpResponseBodyFeature : IHttpResponseBodyFeature | ||||||||||
{ | ||||||||||
private readonly IHttpResponseBodyFeature _inner; | ||||||||||
|
||||||||||
public bool StartCalled; | ||||||||||
|
||||||||||
public TestHttpResponseBodyFeature(IHttpResponseBodyFeature inner) | ||||||||||
{ | ||||||||||
_inner = inner; | ||||||||||
} | ||||||||||
|
||||||||||
public Stream Stream => _inner.Stream; | ||||||||||
|
||||||||||
public PipeWriter Writer => _inner.Writer; | ||||||||||
|
||||||||||
public Task CompleteAsync() | ||||||||||
{ | ||||||||||
return _inner.CompleteAsync(); | ||||||||||
} | ||||||||||
|
||||||||||
public void DisableBuffering() | ||||||||||
{ | ||||||||||
_inner.DisableBuffering(); | ||||||||||
} | ||||||||||
|
||||||||||
public Task SendFileAsync(string path, long offset, long? count, CancellationToken cancellationToken = default) | ||||||||||
{ | ||||||||||
return _inner.SendFileAsync(path, offset, count, cancellationToken); | ||||||||||
} | ||||||||||
|
||||||||||
public Task StartAsync(CancellationToken cancellationToken = default) | ||||||||||
{ | ||||||||||
StartCalled = true; | ||||||||||
return _inner.StartAsync(cancellationToken); | ||||||||||
} | ||||||||||
} | ||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a method to AsyncEnumerableHelper like the following?
It seems like it would reduce a lot of repetitive
HasStarted
and null checks.