Skip to content
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

HTTP: Cache-Control URL matcher should work on request URL without path variables substituted #3162

Merged
merged 1 commit into from
Jun 1, 2024
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
2 changes: 1 addition & 1 deletion http/cache/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NoCache(requestURLs ...string) Middleware {
func matchRequestPathSkipper(requestURLs []string) func(c echo.Context) bool {
return func(c echo.Context) bool {
for _, curr := range requestURLs {
if c.Request().URL.Path == curr {
if c.Path() == curr {
return false
}
}
Expand Down
25 changes: 25 additions & 0 deletions http/cache/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestMaxAge(t *testing.T) {
e := echo.New()
httpResponse := httptest.NewRecorder()
echoContext := e.NewContext(httptest.NewRequest("GET", "/a", nil), httpResponse)
echoContext.SetPath("/a")

err := MaxAge(time.Minute, "/a", "/b").Handle(func(c echo.Context) error {
return c.String(200, "OK")
Expand All @@ -43,6 +44,7 @@ func TestMaxAge(t *testing.T) {
e := echo.New()
httpResponse := httptest.NewRecorder()
echoContext := e.NewContext(httptest.NewRequest("GET", "/c", nil), httpResponse)
echoContext.SetPath("/c")

err := MaxAge(time.Minute, "/a", "/b").Handle(func(c echo.Context) error {
return c.String(200, "OK")
Expand All @@ -59,6 +61,7 @@ func TestNoCache(t *testing.T) {
e := echo.New()
httpResponse := httptest.NewRecorder()
echoContext := e.NewContext(httptest.NewRequest("GET", "/a", nil), httpResponse)
echoContext.SetPath("/a")

err := NoCache("/a", "/b").Handle(func(c echo.Context) error {
return c.String(200, "OK")
Expand All @@ -72,6 +75,7 @@ func TestNoCache(t *testing.T) {
e := echo.New()
httpResponse := httptest.NewRecorder()
echoContext := e.NewContext(httptest.NewRequest("GET", "/c", nil), httpResponse)
echoContext.SetPath("/c")

err := NoCache("/a", "/b").Handle(func(c echo.Context) error {
return c.String(200, "OK")
Expand All @@ -82,3 +86,24 @@ func TestNoCache(t *testing.T) {
require.Empty(t, httpResponse.Header().Get("Pragma"))
})
}

func Test_matchRequestPathSkipper(t *testing.T) {
t.Run("matches on path with path variable", func(t *testing.T) {
e := echo.New()
httpResponse := httptest.NewRecorder()
echoContext := e.NewContext(httptest.NewRequest("GET", "/a/foo", nil), httpResponse)
echoContext.SetPath("/a/:id")

actual := matchRequestPathSkipper([]string{"/a/:id"})(echoContext)
require.False(t, actual)
})
t.Run("no match", func(t *testing.T) {
e := echo.New()
httpResponse := httptest.NewRecorder()
echoContext := e.NewContext(httptest.NewRequest("GET", "/a/foo", nil), httpResponse)
echoContext.SetPath("/a/foo")

actual := matchRequestPathSkipper([]string{"/a/bar"})(echoContext)
require.True(t, actual)
})
}
Loading