Skip to content

feat(#128): redirect to home, when hitting home without trailing slash #133

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

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
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
Loading