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
53
pkg/handlers/handlers_test.go
Normal file
53
pkg/handlers/handlers_test.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/htmx"
|
||||
"github.com/mikestefanello/pagoda/pkg/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetSetHandlers(t *testing.T) {
|
||||
handlers = []Handler{}
|
||||
assert.Empty(t, GetHandlers())
|
||||
h := new(Pages)
|
||||
Register(h)
|
||||
got := GetHandlers()
|
||||
require.Len(t, got, 1)
|
||||
assert.Equal(t, h, got[0])
|
||||
}
|
||||
|
||||
func TestRedirect(t *testing.T) {
|
||||
c.Web.GET("/path/:first/and/:second", func(c echo.Context) error {
|
||||
return nil
|
||||
}).Name = "redirect-test"
|
||||
|
||||
t.Run("normal", func(t *testing.T) {
|
||||
ctx, _ := tests.NewContext(c.Web, "/abc")
|
||||
err := redirect(ctx, "redirect-test", "one", "two")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "/path/one/and/two", ctx.Response().Header().Get(echo.HeaderLocation))
|
||||
assert.Equal(t, http.StatusFound, ctx.Response().Status)
|
||||
})
|
||||
|
||||
t.Run("htmx boosted", func(t *testing.T) {
|
||||
ctx, _ := tests.NewContext(c.Web, "/abc")
|
||||
ctx.Request().Header.Set(htmx.HeaderBoosted, "true")
|
||||
err := redirect(ctx, "redirect-test", "one", "two")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "/path/one/and/two", ctx.Response().Header().Get(htmx.HeaderRedirect))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFail(t *testing.T) {
|
||||
err := fail(errors.New("err message"), "log message")
|
||||
require.IsType(t, new(echo.HTTPError), err)
|
||||
he := err.(*echo.HTTPError)
|
||||
assert.Equal(t, http.StatusInternalServerError, he.Code)
|
||||
assert.Equal(t, "log message: err message", he.Message)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue