Documentation
¶
Overview ¶
Package conntest provides lightweight test helpers for writing connector unit tests. It eliminates the boilerplate of wiring up connector.Ctx and httptest.Server so authors can focus on testing connector logic, not setup.
Typical usage:
func TestMyOp(t *testing.T) {
srv := conntest.JSONServer(t, map[string]any{"ok": true, "id": 42})
c := conntest.Ctx(t,
map[string]string{"base_url": srv.URL, "token": "secret"},
map[string]string{"resource": "users"},
)
result, err := myOp(c)
require.NoError(t, err)
// assert result...
}
Index ¶
- func CaptureRequest(t testing.TB, payload any) (*httptest.Server, *capturedRequest)
- func Ctx(t testing.TB, configs, input map[string]string) *connector.Ctx
- func CtxWithServer(t testing.TB, fn http.HandlerFunc, configs, input map[string]string) (*connector.Ctx, *httptest.Server)
- func JSONServer(t testing.TB, payload any) *httptest.Server
- func JSONServerWithStatus(t testing.TB, status int, payload any) *httptest.Server
- func Server(t testing.TB, fn http.HandlerFunc) *httptest.Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureRequest ¶
CaptureRequest starts a mock server that records the last request it received and returns payload as JSON. The returned *http.Request pointer is populated after the first call. Useful for asserting request shape (headers, method, body) alongside response handling.
func Ctx ¶
Ctx creates a connector.Ctx with the given configs and input maps. Uses http.DefaultClient and a background context. Pass nil for either map to get an empty map.
func CtxWithServer ¶
func CtxWithServer(t testing.TB, fn http.HandlerFunc, configs, input map[string]string) (*connector.Ctx, *httptest.Server)
CtxWithServer combines Server and Ctx: starts a mock server, returns a Ctx whose HTTP client is wired to hit it. Use srv.URL when building config values like base_url.
srv, c := conntest.CtxWithServer(t, handler, configs, input) // configs["base_url"] should use srv.URL
func JSONServer ¶
JSONServer starts a mock server that always returns payload as JSON with HTTP 200. Use when the connector only needs a static response.
func JSONServerWithStatus ¶
JSONServerWithStatus starts a mock server that returns payload as JSON with the given HTTP status code. Use for testing error handling paths.
Types ¶
This section is empty.