From 533d7a8dc147b3c9ebc09726c6bc5e32857392cd Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sat, 29 Apr 2017 11:02:57 -0700 Subject: [PATCH] Closes #918, docs updated for testing handler Signed-off-by: Vishal Rana --- context.go | 4 ++-- website/content/guide/testing.md | 24 +++++++++++------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/context.go b/context.go index 2f9854c1..b90d8db8 100644 --- a/context.go +++ b/context.go @@ -385,7 +385,7 @@ func (c *context) String(code int, s string) (err error) { } func (c *context) JSON(code int, i interface{}) (err error) { - _, pretty := c.request.URL.Query()["pretty"] + _, pretty := c.QueryParams()["pretty"] if c.echo.Debug || pretty { return c.JSONPretty(code, i, " ") } @@ -430,7 +430,7 @@ func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) { } func (c *context) XML(code int, i interface{}) (err error) { - _, pretty := c.request.URL.Query()["pretty"] + _, pretty := c.QueryParams()["pretty"] if c.echo.Debug || pretty { return c.XMLPretty(code, i, " ") } diff --git a/website/content/guide/testing.md b/website/content/guide/testing.md index 39de2a20..73675e31 100644 --- a/website/content/guide/testing.md +++ b/website/content/guide/testing.md @@ -92,27 +92,25 @@ var ( func TestCreateUser(t *testing.T) { // Setup e := echo.New() - req, err := http.NewRequest(echo.POST, "/users", strings.NewReader(userJSON)) - if assert.NoError(t, err) { - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - h := &handler{mockDB} + req := httptest.NewRequest(echo.POST, "/", strings.NewReader(userJSON)) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + h := &handler{mockDB} - // Assertions - if assert.NoError(t, h.createUser(c)) { - assert.Equal(t, http.StatusCreated, rec.Code) - assert.Equal(t, userJSON, rec.Body.String()) - } + // Assertions + if assert.NoError(t, h.createUser(c)) { + assert.Equal(t, http.StatusCreated, rec.Code) + assert.Equal(t, userJSON, rec.Body.String()) } } func TestGetUser(t *testing.T) { // Setup e := echo.New() - req := new(http.Request) + req := httptest.NewRequest(echo.GET, "/", nil) rec := httptest.NewRecorder() - c := e.NewContext(req, rec) + c := e.NewContext(req, rec) c.SetPath("/users/:email") c.SetParamNames("email") c.SetParamValues("jon@labstack.com")