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 ¶
- func RedirectTo(location string) http.HandlerFunc
- func Respond(status int, body string) http.HandlerFunc
- func Sign(payload, secret string) string
- type FakeSender
- func (f *FakeSender) AddError(err error) *FakeSender
- func (f *FakeSender) AddJSON(status int, body any, header ...http.Header) *FakeSender
- func (f *FakeSender) AddRaw(status int, b []byte, header ...http.Header) *FakeSender
- func (f *FakeSender) AddText(status int, s string, header ...http.Header) *FakeSender
- func (f *FakeSender) At(i int) RecordedRequest
- func (f *FakeSender) Count() int
- func (f *FakeSender) Fail(t TB) *FakeSender
- func (f *FakeSender) Last() RecordedRequest
- func (f *FakeSender) Requests() []RecordedRequest
- func (f *FakeSender) Send(ctx context.Context, req *api2convert.Request) (*api2convert.Response, error)
- type RecordedRequest
- type Recorder
- type Sleeper
- type TB
- type TestClient
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.
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 ¶
func (f *FakeSender) Send(ctx context.Context, req *api2convert.Request) (*api2convert.Response, error)
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 ¶
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)).
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.
type TB ¶ added in v10.3.0
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).