Started on controller tests.
This commit is contained in:
parent
098d1b7eb2
commit
c6c9ed7fd2
3 changed files with 51 additions and 10 deletions
|
|
@ -8,8 +8,17 @@ import (
|
|||
"testing"
|
||||
|
||||
"goweb/config"
|
||||
"goweb/msg"
|
||||
"goweb/services"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
|
|
@ -33,3 +42,38 @@ func newContext(url string) echo.Context {
|
|||
req := httptest.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
||||
return c.Web.NewContext(req, httptest.NewRecorder())
|
||||
}
|
||||
|
||||
func initSesssion(t *testing.T, ctx echo.Context) {
|
||||
// Simulate an HTTP request through the session middleware to initiate the session
|
||||
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
|
||||
handler := mw(echo.NotFoundHandler)
|
||||
assert.Error(t, handler(ctx))
|
||||
}
|
||||
|
||||
func TestController_Redirect(t *testing.T) {
|
||||
ctx := newContext("/abc")
|
||||
ctr := NewController(c)
|
||||
err := ctr.Redirect(ctx, "home")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", ctx.Response().Header().Get(echo.HeaderLocation))
|
||||
assert.Equal(t, http.StatusFound, ctx.Response().Status)
|
||||
}
|
||||
|
||||
func TestController_SetValidationErrorMessages(t *testing.T) {
|
||||
type example struct {
|
||||
Name string `validate:"required" label:"Label test"`
|
||||
}
|
||||
e := example{}
|
||||
v := validator.New()
|
||||
err := v.Struct(e)
|
||||
require.Error(t, err)
|
||||
|
||||
ctx := newContext("/")
|
||||
initSesssion(t, ctx)
|
||||
ctr := NewController(c)
|
||||
ctr.SetValidationErrorMessages(ctx, err, e)
|
||||
|
||||
msgs := msg.Get(ctx, msg.TypeDanger)
|
||||
require.Len(t, msgs, 1)
|
||||
assert.Equal(t, "<strong>Label test</strong> is required.", msgs[0])
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue