Move controller to the template renderer (#68)
This commit is contained in:
parent
baa391fb20
commit
8eafb6b666
26 changed files with 654 additions and 679 deletions
|
|
@ -1,12 +1,13 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mikestefanello/pagoda/pkg/page"
|
||||
"github.com/mikestefanello/pagoda/pkg/tests"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
|
|
@ -15,38 +16,34 @@ import (
|
|||
|
||||
func TestServeCachedPage(t *testing.T) {
|
||||
// Cache a page
|
||||
cp := CachedPage{
|
||||
URL: "/cache",
|
||||
HTML: []byte("html"),
|
||||
Headers: make(map[string]string),
|
||||
StatusCode: http.StatusCreated,
|
||||
}
|
||||
cp.Headers["a"] = "b"
|
||||
cp.Headers["c"] = "d"
|
||||
|
||||
err := c.Cache.
|
||||
Set().
|
||||
Group(CachedPageGroup).
|
||||
Key(cp.URL).
|
||||
Data(cp).
|
||||
Save(context.Background())
|
||||
ctx, rec := tests.NewContext(c.Web, "/cache")
|
||||
p := page.New(ctx)
|
||||
p.Layout = templates.LayoutHTMX
|
||||
p.Name = templates.PageHome
|
||||
p.Cache.Enabled = true
|
||||
p.Cache.Expiration = time.Minute
|
||||
p.StatusCode = http.StatusCreated
|
||||
p.Headers["a"] = "b"
|
||||
p.Headers["c"] = "d"
|
||||
err := c.TemplateRenderer.RenderPage(ctx, p)
|
||||
output := rec.Body.Bytes()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Request the URL of the cached page
|
||||
ctx, rec := tests.NewContext(c.Web, cp.URL)
|
||||
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.Cache))
|
||||
ctx, rec = tests.NewContext(c.Web, "/cache")
|
||||
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cp.StatusCode, ctx.Response().Status)
|
||||
assert.Equal(t, cp.Headers["a"], ctx.Response().Header().Get("a"))
|
||||
assert.Equal(t, cp.Headers["c"], ctx.Response().Header().Get("c"))
|
||||
assert.Equal(t, cp.HTML, rec.Body.Bytes())
|
||||
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.Cache))
|
||||
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.TemplateRenderer))
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue