Skip to content

Commit 4eb209d

Browse files
committed
[#288] [add] impl
1 parent 8aa369d commit 4eb209d

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/Simplify.Web.Tests/RequestHandling/Handlers/SetLoginUrlForUnauthorizedRequestHandlerTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ public async Task HandleAsync_StatusCodeIsUnauthorized_SetLoginReturnUrlFromCurr
4343
_redirector.Verify(x => x.SetLoginReturnUrlFromCurrentUri());
4444
}
4545

46+
[Test]
47+
public async Task HandleAsync_UnauthorizedButResponseAlreadyStarted_SetLoginReturnUrlFromCurrentUriNotCalled()
48+
{
49+
// Arrange
50+
51+
var httpContext = Mock.Of<HttpContext>(x =>
52+
x.Response.StatusCode == (int)HttpStatusCode.Unauthorized && x.Response.HasStarted == true);
53+
var next = new Mock<RequestHandlerAsync>();
54+
55+
// Act
56+
await _handler.HandleAsync(httpContext, next.Object);
57+
58+
// Assert
59+
60+
next.Verify(x => x.Invoke());
61+
_redirector.Verify(x => x.SetLoginReturnUrlFromCurrentUri(), Times.Never);
62+
}
63+
4664
[Test]
4765
public async Task HandleAsync_StatusCodeIsNoUnauthorized_SetLoginReturnUrlFromCurrentUriNotCalled()
4866
{

src/Simplify.Web/RequestHandling/Handlers/SetLoginUrlForUnauthorizedRequestHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public async Task HandleAsync(HttpContext context, RequestHandlerAsync next)
2020
{
2121
await next();
2222

23-
if (context.Response.StatusCode == (int)HttpStatusCode.Unauthorized)
23+
// The login return URL is stored in a Set-Cookie header, which cannot be added once the
24+
// response has started (e.g. a controller that returned 401 together with a body). Skip it
25+
// in that case instead of throwing "response has already started".
26+
if (context.Response.StatusCode == (int)HttpStatusCode.Unauthorized && !context.Response.HasStarted)
2427
redirector.SetLoginReturnUrlFromCurrentUri();
2528
}
2629
}

0 commit comments

Comments
 (0)