-
Notifications
You must be signed in to change notification settings - Fork 0
feat(middleware): skip /readyz by default and add WithExcludedRoutes #10
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //go:build unit | ||
|
|
||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gofiber/fiber/v2" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestIsRouteExcludedFromList(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| path string | ||
| excluded []string | ||
| want bool | ||
| }{ | ||
| // exact match | ||
| {name: "exact match", path: "/health", excluded: []string{"/health"}, want: true}, | ||
|
|
||
| // subpath match (segment boundary) | ||
| {name: "subpath under excluded route", path: "/health/check", excluded: []string{"/health"}, want: true}, | ||
| {name: "deep subpath", path: "/api/v1/users/42", excluded: []string{"/api/v1"}, want: true}, | ||
|
|
||
| // regression guards against the original raw-prefix bug | ||
| {name: "sibling with shared prefix (suffix letter)", path: "/healthz", excluded: []string{"/health"}, want: false}, | ||
| {name: "sibling with shared prefix (hyphenated)", path: "/health-check", excluded: []string{"/health"}, want: false}, | ||
| {name: "sibling with shared prefix (under /metrics)", path: "/metricsproxy", excluded: []string{"/metrics"}, want: false}, | ||
| {name: "minor version not a child", path: "/api/v1.0/users", excluded: []string{"/api/v1"}, want: false}, | ||
|
|
||
| // trailing slash tolerance on the excluded entry | ||
| {name: "excluded route with trailing slash matches path without", path: "/metrics", excluded: []string{"/metrics/"}, want: true}, | ||
| {name: "excluded route with trailing slash matches subpath", path: "/metrics/cpu", excluded: []string{"/metrics/"}, want: true}, | ||
|
|
||
| // empty / root entries are not wildcards | ||
| {name: "empty string entry is not a wildcard", path: "/anything", excluded: []string{""}, want: false}, | ||
| {name: "root slash entry is not a wildcard", path: "/anything", excluded: []string{"/"}, want: false}, | ||
|
|
||
| // list semantics | ||
| {name: "no excluded routes", path: "/anywhere", excluded: nil, want: false}, | ||
| {name: "no match in list", path: "/v1/orders", excluded: []string{"/health", "/readyz"}, want: false}, | ||
| {name: "match second entry", path: "/readyz", excluded: []string{"/health", "/readyz"}, want: true}, | ||
| {name: "match wins over later non-matches", path: "/health/x", excluded: []string{"/health", "/no-match"}, want: true}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| app := fiber.New() | ||
| var got bool | ||
| app.Use(func(c *fiber.Ctx) error { | ||
| got = isRouteExcludedFromList(c, tc.excluded) | ||
| return c.SendStatus(http.StatusNoContent) | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, tc.path, nil) | ||
| resp, err := app.Test(req) | ||
| assert.NoError(t, err) | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| assert.Equal(t, tc.want, got, "isRouteExcludedFromList(path=%q, excluded=%v)", tc.path, tc.excluded) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.