This commit is contained in:
@@ -1,18 +1,38 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"vctp/version"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"vctp/version"
|
||||
)
|
||||
|
||||
// CacheMiddleware sets the Cache-Control header based on the version.
|
||||
func CacheMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if version.Value == "dev" {
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
} else {
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
||||
cacheControl := "public, max-age=31536000"
|
||||
if isVersionedAssetRequest(r) {
|
||||
cacheControl += ", immutable"
|
||||
}
|
||||
w.Header().Set("Cache-Control", cacheControl)
|
||||
w.Header().Set("Expires", time.Now().UTC().Add(365*24*time.Hour).Format(http.TimeFormat))
|
||||
}
|
||||
w.Header().Set("Vary", "Accept-Encoding")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func isVersionedAssetRequest(r *http.Request) bool {
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
if r.URL.Query().Get("v") != "" {
|
||||
return true
|
||||
}
|
||||
return strings.Contains(r.URL.Path, "@")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"vctp/version"
|
||||
)
|
||||
|
||||
func TestCacheMiddlewareDev(t *testing.T) {
|
||||
orig := version.Value
|
||||
version.Value = "dev"
|
||||
defer func() { version.Value = orig }()
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/assets/css/web3.css", nil)
|
||||
h := CacheMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if got := rr.Header().Get("Cache-Control"); got != "no-cache, no-store, must-revalidate" {
|
||||
t.Fatalf("unexpected Cache-Control: %q", got)
|
||||
}
|
||||
if got := rr.Header().Get("Pragma"); got != "no-cache" {
|
||||
t.Fatalf("unexpected Pragma: %q", got)
|
||||
}
|
||||
if got := rr.Header().Get("Expires"); got != "0" {
|
||||
t.Fatalf("unexpected Expires: %q", got)
|
||||
}
|
||||
if got := rr.Header().Get("Vary"); got != "Accept-Encoding" {
|
||||
t.Fatalf("unexpected Vary: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheMiddlewareProd(t *testing.T) {
|
||||
orig := version.Value
|
||||
version.Value = "1.2.3"
|
||||
defer func() { version.Value = orig }()
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/assets/css/web3.css?v=1.2.3", nil)
|
||||
h := CacheMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if got := rr.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
|
||||
t.Fatalf("unexpected Cache-Control: %q", got)
|
||||
}
|
||||
if rr.Header().Get("Expires") == "" {
|
||||
t.Fatalf("expected Expires header")
|
||||
}
|
||||
if got := rr.Header().Get("Vary"); got != "Accept-Encoding" {
|
||||
t.Fatalf("unexpected Vary: %q", got)
|
||||
}
|
||||
if got := rr.Header().Get("Pragma"); got != "" {
|
||||
t.Fatalf("expected no Pragma in prod, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheMiddlewareProdUnversionedStillCached(t *testing.T) {
|
||||
orig := version.Value
|
||||
version.Value = "1.2.3"
|
||||
defer func() { version.Value = orig }()
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/swagger/swagger-ui.css", nil)
|
||||
h := CacheMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
h.ServeHTTP(rr, req)
|
||||
|
||||
if got := rr.Header().Get("Cache-Control"); got != "public, max-age=31536000" {
|
||||
t.Fatalf("unexpected Cache-Control: %q", got)
|
||||
}
|
||||
if rr.Header().Get("Expires") == "" {
|
||||
t.Fatalf("expected Expires header")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user