Skip to content
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
5 changes: 4 additions & 1 deletion src/Http/Http/src/Builder/ApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ public RequestDelegate Build()
throw new InvalidOperationException(message);
}

context.Response.StatusCode = StatusCodes.Status404NotFound;
if (!context.Response.HasStarted)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
}
return Task.CompletedTask;
};

Expand Down
46 changes: 46 additions & 0 deletions src/Http/Http/test/ApplicationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace Microsoft.AspNetCore.Builder.Internal;

Expand All @@ -19,6 +20,23 @@ public void BuildReturnsCallableDelegate()
Assert.Equal(404, httpContext.Response.StatusCode);
}

[Fact]
public async Task BuildReturnDelegateThatDoesNotSetStatusCodeIfResponseHasStarted()
{
var builder = new ApplicationBuilder(null);
var app = builder.Build();

var httpContext = new DefaultHttpContext();
var responseFeature = new TestHttpResponseFeature();
httpContext.Features.Set<IHttpResponseFeature>(responseFeature);
httpContext.Response.StatusCode = 200;

responseFeature.HasStarted = true;

await app.Invoke(httpContext);
Assert.Equal(200, httpContext.Response.StatusCode);
}

[Fact]
public void ServerFeaturesEmptyWhenNotSpecified()
{
Expand Down Expand Up @@ -96,4 +114,32 @@ public void PropertiesDictionaryIsDistinctAfterNew()

Assert.Equal("value1", builder1.Properties["test"]);
}

private class TestHttpResponseFeature : IHttpResponseFeature
{
private int _statusCode = 200;
public int StatusCode
{
get => _statusCode;
set
{
_statusCode = HasStarted ? throw new NotSupportedException("The response has already started") : value;
}
}
public string ReasonPhrase { get; set; }
public IHeaderDictionary Headers { get; set; }
public Stream Body { get; set; } = Stream.Null;

public bool HasStarted { get; set; }

public void OnCompleted(Func<object, Task> callback, object state)
{

}

public void OnStarting(Func<object, Task> callback, object state)
{

}
}
}