This commit is contained in:
@@ -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