119 lines
3.7 KiB
Go
119 lines
3.7 KiB
Go
package httputil
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"code.nochebuena.dev/einherjar/contracts/logging"
|
|
"code.nochebuena.dev/einherjar/core/logz"
|
|
"code.nochebuena.dev/einherjar/core/valid"
|
|
)
|
|
|
|
type tReq struct {
|
|
Name string `json:"name" validate:"required"`
|
|
}
|
|
type tRes struct {
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
func discardLogger() logging.Logger { return logz.New(logz.Config{Writer: io.Discard}) }
|
|
|
|
// Default: Handle writes 200 with the JSON payload (regression — no opts).
|
|
func TestHandle_Default200(t *testing.T) {
|
|
h := Handle(valid.New(), discardLogger(), func(context.Context, tReq) (tRes, error) {
|
|
return tRes{ID: "x"}, nil
|
|
})
|
|
rec := httptest.NewRecorder()
|
|
h(rec, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"name":"a"}`)))
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", rec.Code)
|
|
}
|
|
if got := strings.TrimSpace(rec.Body.String()); got != `{"id":"x"}` {
|
|
t.Errorf("body = %q, want {\"id\":\"x\"}", got)
|
|
}
|
|
}
|
|
|
|
// WithStatus(201) makes a resource-creating Handle return 201 Created + the body.
|
|
func TestHandle_WithStatusCreated(t *testing.T) {
|
|
h := Handle(valid.New(), discardLogger(), func(context.Context, tReq) (tRes, error) {
|
|
return tRes{ID: "x"}, nil
|
|
}, WithStatus(http.StatusCreated))
|
|
rec := httptest.NewRecorder()
|
|
h(rec, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"name":"a"}`)))
|
|
if rec.Code != http.StatusCreated {
|
|
t.Fatalf("status = %d, want 201", rec.Code)
|
|
}
|
|
if got := strings.TrimSpace(rec.Body.String()); got != `{"id":"x"}` {
|
|
t.Errorf("body = %q, want the payload", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleNoBody_WithStatus(t *testing.T) {
|
|
h := HandleNoBody(discardLogger(), func(context.Context) (tRes, error) {
|
|
return tRes{ID: "y"}, nil
|
|
}, WithStatus(http.StatusAccepted))
|
|
rec := httptest.NewRecorder()
|
|
h(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want 202", rec.Code)
|
|
}
|
|
}
|
|
|
|
// Default: HandleEmpty writes 204 with no body.
|
|
func TestHandleEmpty_Default204(t *testing.T) {
|
|
h := HandleEmpty(valid.New(), discardLogger(), func(context.Context, tReq) error { return nil })
|
|
rec := httptest.NewRecorder()
|
|
h(rec, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"name":"a"}`)))
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want 204", rec.Code)
|
|
}
|
|
if rec.Body.Len() != 0 {
|
|
t.Errorf("expected empty body, got %q", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// WithStatus on a body-less adapter changes the code but keeps the empty body.
|
|
func TestHandleEmpty_WithStatusAccepted_NoBody(t *testing.T) {
|
|
h := HandleEmpty(valid.New(), discardLogger(), func(context.Context, tReq) error { return nil }, WithStatus(http.StatusAccepted))
|
|
rec := httptest.NewRecorder()
|
|
h(rec, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"name":"a"}`)))
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want 202", rec.Code)
|
|
}
|
|
if rec.Body.Len() != 0 {
|
|
t.Errorf("expected empty body, got %q", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// A non-2xx code is a wiring mistake: WithStatus panics at construction so the
|
|
// service fails to boot rather than emitting a wrong status at request time.
|
|
func TestWithStatus_PanicsOnNon2xx(t *testing.T) {
|
|
for _, code := range []int{0, 100, 199, 300, 404, 500, 1000} {
|
|
func() {
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Errorf("WithStatus(%d) did not panic", code)
|
|
}
|
|
}()
|
|
_ = WithStatus(code)
|
|
}()
|
|
}
|
|
}
|
|
|
|
func TestWithStatus_Allows2xx(t *testing.T) {
|
|
for _, code := range []int{200, 201, 202, 204, 299} {
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("WithStatus(%d) panicked: %v", code, r)
|
|
}
|
|
}()
|
|
_ = WithStatus(code)
|
|
}()
|
|
}
|
|
}
|