Update echo example to make it more clear
TudorHulban opened this issue · 3 comments
hi,
nice approach but too bad that for echo you would need the application to be written as per your way.
for me this limits the sharing of the echo logger and context.
note that this is different than the way labstack presented their use and also testing.
maybe you could ease up the handler so it can be prepared even if not using your app struct approach.
Hey,
apitest doesn't impose any constraints on how you develop your echo application, it simply needs an http.Handler to work, which is implemented by the *echo.Echo type.
Perhaps you could provide an example of how you are using echo and we would be happy to point you in the right direction with how to use apitest with it.
Thanks,
George
Having re-read your question: you don't need the wrapping application struct at all. The simplest example of echo working would be:
func main() {
app := CreateApp()
err := app.Start("localhost:8080")
if err != nil {
panic(err)
}
}
func CreateApp() *echo.Echo {
app := echo.New()
app.GET("/blah", func(c echo.Context) error {
return c.JSON(http.StatusOK, `{"status": "all good!"}`)
})
return app
}
func TestApp(t *testing.T) {
apitest.New().
Handler(CreateApp()).
Get("/blah").
Expect(t).
Status(http.StatusOK).
Assert(jsonpath.Equal(`$.status`, "all good!")).
End()
}
Thank you very much for fast response. Works like a charm:
func Test1Live(t *testing.T) {
e := echo.New()
e.Logger.SetLevel(log.DEBUG)
e.GET(commons.EndpointLive, Live)
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, commons.EndpointLive, nil)
e.ServeHTTP(w, req)
resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func Test2Live(t *testing.T) {
e := echo.New()
e.Logger.SetLevel(log.DEBUG)
e.GET(commons.EndpointLive, Live)
apitest.New().
Handler(e).
Get(commons.EndpointLive).
Expect(t).
Status(http.StatusOK).
Body(`{"ok":"OK"}`).
End()
}
Much nicer with your code.