Skip to content

Commit

Permalink
feat(serilog-contrib#128): redirect to home, when hitting home withou…
Browse files Browse the repository at this point in the history
…t trailing slash (serilog-contrib#133)
  • Loading branch information
followynne authored Aug 13, 2024
1 parent 7906b96 commit 0ae7a16
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Serilog.Ui.Web/Endpoints/SerilogUiAppRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public Task RedirectHomeAsync()
var httpContext = Guard.Against.Null(httpContextAccessor.HttpContext);

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

httpContext.Response.Redirect(indexUrl, true);
httpContext.Response.Redirect(indexUrlWithTrailingSlash, true);

return Task.CompletedTask;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Serilog.Ui.Web/SerilogUiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public Task Invoke(HttpContext httpContext, ISerilogUiAppRoutes uiAppRoutes, ISe

if (CheckPath(path, "/api/keys/?")) return uiEndpoints.GetApiKeysAsync();
if (CheckPath(path, "/api/logs/?")) return uiEndpoints.GetLogsAsync();
if (CheckPath(path, "/index.html")) return uiAppRoutes.RedirectHomeAsync();

// prefix without trailing slash or old index.html routing
if (CheckPath(path, "/index.html") || CheckPath(path, "")) return uiAppRoutes.RedirectHomeAsync();

// asset request, we remove any extra path part since it's always served from the serilog-ui root
if (CheckPath(path, "/(?:.*(.*/))(?:(assets/)).*")) return ChangeAssetRequestPath(httpContext);

return CheckPath(path, "/(?!.*(assets/)).*") ? uiAppRoutes.GetHomeAsync() : _staticFileMiddleware.Invoke(httpContext);
Expand Down
6 changes: 5 additions & 1 deletion tests/Serilog.Ui.Web.Tests/SerilogUiMiddlewareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SerilogUiMiddlewareTest(WebAppFactory.WithMocks program, WebAppFact
[InlineData("/serilog-ui/api/logs/", 409)]
[InlineData("/serilog-ui/", 418)]
[InlineData("/serilog-ui/index.html", 400)]
[InlineData("/serilog-ui", 400)]
public async Task It_hits_ui_endpoint_when_request_matches_method_and_options_prefix(string pathReq, int statusCode)
{
// Act
Expand All @@ -35,6 +36,7 @@ public async Task It_hits_ui_endpoint_when_request_matches_method_and_options_pr
[InlineData("/test/api/logs/", 409)]
[InlineData("/test/", 418)]
[InlineData("/test/index.html", 400)]
[InlineData("/test", 400)]
public async Task It_hits_ui_endpoint_when_request_matches_method_and_custom_options_prefix(string pathReq, int statusCode)
{
// Act
Expand All @@ -43,7 +45,7 @@ public async Task It_hits_ui_endpoint_when_request_matches_method_and_custom_opt
// Assert
send.StatusCode.Should().Be((HttpStatusCode)statusCode);
}

[Theory]
[InlineData("/serilog-ui/assets/index.js")]
[InlineData("/serilog-ui/assets/index.js?query=query", "?query=query")]
Expand Down Expand Up @@ -83,6 +85,7 @@ public async Task It_not_map_request_to_assets_when_request_final_path_part_not_
[InlineData("/fake-prefix", 400)]
[InlineData("/fake-prefix/", 400)]
[InlineData("/fake-prefix/index.html", 418)]
[InlineData("/fake-prefix", 418)]
public async Task It_proceeds_onwards_when_request_does_not_match_options_prefix(string pathReq, int statusCode)
{
// Act
Expand All @@ -97,6 +100,7 @@ public async Task It_proceeds_onwards_when_request_does_not_match_options_prefix
[InlineData("/serilog-ui/api/logs/", 409)]
[InlineData("/serilog-ui/", 400)]
[InlineData("/serilog-ui/index.html", 418)]
[InlineData("/serilog-ui", 418)]
public async Task It_proceeds_onwards_when_request_is_not_a_get(string pathReq, int statusCode)
{
// Arrange
Expand Down

0 comments on commit 0ae7a16

Please sign in to comment.