conntest

package
v0.11.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2026 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureRequest

func CaptureRequest(t testing.TB, payload any) (*httptest.Server, *capturedRequest)

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

func Ctx(t testing.TB, configs, input map[string]string) *connector.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

func JSONServer(t testing.TB, payload any) *httptest.Server

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

func JSONServerWithStatus(t testing.TB, status int, payload any) *httptest.Server

JSONServerWithStatus starts a mock server that returns payload as JSON with the given HTTP status code. Use for testing error handling paths.

func Server

func Server(t testing.TB, fn http.HandlerFunc) *httptest.Server

Server starts an httptest.Server using fn as its handler and registers t.Cleanup to close it. The server URL is accessible via srv.URL.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL