Replies: 1 comment
-
This is example with 2 versions of handlers - handler function and handler method func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/ok1", okEndpointFunc) // handler function
ctrl := controller{logger: e.Logger}
e.GET("/ok2", ctrl.okEndpoint) // handler method with controller struct
if err := e.Start(":8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
}
func okEndpointFunc(c echo.Context) error {
test := c.QueryParam("test")
if test == "error" {
return echo.ErrBadRequest
}
return c.String(http.StatusTeapot, "OK:"+test)
}
type controller struct {
logger echo.Logger // just for an example
}
func (ctrl *controller) okEndpoint(c echo.Context) error {
test := c.QueryParam("test")
if test == "error" {
return echo.ErrBadRequest
}
return c.String(http.StatusTeapot, "OK:"+test)
} Test for handler function import (
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandlerFunc(t *testing.T) {
var testCases = []struct {
name string
whenQuery string
expectBody string
expectCode int
expectError string
}{
{
name: "ok",
whenQuery: "test=1",
expectBody: "OK:1",
expectCode: http.StatusTeapot,
},
{
name: "nok",
whenQuery: "test=error",
expectError: "code=400, message=Bad Request",
expectBody: "", // no body when testing only handler
expectCode: http.StatusOK, // no error code yet when testing only handler
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "http://somewhere?"+tc.whenQuery, nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := okEndpointFunc(c)
assert.Equal(t, tc.expectBody, rec.Body.String())
assert.Equal(t, tc.expectCode, rec.Code)
if tc.expectError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectError)
}
})
}
} Test for handler method: func TestHandlerMethod(t *testing.T) {
var testCases = []struct {
name string
whenQuery string
expectBody string
expectCode int
expectError string
}{
{
name: "ok",
whenQuery: "test=1",
expectBody: "OK:1",
expectCode: http.StatusTeapot,
},
{
name: "nok",
whenQuery: "test=error",
expectError: "code=400, message=Bad Request",
expectBody: "", // no body when testing only handler
expectCode: http.StatusOK, // no error code yet when testing only handler
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "http://somewhere?"+tc.whenQuery, nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
ctrl := controller{logger: nil}
err := ctrl.okEndpoint(c)
assert.Equal(t, tc.expectBody, rec.Body.String())
assert.Equal(t, tc.expectCode, rec.Code)
if tc.expectError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectError)
}
})
}
} This is example what serves whole echo stack in test (including middlewares) func TestHandlerFunctionWithMiddlewares(t *testing.T) {
var testCases = []struct {
name string
whenQuery string
expectBody string
expectCode int
}{
{
name: "ok",
whenQuery: "test=1",
expectBody: "OK:1",
expectCode: http.StatusTeapot,
},
{
name: "nok",
whenQuery: "test=error",
expectBody: "{\"message\":\"Bad Request\"}\n",
expectCode: http.StatusBadRequest,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := echo.New()
e.Use(middleware.Logger())
req := httptest.NewRequest(http.MethodGet, "http://somewhere/ok?"+tc.whenQuery, nil)
rec := httptest.NewRecorder()
e.GET("/ok", okEndpointFunc)
e.ServeHTTP(rec, req)
assert.Equal(t, tc.expectBody, rec.Body.String())
assert.Equal(t, tc.expectCode, rec.Code)
})
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to test my endpoints, but I can't execute the test call, pls help me 😄
Beta Was this translation helpful? Give feedback.
All reactions