Skip to content

TestServer sends remaining content in pipe writer on complete #11268

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions src/Hosting/TestHost/src/HttpContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ internal void AbortRequest()

internal async Task CompleteResponseAsync()
{
// Flush any pending content in writer
await _httpContext.Response.BodyWriter.FlushAsync();
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this redundant with PipeWriter.Complete() that's called from CompleteWrites()? Do you just need to reverse the calls to ReturnResponseMessageAsync and CompleteWrites?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah. I'm not sure I understand this change. If the PipeWriter is completed, that should flush any pending content. The reader might see the pending data until after Complete() returns, but that's also true for FlushAsync() with a PauseWriterThreshold of anything other than 1.

Copy link
Member Author

Choose a reason for hiding this comment

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

It isn't redundant because the unit test fails if FlushAsync isn't here.

Is there a better way to do this?


_pipelineFinished = true;
await ReturnResponseMessageAsync();
_responseStream.CompleteWrites();
Expand Down
19 changes: 19 additions & 0 deletions src/Hosting/TestHost/test/ClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -267,6 +268,24 @@ public async Task HeadersAvailableBeforeBodyFinished()
Assert.Equal("BodyStarted,BodyFinished", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task PendingContentInWriterIsSent()
{
var handler = new ClientHandler(PathString.Empty, new DummyApplication(context =>
{
var data = Encoding.UTF8.GetBytes("Hello world");

var span = context.Response.BodyWriter.GetSpan(data.Length);
data.CopyTo(span);
context.Response.BodyWriter.Advance(data.Length);

return Task.CompletedTask;
}));
var httpClient = new HttpClient(handler);
HttpResponseMessage response = await httpClient.GetAsync("https://example.com/");
Assert.Equal("Hello world", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task FlushSendsHeaders()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
<Reference Include="System.ServiceProcess.ServiceController" />
</ItemGroup>
<ItemGroup>
<Compile Update="WebHostService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Update="WebHostService.cs" />
Copy link
Member Author

Choose a reason for hiding this comment

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

VS changed this automatically 🤷‍♂

</ItemGroup>

</Project>