-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IAM: Add Cache-Control max-age header to cachable IAM resources
- Loading branch information
Showing
4 changed files
with
120 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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,41 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Middleware struct { | ||
Skipper middleware.Skipper | ||
maxAge time.Duration | ||
} | ||
|
||
func (m Middleware) Handle(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
if !m.Skipper(c) { | ||
if m.maxAge > 0 { | ||
c.Response().Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", int(m.maxAge.Seconds()))) | ||
} | ||
} | ||
return next(c) | ||
} | ||
} | ||
|
||
// MaxAge creates a new middleware that sets the Cache-Control header to the given max-age for the given request URLs. | ||
func MaxAge(maxAge time.Duration, requestURLs []string) Middleware { | ||
return Middleware{ | ||
Skipper: func(c echo.Context) bool { | ||
for _, curr := range requestURLs { | ||
// trim leading and trailing /before comparing, just in case | ||
if strings.Trim(c.Request().URL.Path, "/") == strings.Trim(curr, "/") { | ||
return false | ||
} | ||
} | ||
return true | ||
}, | ||
maxAge: maxAge, | ||
} | ||
} |
This file contains 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,37 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/require" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestMaxAge(t *testing.T) { | ||
t.Run("match", func(t *testing.T) { | ||
e := echo.New() | ||
httpResponse := httptest.NewRecorder() | ||
echoContext := e.NewContext(httptest.NewRequest("GET", "/a/", nil), httpResponse) | ||
|
||
err := MaxAge(time.Minute, []string{"a", "b"}).Handle(func(c echo.Context) error { | ||
return c.String(200, "OK") | ||
})(echoContext) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "max-age=60", httpResponse.Header().Get("Cache-Control")) | ||
}) | ||
t.Run("no match", func(t *testing.T) { | ||
e := echo.New() | ||
httpResponse := httptest.NewRecorder() | ||
echoContext := e.NewContext(httptest.NewRequest("GET", "/c", nil), httpResponse) | ||
|
||
err := MaxAge(time.Minute, []string{"a", "b"}).Handle(func(c echo.Context) error { | ||
return c.String(200, "OK") | ||
})(echoContext) | ||
|
||
require.NoError(t, err) | ||
require.Empty(t, httpResponse.Header().Get("Cache-Control")) | ||
}) | ||
|
||
} |