How to test errors that was called by echo.NewHTTPError?


#1

In docs there is an example with echo.NewHTTPError use and I also use such approach in my code. But this method don’t writes into response, I guess this does it later somewhere that don’t triggers in tests.

My code:

func TestIncorrectAuth(t *testing.T) {
	e := echo.New()

	req := httptest.NewRequest(echo.POST, v1+"/reset", nil)
	req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
	req.Header.Set(echo.HeaderAuthorization, "INCORRECT TOKEN")

	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)

	c.SetHandler(
		ContextExtender(testDB, testConfig)(
			Authorization(
				Reset,
			),
		),
	)

	// We should get error 422
	if assert.Error(t, c.Handler()(c)) {
		assert.Equal(t, http.StatusUnprocessableEntity, rec.Code)
	}
}