Documentation
¶
Overview ¶
Package testing provides testing utilities for HTTP handlers and common test patterns.
This package provides helpers for testing HTTP handlers, including: - Response status and body assertions - JSON comparison - HTTP handler testing helpers - Stub helpers
Example:
func TestCreateUser(t *testing.T) {
rr := testing.RunHandler(handler, http.MethodPost, "/users", userJSON)
testing.AssertStatus(t, rr.Code, http.StatusCreated)
testing.AssertJSON(t, rr.Body.String(), expected)
}
Index ¶
- func AssertBody(t *testing.T, got, want string)
- func AssertError(t *testing.T, err error)
- func AssertHeader(t *testing.T, header http.Header, key, want string)
- func AssertJSON(t *testing.T, got, want string)
- func AssertJSONContains(t *testing.T, body, want string)
- func AssertNoError(t *testing.T, err error)
- func AssertStatus(t *testing.T, got, want int)
- func AssertStatusBadRequest(t *testing.T, code int)
- func AssertStatusCreated(t *testing.T, code int)
- func AssertStatusForbidden(t *testing.T, code int)
- func AssertStatusInternalError(t *testing.T, code int)
- func AssertStatusNotFound(t *testing.T, code int)
- func AssertStatusOK(t *testing.T, code int)
- func AssertStatusUnauthorized(t *testing.T, code int)
- func JSONBody(v any) string
- func JSONEqual(t *testing.T, got, want string) bool
- func NewJSONRequest(method, path string, body any) *http.Request
- func NewRequest(method, path, body string) *http.Request
- func NewRequestWithHeaders(method, path, body string, headers map[string]string) *http.Request
- func RunHandler(t *testing.T, handler http.HandlerFunc, method, path, body string) *httptest.ResponseRecorder
- type StubCache
- type StubDB
- type StubWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertBody ¶
AssertBody asserts the body matches expected.
func AssertError ¶
AssertError asserts the error is not nil.
func AssertHeader ¶
AssertHeader asserts a response header matches expected.
func AssertJSON ¶
AssertJSON asserts the JSON body matches expected (order-independent).
func AssertJSONContains ¶
AssertJSONContains asserts the JSON contains the expected field/value.
func AssertNoError ¶
AssertNoError asserts the error is nil.
func AssertStatus ¶
AssertStatus asserts the response status matches expected.
func AssertStatusBadRequest ¶
AssertStatusBadRequest asserts the response is 400 Bad Request.
func AssertStatusCreated ¶
AssertStatusCreated asserts the response is 201 Created.
func AssertStatusForbidden ¶
AssertStatusForbidden asserts the response is 403 Forbidden.
func AssertStatusInternalError ¶
AssertStatusInternalError asserts the response is 500 Internal Server Error.
func AssertStatusNotFound ¶
AssertStatusNotFound asserts the response is 404 Not Found.
func AssertStatusOK ¶
AssertStatusOK asserts the response is 200 OK.
func AssertStatusUnauthorized ¶
AssertStatusUnauthorized asserts the response is 401 Unauthorized.
func NewJSONRequest ¶
NewJSONRequest creates a JSON request from a value.
func NewRequest ¶
NewRequest creates a new HTTP request for testing.
func NewRequestWithHeaders ¶
NewRequestWithHeaders creates a request with custom headers.
func RunHandler ¶
func RunHandler(t *testing.T, handler http.HandlerFunc, method, path, body string) *httptest.ResponseRecorder
RunHandler tests a standard HTTP handler.
Types ¶
type StubWriter ¶
StubWriter is a stub http.ResponseWriter for testing.
func (*StubWriter) Header ¶
func (w *StubWriter) Header() http.Header
Header returns the header map.
func (*StubWriter) WriteHeader ¶
func (w *StubWriter) WriteHeader(code int)
WriteHeader writes the status code.