testutil

package
v10.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package testutil provides shared test helpers for the api2convert SDK: an injectable fake HTTP sender, a recording sleeper, and real loopback servers for the security suite. It drives the SDK through its public options only (WithHTTPSender / WithSleeper / WithRand), so it never needs package-internal access.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RedirectTo

func RedirectTo(location string) http.HandlerFunc

RedirectTo returns a handler that responds with a 302 to location.

func Respond

func Respond(status int, body string) http.HandlerFunc

Respond returns a handler that writes status and body.

func Sign

func Sign(payload, secret string) string

Sign computes the hex HMAC-SHA256 the server uses to sign webhooks.

Types

type FakeSender

type FakeSender struct {
	// contains filtered or unexported fields
}

FakeSender implements api2convert.HttpSender: it records every request and returns canned responses from a FIFO queue. Real network I/O adds nothing for header/body/error assertions, so unit tests use this; the security suite uses real loopback servers only where a genuine redirect round-trip matters.

func (*FakeSender) AddError

func (f *FakeSender) AddError(err error) *FakeSender

AddError queues a transport-level error (as if the network call failed).

func (*FakeSender) AddJSON

func (f *FakeSender) AddJSON(status int, body any, header ...http.Header) *FakeSender

AddJSON queues a JSON response.

func (*FakeSender) AddRaw

func (f *FakeSender) AddRaw(status int, b []byte, header ...http.Header) *FakeSender

AddRaw queues a raw-bytes response.

func (*FakeSender) AddText

func (f *FakeSender) AddText(status int, s string, header ...http.Header) *FakeSender

AddText queues a text response.

func (*FakeSender) At

func (f *FakeSender) At(i int) RecordedRequest

At returns the i-th recorded request.

func (*FakeSender) Count

func (f *FakeSender) Count() int

Count returns the number of recorded requests.

func (*FakeSender) Fail added in v10.3.0

func (f *FakeSender) Fail(t TB) *FakeSender

Fail wires a TB (e.g. *testing.T) so an empty fixture queue fails the test immediately instead of returning a plain error the code under test might mistake for the behavior it is exercising.

func (*FakeSender) Last

func (f *FakeSender) Last() RecordedRequest

Last returns the most recently recorded request.

func (*FakeSender) Requests

func (f *FakeSender) Requests() []RecordedRequest

Requests returns a snapshot of all recorded requests.

func (*FakeSender) Send

Send records the request and returns the next queued response. It honors ctx cancellation and propagates MakeBody/body-read errors, mirroring the real sender, so those guarantee-critical paths are actually exercised.

type RecordedRequest

type RecordedRequest struct {
	Method          string
	URL             string
	Header          http.Header
	FollowRedirects bool
	Replayable      bool
	Timeout         time.Duration
	Stream          bool
	Body            []byte
}

RecordedRequest is a captured outbound request (the Go analog of Node's RecordedRequest / Java's requestAt(i)).

func (RecordedRequest) H

func (r RecordedRequest) H(name string) string

H returns a request header by name (case-insensitive).

func (RecordedRequest) JSON

func (r RecordedRequest) JSON(v any) error

JSON unmarshals the recorded request body into v.

type Recorder

type Recorder struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Recorder wraps an httptest.Server, counts hits and records each request's headers in order — the Go analog of Node's LoopbackServer and Java's AtomicInteger evilHits. It binds 127.0.0.1:0 so cross-host redirect guarantees can be proven against real servers (a mocked sender would short-circuit the redirect and prove nothing).

func StartServer

func StartServer(h http.HandlerFunc) *Recorder

StartServer starts a recording loopback server delegating to h. The caller must arrange for Close (e.g. t.Cleanup(rec.Close)).

func (*Recorder) Headers

func (r *Recorder) Headers() []http.Header

Headers returns a snapshot of the recorded request headers, in order.

func (*Recorder) Hits

func (r *Recorder) Hits() int

Hits returns the number of requests the server received.

type Sleeper

type Sleeper struct {
	// contains filtered or unexported fields
}

Sleeper is a recording, instant sleeper: it never actually waits, but records every requested duration so backoff/poll tests can assert on them.

func (*Sleeper) Durations

func (s *Sleeper) Durations() []time.Duration

Durations returns a snapshot of the recorded sleep durations.

func (*Sleeper) Sleep

func (s *Sleeper) Sleep(ctx context.Context, d time.Duration) error

Sleep records d and returns immediately — but honors ctx cancellation first, so a canceled context surfaces during a backoff/poll wait exactly as the real timer-based sleeper would.

type TB added in v10.3.0

type TB interface {
	Helper()
	Fatalf(format string, args ...any)
}

TB is the subset of testing.TB the fake uses to fail fast on a missing fixture. Satisfied by *testing.T.

type TestClient

type TestClient struct {
	Client  *api2convert.Client
	HTTP    *FakeSender
	Sleeper *Sleeper
}

TestClient bundles a client wired to a FakeSender and a recording Sleeper, with jitter disabled (rng == 0) for deterministic backoff assertions.

func NewTestClient

func NewTestClient(t *testing.T, opts ...api2convert.Option) *TestClient

NewTestClient builds a TestClient with the API key "test-key". Additional options are appended after the injected seams, so a caller may override, for example, maxRetries. The FakeSender is wired to t so a missing fixture fails the test immediately. It panics only on an impossible construction error (the non-empty key guarantees success).

Jump to

Keyboard shortcuts

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