Files
Gumble-Backend/internal/util/cache_control_test.go
Zane Walker 7e940c83a7
Some checks failed
Build & Test / build-test (push) Has been cancelled
Build & Test / swagger-codegen-cli (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
(Feat): Initial Commit
2026-07-03 19:41:31 +05:30

78 lines
2.5 KiB
Go

package util_test
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"allaboutapps.dev/aw/go-starter/internal/api"
"allaboutapps.dev/aw/go-starter/internal/api/middleware"
"allaboutapps.dev/aw/go-starter/internal/test"
"allaboutapps.dev/aw/go-starter/internal/util"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCacheControl(t *testing.T) {
test.WithTestServer(t, func(s *api.Server) {
path := "/testing-1c2cad5f-7545-4177-9bfe-8dc7ed368b33"
s.Echo.GET(path, func(c echo.Context) error {
cache := util.CacheControlDirectiveFromContext(c.Request().Context())
switch {
case cache.HasDirective(util.CacheControlDirectiveNoCache) && cache.HasDirective(util.CacheControlDirectiveNoStore):
return c.JSON(http.StatusOK, "no-cache,no-store")
case cache.HasDirective(util.CacheControlDirectiveNoCache):
return c.JSON(http.StatusOK, "no-cache")
case cache.HasDirective(util.CacheControlDirectiveNoStore):
return c.JSON(http.StatusOK, "no-store")
}
return c.NoContent(http.StatusNoContent)
}, middleware.CacheControl())
cacheControlNoCache := util.CacheControlDirectiveNoCache
cacheControlNoStore := util.CacheControlDirectiveNoStore
header := http.Header{}
header.Set(util.HTTPHeaderCacheControl, fmt.Sprintf("%s,%s", cacheControlNoStore.String(), cacheControlNoCache.String()))
res := test.PerformRequest(t, s, "GET", path, nil, header)
require.Equal(t, http.StatusOK, res.Result().StatusCode)
var resp string
err := json.NewDecoder(res.Result().Body).Decode(&resp)
require.NoError(t, err)
assert.Equal(t, "no-cache,no-store", resp)
header.Set(util.HTTPHeaderCacheControl, cacheControlNoCache.String())
res = test.PerformRequest(t, s, "GET", path, nil, header)
require.Equal(t, http.StatusOK, res.Result().StatusCode)
err = json.NewDecoder(res.Result().Body).Decode(&resp)
require.NoError(t, err)
assert.Equal(t, "no-cache", resp)
header.Set(util.HTTPHeaderCacheControl, cacheControlNoStore.String())
res = test.PerformRequest(t, s, "GET", path, nil, header)
require.Equal(t, http.StatusOK, res.Result().StatusCode)
err = json.NewDecoder(res.Result().Body).Decode(&resp)
require.NoError(t, err)
assert.Equal(t, "no-store", resp)
res = test.PerformRequest(t, s, "GET", path, nil, nil)
assert.Equal(t, http.StatusNoContent, res.Result().StatusCode)
header.Set(util.HTTPHeaderCacheControl, "gunther")
res = test.PerformRequest(t, s, "GET", path, nil, header)
assert.Equal(t, http.StatusNoContent, res.Result().StatusCode)
})
}