From d9fb609bf3435dd940d0cd0de8aa64d732336101 Mon Sep 17 00:00:00 2001 From: Ryan Neal Date: Mon, 27 Mar 2017 11:30:55 -0700 Subject: [PATCH 1/2] document basic middleware testing --- website/content/guide/testing.md | 64 +++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/website/content/guide/testing.md b/website/content/guide/testing.md index 39de2a20b..8fe6c8b39 100644 --- a/website/content/guide/testing.md +++ b/website/content/guide/testing.md @@ -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). From 49d29b934367609dccac40079e2c908470e63736 Mon Sep 17 00:00:00 2001 From: Ryan Neal Date: Mon, 27 Mar 2017 11:35:09 -0700 Subject: [PATCH 2/2] update whitespace --- website/content/guide/testing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/content/guide/testing.md b/website/content/guide/testing.md index 8fe6c8b39..63b775cbe 100644 --- a/website/content/guide/testing.md +++ b/website/content/guide/testing.md @@ -179,7 +179,7 @@ And to create a test for it you just have to setup the echo and context. func TestForBatman(t *testing.T) { e := echo.New() - // create request, recorder and context for the actual query + // create request, recorder and context for the actual query r := httptest.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder() c := e.NewContext(r, w) @@ -191,7 +191,7 @@ func TestForBatman(t *testing.T) { return c.NoContent(http.StatusNoContent) } - // set the basic auth to match our test + // set the basic auth to match our test r.SetBasicAuth("batman", "j0kEr5ux!") handler := checkForBatman(next)