Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions website/content/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ Handler below retrieves user by id from the database. If user is not found it re
- On success `200 - OK`
- On error `404 - Not Found` if user is not found otherwise `500 - Internal Server Error`

### CheckEmail

- On invalid email `400 - BadRequest`
- On valid email `nil`

`handler.go`

```go
package handler

import (
"net/http"
"strings"

"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -65,6 +71,17 @@ func (h *handler) getUser(c echo.Context) error {
}
return c.JSON(http.StatusOK, user)
}

func (h *handler) checkEmail(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
email := c.Param("email")
if !strings.Contains(email, "@") {
echo.NewHTTPError(http.StatusBadRequest, "invalid email address")
}
c.Set("validEmail", true)
return next(c)
}
}
```

`handler_test.go`
Expand Down Expand Up @@ -153,6 +170,63 @@ req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)

## Testing Middleware

*TBD*
`handler_test.go`

```go
package handler

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

func TestCheckEmail(t *testing.T) {
var tcs = []struct{
email string
err string
}{
{
email: "valid@email.com",
err: "",
},
{
email: "invalid",
err: "invalid email address",
},
}

for _, tc := range tcs {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("email")
c.SetParamValues(tc.email)
h := &handler{mockDB}

// Check that "validEmail" was properly set.
var next = h.checkEmail(func(ec echo.Context) error {
var e = ec.Get("validEmail")
var email, ok = e.(bool)
assert.True(t, ok)
assert.Equal(t, email, tc.email)

return nil
})

err := next(c)
if tc.err == "" {
assert.EqualError(t, err, tc.err)
} else {
assert.Nil(t, err)
}
}
}
```

For now you can look into built-in middleware [test cases](https://github.com/labstack/echo/tree/master/middleware).
For additional examples, check the built-in middleware [test cases](https://github.com/labstack/echo/tree/master/middleware).