Skip to content
Closed
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
64 changes: 62 additions & 2 deletions website/content/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,66 @@ req, err := http.NewRequest(echo.POST, "/?"+q.Encode(), nil)

## Testing Middleware

*TBD*
Middleware is declared as:

For now you can look into built-in middleware [test cases](https://github.com/labstack/echo/tree/master/middleware).
``` go
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
type HandlerFunc func(c Context) error
```

meaning we can make a simple middleware that just checks the user's auth credentials

``` go
func checkForBatman(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
user, pass, ok := c.Request().BasicAuth()
if ok && pass == "j0kEr5ux!" && user == "batman" {
return next(c)
}
return echo.ErrUnauthorized
}
}
```

And to create a test for it you just have to setup the echo and context.

``` go
func TestForBatman(t *testing.T) {
e := echo.New()

// create request, recorder and context for the actual query
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
c := e.NewContext(r, w)

// create an endpoint to call and flag to verify it was called
called := false
next := func(c echo.Context) error {
called = true
return c.NoContent(http.StatusNoContent)
}

// set the basic auth to match our test
r.SetBasicAuth("batman", "j0kEr5ux!")
handler := checkForBatman(next)

// now make the actual call with the context created above
assert.NoError(t, handler(c))

// test that we got back what we wanted
assert.True(t, called)
assert.Equal(t, http.StatusNoContent, w.Code)

// reset for a new call with a bad value
r.SetBasicAuth("joker", "batty")
called = false
err := handler(c)
assert.NotNil(t, err)
assert.Equal(t, http.StatusUnauthorized, err.(*echo.HTTPError).Code)

// validate that we didn't call through
assert.False(t, called)
}
```

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