Files
wehub-resource-sync 75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:11:07 +08:00

56 lines
1.4 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func assertNoStoreHeaders(t *testing.T, response *httptest.ResponseRecorder) {
t.Helper()
assert.Equal(t, "no-store, no-cache, must-revalidate, max-age=0", response.Header().Get("Cache-Control"))
assert.Equal(t, "no-cache", response.Header().Get("Pragma"))
assert.Equal(t, "0", response.Header().Get("Expires"))
}
func buildRouterWithNoStore() *gin.Engine {
gin.SetMode(gin.TestMode)
router := gin.New()
router.Use(NoStoreCacheControl())
router.GET("/api/v1/system/version", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"version": "3.45.0"})
})
router.NoRoute(func(ctx *gin.Context) {
ctx.String(http.StatusOK, "<html></html>")
})
return router
}
func Test_NoStoreCacheControl_OnApiRoute_SetsUncacheableHeaders(t *testing.T) {
response := httptest.NewRecorder()
buildRouterWithNoStore().ServeHTTP(
response,
httptest.NewRequest(http.MethodGet, "/api/v1/system/version", nil),
)
assert.Equal(t, http.StatusOK, response.Code)
assertNoStoreHeaders(t, response)
}
func Test_NoStoreCacheControl_OnStaticFallbackRoute_SetsUncacheableHeaders(t *testing.T) {
response := httptest.NewRecorder()
buildRouterWithNoStore().ServeHTTP(
response,
httptest.NewRequest(http.MethodGet, "/assets/index-a1b2c3d4.js", nil),
)
assert.Equal(t, http.StatusOK, response.Code)
assertNoStoreHeaders(t, response)
}