b2wiretest

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package b2wiretest provides a recording HTTP server for B2 wire tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertHeader

func AssertHeader(t testing.TB, r Request, name, want string)

AssertHeader checks one request header's value.

func AssertJSON

func AssertJSON(t testing.TB, r Request, path string, want any)

AssertJSON decodes r.Body and checks a non-empty, dot-separated path in its JSON object. A hand-built Request must set Body. Numbers compare by value with large integers kept exact. Dots always separate path segments; keys containing literal dots cannot be addressed.

func AssertNoJSON

func AssertNoJSON(t testing.TB, r Request, path string)

AssertNoJSON checks that a non-empty, dot-separated path is absent from r.Body. An empty Body means the field is absent; a non-empty Body must be a single JSON object. It has the same path limitations as AssertJSON.

func AssertQuery

func AssertQuery(t testing.TB, r Request, name, want string)

AssertQuery checks one request query parameter's value.

Types

type Request

type Request struct {
	Seq    int
	Method string
	Path   string
	// Call is the last segment of Path, including for non-API paths; a trailing slash gives "".
	Call   string
	Query  url.Values
	Header http.Header
	Body   []byte
	JSON   map[string]any
}

Request is a snapshot of one HTTP request received by a Server.

type Response

type Response struct {
	Status int
	Body   string
	Header http.Header
}

Response is the status, headers, and body returned for a scripted call. Status 0 means 200. Content-Type defaults to application/json unless Header sets it; X-Content-Type-Options is always nosniff.

func RangeDownload

func RangeDownload(body, byteRange string) Response

RangeDownload returns a B2-style download response for a fixture's body. A range covering the whole body is returned as HTTP 200; partial ranges are 206.

type Server

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

Server records B2 requests and answers with built-in or scripted responses.

func New

func New(t testing.TB) *Server

New starts a recording server and closes it when t finishes. Cleanup rejects new requests and waits for active HTTP handlers before returning.

func (*Server) Calls

func (s *Server) Calls(call string) []Request

Calls returns requests for call in arrival order.

func (*Server) Handle

func (s *Server) Handle(call string, h func(Request) Response)

Handle registers a response computed from each request. It replaces any earlier script. See Response for status and header defaults.

func (*Server) Last

func (s *Server) Last(t testing.TB, call string) Request

Last returns the last request for call, or fails the test if none exists.

func (*Server) Reply

func (s *Server) Reply(call string, status int, body string)

Reply registers a response repeated for every request to call. Status 0 means 200.

func (*Server) ReplySequence

func (s *Server) ReplySequence(call string, rs ...Response)

ReplySequence registers successive responses; the last response repeats. See Response for status and header defaults.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns all requests in arrival order.

func (*Server) StrictReplySequence

func (s *Server) StrictReplySequence(call string, rs ...Response)

StrictReplySequence registers responses that must be used exactly once each. It reports an error for an extra request or an unused response at test cleanup. Replacing it reports any unused responses from the earlier sequence.

func (*Server) URL

func (s *Server) URL() string

URL returns the server's base URL.

Jump to

Keyboard shortcuts

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