Documentation
¶
Overview ¶
Package apitest provides HTTP response assertions for application tests.
The package is stable core API for tests. It builds on httptest.ResponseRecorder semantics and complements contracttest: contracttest checks route registries and OpenAPI metadata, while apitest checks concrete HTTP responses emitted by handlers.
Use AssertProblem, AssertProblemCode, AssertValidationFields, AssertJSON, AssertHeader, AssertRateLimitHeaders, AssertETag, AssertDeprecationHeaders, AssertPagination, AssertOperationAccepted, AssertWebhookSignature, and AssertOpenAPIGolden to keep API tests focused on externally visible behavior.
These helpers call testing.TB.Fatal when assertions fail. They are not runtime middleware and should not be imported by production handlers. For examples, see docs/cookbook.md.
Purpose: See the package summary above. Import: `github.com/aatuh/api-toolkit/v4/apitest`. Example: See docs/api-reference.md for package example links and docs/cookbook.md for task recipes. Errors: Constructors, parsers, and handlers return or write documented errors according to their signatures; packages with plain data types do not add hidden error channels. Concurrency: Treat configured middleware and helpers as immutable after construction; request and response values remain request-scoped unless a type documents stronger guarantees. Stability: Stable core API under VERSIONING.md and scripts/apicheck.sh. When not to use: Prefer net/http, application-owned types, or narrower helpers when this package contract is not needed.
Index ¶
- func AssertDeprecationHeaders(t testing.TB, recorder *httptest.ResponseRecorder)
- func AssertETag(t testing.TB, recorder *httptest.ResponseRecorder, want string)
- func AssertHeader(t testing.TB, recorder *httptest.ResponseRecorder, name, want string)
- func AssertJSON(t testing.TB, recorder *httptest.ResponseRecorder, want any)
- func AssertOpenAPIGolden(t testing.TB, got, golden []byte)
- func AssertOperationAccepted(t testing.TB, recorder *httptest.ResponseRecorder, location string)
- func AssertPagination(t testing.TB, recorder *httptest.ResponseRecorder, count, limit int)
- func AssertProblem(t testing.TB, recorder *httptest.ResponseRecorder, status int) map[string]any
- func AssertProblemCode(t testing.TB, recorder *httptest.ResponseRecorder, code httpx.ProblemCode)
- func AssertRateLimitHeaders(t testing.TB, recorder *httptest.ResponseRecorder)
- func AssertValidationFields(t testing.TB, recorder *httptest.ResponseRecorder, fields ...string)
- func AssertWebhookSignature(t testing.TB, header http.Header, name string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertDeprecationHeaders ¶
func AssertDeprecationHeaders(t testing.TB, recorder *httptest.ResponseRecorder)
AssertDeprecationHeaders asserts runtime deprecation headers.
func AssertETag ¶
func AssertETag(t testing.TB, recorder *httptest.ResponseRecorder, want string)
AssertETag asserts the ETag response header.
func AssertHeader ¶
func AssertHeader(t testing.TB, recorder *httptest.ResponseRecorder, name, want string)
AssertHeader asserts a response header value.
func AssertJSON ¶
func AssertJSON(t testing.TB, recorder *httptest.ResponseRecorder, want any)
AssertJSON asserts a JSON response body.
Example ¶
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/aatuh/api-toolkit/v4/apitest"
"github.com/aatuh/api-toolkit/v4/httpx"
)
func main() {
recorder := httptest.NewRecorder()
httpx.WriteJSON(recorder, http.StatusOK, map[string]string{"status": "ok"})
var t testing.T
apitest.AssertJSON(&t, recorder, map[string]string{"status": "ok"})
}
Output:
func AssertOpenAPIGolden ¶
AssertOpenAPIGolden asserts normalized OpenAPI JSON against a golden document.
func AssertOperationAccepted ¶
func AssertOperationAccepted(t testing.TB, recorder *httptest.ResponseRecorder, location string)
AssertOperationAccepted asserts a 202 operation response.
func AssertPagination ¶
func AssertPagination(t testing.TB, recorder *httptest.ResponseRecorder, count, limit int)
AssertPagination asserts common list metadata values.
func AssertProblem ¶
AssertProblem asserts a Problem Details response and returns the decoded body.
func AssertProblemCode ¶
func AssertProblemCode(t testing.TB, recorder *httptest.ResponseRecorder, code httpx.ProblemCode)
AssertProblemCode asserts a typed problem code extension.
func AssertRateLimitHeaders ¶
func AssertRateLimitHeaders(t testing.TB, recorder *httptest.ResponseRecorder)
AssertRateLimitHeaders asserts standard quota headers exist.
func AssertValidationFields ¶
func AssertValidationFields(t testing.TB, recorder *httptest.ResponseRecorder, fields ...string)
AssertValidationFields asserts validation field names in a Problem Details response.
Types ¶
This section is empty.