✨ Refactor ResponseHelper and add ResponderHandler
- Modified ResponseHelper.Body signature to return an io.Reader and an error. - Updated ResponseHandler.ServeHTTP to handle errors from ResponseHelper.Body. - Implemented JSONResponse and JSONResponseWithStatus functions for easier JSON responses. - Added comprehensive unit tests for JSON response handling, including error scenarios. - Extended Helper.ResponderHandler with a new test case to ensure proper error handling and response generation. - Resolved linting issues related to dynamic error definition and function length in tests.
This commit is contained in:
parent
a136616088
commit
1c16a893d6
5 changed files with 270 additions and 1 deletions
|
|
@ -44,6 +44,14 @@ func runHelperTest(t *testing.T, name string, handlerErr, expectedErr error, exp
|
|||
return handlerErr
|
||||
})
|
||||
wrappedHandler = helper.HandlerFunc(mockHandlerFunc)
|
||||
case "ResponderHandler":
|
||||
mockResponseHandler := ezhandler.ResponseHandler(func(r *http.Request) (ezhandler.ResponseHelper, error) {
|
||||
if handlerErr != nil {
|
||||
return nil, handlerErr
|
||||
}
|
||||
return ezhandler.JSONResponse(map[string]string{"status": "ok"}), nil
|
||||
})
|
||||
wrappedHandler = helper.ResponderHandler(mockResponseHandler)
|
||||
}
|
||||
|
||||
wrappedHandler.ServeHTTP(rec, req)
|
||||
|
|
@ -61,3 +69,8 @@ func TestHelper_HandlerFunc(t *testing.T) {
|
|||
runHelperTest(t, "no error", nil, nil, false, "HandlerFunc")
|
||||
runHelperTest(t, "with error", errTest, errTest, true, "HandlerFunc")
|
||||
}
|
||||
|
||||
func TestHelper_ResponderHandler(t *testing.T) {
|
||||
runHelperTest(t, "no error", nil, nil, false, "ResponderHandler")
|
||||
runHelperTest(t, "with error", errTest, errTest, true, "ResponderHandler")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue