Implement Handler and HandlerFunc, add comprehensive tests

This commit is contained in:
Dan Jones 2025-07-07 21:39:47 -05:00
commit a136616088
5 changed files with 138 additions and 5 deletions

8
go.mod
View file

@ -1,3 +1,11 @@
module codeberg.org/danjones000/ezhandler
go 1.24.2
require github.com/stretchr/testify v1.10.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum Normal file
View file

@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -1 +1,48 @@
package ezhandler_test
import (
"net/http"
"net/http/httptest"
"testing"
"codeberg.org/danjones000/ezhandler"
"github.com/stretchr/testify/assert"
)
func TestHandlerFunc_ServeHTTP(t *testing.T) {
tests := []struct {
name string
handlerFunc ezhandler.HandlerFunc
expectedErr error
}{
{
name: "no error",
handlerFunc: ezhandler.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
w.WriteHeader(http.StatusOK)
return nil
}),
expectedErr: nil,
},
{
name: "with error",
handlerFunc: ezhandler.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
return errTest
}),
expectedErr: errTest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
rec := httptest.NewRecorder()
err := tt.handlerFunc.ServeHTTP(rec, req)
if tt.expectedErr != nil {
assert.Equal(t, tt.expectedErr, err)
} else {
assert.NoError(t, err)
}
})
}
}

View file

@ -18,13 +18,19 @@ type Helper struct{
// Handler returns an [http.Handler] for the provided [Handler].
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
func (help *Helper) Handler(hnd Handler) http.Handler {
// TODO implement this
panic("unimplemented")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := hnd.ServeHTTP(w, r); err != nil {
help.errHandler(w, r, err)
}
})
}
// Handler returns an [http.Handler] for the provided [HandlerFunc].
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
func (help *Helper) HandlerFunc(hnd HandlerFunc) http.Handler {
// TODO implement this
panic("unimplemented")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := hnd(w, r); err != nil {
help.errHandler(w, r, err)
}
})
}

View file

@ -1 +1,63 @@
package ezhandler_test
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"codeberg.org/danjones000/ezhandler"
"github.com/stretchr/testify/assert"
)
var (
errTest = errors.New("test error")
)
func TestNewHelper(t *testing.T) {
mockErrorHandler := func(w http.ResponseWriter, r *http.Request, err error) {}
helper := ezhandler.NewHelper(mockErrorHandler)
assert.NotNil(t, helper)
}
func runHelperTest(t *testing.T, name string, handlerErr, expectedErr error, expectedErrorHandlerCalled bool, handlerType string) {
t.Run(name, func(t *testing.T) {
var errorHandlerCalled bool
mockErrorHandler := func(w http.ResponseWriter, r *http.Request, err error) {
errorHandlerCalled = true
assert.Equal(t, expectedErr, err)
}
helper := ezhandler.NewHelper(mockErrorHandler)
req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
rec := httptest.NewRecorder()
var wrappedHandler http.Handler
switch handlerType {
case "Handler":
mockHandler := ezhandler.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
return handlerErr
})
wrappedHandler = helper.Handler(mockHandler)
case "HandlerFunc":
mockHandlerFunc := ezhandler.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
return handlerErr
})
wrappedHandler = helper.HandlerFunc(mockHandlerFunc)
}
wrappedHandler.ServeHTTP(rec, req)
assert.Equal(t, expectedErrorHandlerCalled, errorHandlerCalled)
})
}
func TestHelper_Handler(t *testing.T) {
runHelperTest(t, "no error", nil, nil, false, "Handler")
runHelperTest(t, "with error", errTest, errTest, true, "Handler")
}
func TestHelper_HandlerFunc(t *testing.T) {
runHelperTest(t, "no error", nil, nil, false, "HandlerFunc")
runHelperTest(t, "with error", errTest, errTest, true, "HandlerFunc")
}