✨ Implement Handler and HandlerFunc, add comprehensive tests
This commit is contained in:
parent
aa7dfb532f
commit
a136616088
5 changed files with 138 additions and 5 deletions
8
go.mod
8
go.mod
|
|
@ -1,3 +1,11 @@
|
||||||
module codeberg.org/danjones000/ezhandler
|
module codeberg.org/danjones000/ezhandler
|
||||||
|
|
||||||
go 1.24.2
|
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
10
go.sum
Normal 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=
|
||||||
|
|
@ -1 +1,48 @@
|
||||||
package ezhandler_test
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
16
helper.go
16
helper.go
|
|
@ -11,20 +11,26 @@ func NewHelper(errH ErrorHandler) *Helper {
|
||||||
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
|
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
|
||||||
|
|
||||||
// Helper provides methods to make easier, more idiomatic [http.Handler]s.
|
// Helper provides methods to make easier, more idiomatic [http.Handler]s.
|
||||||
type Helper struct{
|
type Helper struct {
|
||||||
errHandler ErrorHandler
|
errHandler ErrorHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler returns an [http.Handler] for the provided [Handler].
|
// Handler returns an [http.Handler] for the provided [Handler].
|
||||||
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
|
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
|
||||||
func (help *Helper) Handler(hnd Handler) http.Handler {
|
func (help *Helper) Handler(hnd Handler) http.Handler {
|
||||||
// TODO implement this
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
panic("unimplemented")
|
if err := hnd.ServeHTTP(w, r); err != nil {
|
||||||
|
help.errHandler(w, r, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler returns an [http.Handler] for the provided [HandlerFunc].
|
// Handler returns an [http.Handler] for the provided [HandlerFunc].
|
||||||
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
|
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
|
||||||
func (help *Helper) HandlerFunc(hnd HandlerFunc) http.Handler {
|
func (help *Helper) HandlerFunc(hnd HandlerFunc) http.Handler {
|
||||||
// TODO implement this
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
panic("unimplemented")
|
if err := hnd(w, r); err != nil {
|
||||||
|
help.errHandler(w, r, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,63 @@
|
||||||
package ezhandler_test
|
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")
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue