core/internal/port/handler_http_api_test.go

37 lines
746 B
Go

package port_test
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"gitea.dwysokinski.me/twhelp/corev3/internal/port"
)
type apiHTTPHandlerConfig struct {
options []port.APIHTTPHandlerOption
}
func newAPIHTTPHandler(tb testing.TB, opts ...func(cfg *apiHTTPHandlerConfig)) http.Handler {
tb.Helper()
cfg := &apiHTTPHandlerConfig{}
for _, opt := range opts {
opt(cfg)
}
return port.NewAPIHTTPHandler(cfg.options...)
}
func doRequest(h http.Handler, method, target string, body io.Reader) *http.Response {
return doCustomRequest(h, httptest.NewRequest(method, target, body))
}
func doCustomRequest(h http.Handler, r *http.Request) *http.Response {
rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
return rr.Result()
}