Skip to content

Commit 0ae7a16

Browse files
authored
feat(#128): redirect to home, when hitting home without trailing slash (#133)
1 parent 7906b96 commit 0ae7a16

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/Serilog.Ui.Web/Endpoints/SerilogUiAppRoutes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public Task RedirectHomeAsync()
5252
var httpContext = Guard.Against.Null(httpContextAccessor.HttpContext);
5353

5454
var indexUrl = httpContext.Request.GetEncodedUrl().Replace("index.html", "");
55+
var indexUrlWithTrailingSlash = indexUrl.EndsWith('/') ? indexUrl : $"{indexUrl}/";
5556

56-
httpContext.Response.Redirect(indexUrl, true);
57+
httpContext.Response.Redirect(indexUrlWithTrailingSlash, true);
5758

5859
return Task.CompletedTask;
5960
}

src/Serilog.Ui.Web/SerilogUiMiddleware.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public Task Invoke(HttpContext httpContext, ISerilogUiAppRoutes uiAppRoutes, ISe
4949

5050
if (CheckPath(path, "/api/keys/?")) return uiEndpoints.GetApiKeysAsync();
5151
if (CheckPath(path, "/api/logs/?")) return uiEndpoints.GetLogsAsync();
52-
if (CheckPath(path, "/index.html")) return uiAppRoutes.RedirectHomeAsync();
52+
53+
// prefix without trailing slash or old index.html routing
54+
if (CheckPath(path, "/index.html") || CheckPath(path, "")) return uiAppRoutes.RedirectHomeAsync();
55+
56+
// asset request, we remove any extra path part since it's always served from the serilog-ui root
5357
if (CheckPath(path, "/(?:.*(.*/))(?:(assets/)).*")) return ChangeAssetRequestPath(httpContext);
5458

5559
return CheckPath(path, "/(?!.*(assets/)).*") ? uiAppRoutes.GetHomeAsync() : _staticFileMiddleware.Invoke(httpContext);

tests/Serilog.Ui.Web.Tests/SerilogUiMiddlewareTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class SerilogUiMiddlewareTest(WebAppFactory.WithMocks program, WebAppFact
2121
[InlineData("/serilog-ui/api/logs/", 409)]
2222
[InlineData("/serilog-ui/", 418)]
2323
[InlineData("/serilog-ui/index.html", 400)]
24+
[InlineData("/serilog-ui", 400)]
2425
public async Task It_hits_ui_endpoint_when_request_matches_method_and_options_prefix(string pathReq, int statusCode)
2526
{
2627
// Act
@@ -35,6 +36,7 @@ public async Task It_hits_ui_endpoint_when_request_matches_method_and_options_pr
3536
[InlineData("/test/api/logs/", 409)]
3637
[InlineData("/test/", 418)]
3738
[InlineData("/test/index.html", 400)]
39+
[InlineData("/test", 400)]
3840
public async Task It_hits_ui_endpoint_when_request_matches_method_and_custom_options_prefix(string pathReq, int statusCode)
3941
{
4042
// Act
@@ -43,7 +45,7 @@ public async Task It_hits_ui_endpoint_when_request_matches_method_and_custom_opt
4345
// Assert
4446
send.StatusCode.Should().Be((HttpStatusCode)statusCode);
4547
}
46-
48+
4749
[Theory]
4850
[InlineData("/serilog-ui/assets/index.js")]
4951
[InlineData("/serilog-ui/assets/index.js?query=query", "?query=query")]
@@ -83,6 +85,7 @@ public async Task It_not_map_request_to_assets_when_request_final_path_part_not_
8385
[InlineData("/fake-prefix", 400)]
8486
[InlineData("/fake-prefix/", 400)]
8587
[InlineData("/fake-prefix/index.html", 418)]
88+
[InlineData("/fake-prefix", 418)]
8689
public async Task It_proceeds_onwards_when_request_does_not_match_options_prefix(string pathReq, int statusCode)
8790
{
8891
// Act
@@ -97,6 +100,7 @@ public async Task It_proceeds_onwards_when_request_does_not_match_options_prefix
97100
[InlineData("/serilog-ui/api/logs/", 409)]
98101
[InlineData("/serilog-ui/", 400)]
99102
[InlineData("/serilog-ui/index.html", 418)]
103+
[InlineData("/serilog-ui", 418)]
100104
public async Task It_proceeds_onwards_when_request_is_not_a_get(string pathReq, int statusCode)
101105
{
102106
// Arrange

0 commit comments

Comments
 (0)