saasitone/pkg/middleware/cache_test.go

56 lines
1.8 KiB
Go

package middleware
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
"git.grosinger.net/tgrosinger/saasitone/pkg/tests"
"git.grosinger.net/tgrosinger/saasitone/templ/pages"
"git.grosinger.net/tgrosinger/saasitone/templ/layouts"
)
func TestServeCachedPage(t *testing.T) {
// Cache a page
ctx, rec := tests.NewContext(c.Web, "/cache")
p := page.New(ctx)
p.Cache.Enabled = true
p.Cache.Expiration = time.Minute
p.StatusCode = http.StatusCreated
p.Headers["a"] = "b"
p.Headers["c"] = "d"
p.LayoutComponent = layouts.HTMX
err := c.TemplateRenderer.RenderPageTempl(ctx, p, pages.Cache(p, &pages.CacheForm{}, nil))
output := rec.Body.Bytes()
require.NoError(t, err)
// Request the URL of the cached page
ctx, rec = tests.NewContext(c.Web, "/cache")
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
assert.NoError(t, err)
assert.Equal(t, p.StatusCode, ctx.Response().Status)
assert.Equal(t, p.Headers["a"], ctx.Response().Header().Get("a"))
assert.Equal(t, p.Headers["c"], ctx.Response().Header().Get("c"))
assert.Equal(t, output, rec.Body.Bytes())
// Login and try again
tests.InitSession(ctx)
err = c.Auth.Login(ctx, usr.ID)
require.NoError(t, err)
_ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
assert.Nil(t, err)
}
func TestCacheControl(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/")
_ = tests.ExecuteMiddleware(ctx, CacheControl(time.Second*5))
assert.Equal(t, "public, max-age=5", ctx.Response().Header().Get("Cache-Control"))
_ = tests.ExecuteMiddleware(ctx, CacheControl(0))
assert.Equal(t, "no-cache, no-store", ctx.Response().Header().Get("Cache-Control"))
}