httptesting

package
v1.64.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AlwaysRecord = false
View Source
var RecordIfFileNotFound = false

Functions

func BuildResponse

func BuildResponse(code int, payload []byte) *http.Response

func BuildResponseFromRecord added in v1.64.0

func BuildResponseFromRecord(respRec *ResponseRecord) *http.Response

BuildResponseFromRecord creates an http.Response from a ResponseRecord.

func BuildResponseJson

func BuildResponseJson(code int, payload interface{}) *http.Response

func BuildResponseString

func BuildResponseString(code int, payload string) *http.Response

func DeleteHeader

func DeleteHeader(resp *http.Response, name string) *http.Response

func HttpClientFromFile

func HttpClientFromFile(filename string) *http.Client

func HttpClientSaver

func HttpClientSaver(saved **http.Request, content string) *http.Client

"Saver" refers to saving the *http.Request in a local variable provided by the caller.

func HttpClientSaverWithJson

func HttpClientSaverWithJson(saved **http.Request, jsonData interface{}) *http.Client

func HttpClientWithContent

func HttpClientWithContent(content string) *http.Client

func HttpClientWithError

func HttpClientWithError(err error) *http.Client

func HttpClientWithJson

func HttpClientWithJson(jsonData interface{}) *http.Client

func MockWithJsonReply

func MockWithJsonReply(url string, rawData interface{}) *http.Client

func RunHttpTestWithRecorder added in v1.64.0

func RunHttpTestWithRecorder(t *testing.T, client *http.Client, recordFile string) (bool, func())

RunHttpTestWithRecorder sets up HTTP recording or playback for integration tests. It configures the provided http.Client to either record live HTTP requests to a file, or replay them from a file for deterministic testing. The function returns a boolean indicating whether recording is enabled, and a cleanup function to save recordings.

Usage example:

// Enable recording for this test case
httptesting.AlwaysRecord = true
isRecording, saveRecord := httptesting.RunHttpTestWithRecorder(t, client.HttpClient, "testdata/record.json")
defer saveRecord()

// ... run your test logic ...

Environment variable settings:

TEST_HTTP_RECORD=1   # enable recording mode
TEST_HTTP_LIVE=1     # enable live mode (no recording)

The variable httptesting.AlwaysRecord can also be set directly in your test case to force recording mode. If recording is enabled, HTTP requests are captured and saved to the specified file. If not, requests are replayed from the file for fast, repeatable tests.

func SetHeader

func SetHeader(resp *http.Response, name string, value string) *http.Response

Types

type EchoSave

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

func (*EchoSave) RoundTrip

func (st *EchoSave) RoundTrip(req *http.Request) (*http.Response, error)

type MockTransport

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

func (*MockTransport) DELETE

func (transport *MockTransport) DELETE(path string, f RoundTripFunc)

func (*MockTransport) GET

func (transport *MockTransport) GET(path string, f RoundTripFunc)

func (*MockTransport) LoadFromRecorder added in v1.64.0

func (transport *MockTransport) LoadFromRecorder(recorder *Recorder) error

LoadFromRecorder loads request/response records from file and registers handlers to MockTransport. Each handler will respond with the recorded response for the matching method and path.

func (*MockTransport) POST

func (transport *MockTransport) POST(path string, f RoundTripFunc)

func (*MockTransport) PUT

func (transport *MockTransport) PUT(path string, f RoundTripFunc)

func (*MockTransport) PostOrDelete

func (transport *MockTransport) PostOrDelete(isDelete bool, path string, f RoundTripFunc)

Used for migration to MAX v3 api, where order cancel uses DELETE (MAX v2 api uses POST).

func (*MockTransport) RoundTrip

func (transport *MockTransport) RoundTrip(req *http.Request) (*http.Response, error)

type Recorder added in v1.64.0

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

Recorder is used to record request and response pairs to files. It can also playback the recorded pairs by registering handlers to a MockTransport.

func NewRecorder added in v1.64.0

func NewRecorder(transport http.RoundTripper) *Recorder

NewRecorder creates a new Recorder instance with the given transport. transport is the underlying http.RoundTripper used for actual HTTP requests.

func (*Recorder) Load added in v1.64.0

func (r *Recorder) Load(filename string) error

Load loads recorded entries from a JSON file.

func (*Recorder) RecordEntry added in v1.64.0

func (r *Recorder) RecordEntry(req *http.Request, resp *http.Response, err error)

RecordEntry records a request and response, and automatically filters sensitive credentials from the request header.

func (*Recorder) RoundTrip added in v1.64.0

func (r *Recorder) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface for Recorder. It records the request and response, and returns the response from the underlying transport.

func (*Recorder) RoundTripFunc added in v1.64.0

func (r *Recorder) RoundTripFunc(f RoundTripFunc) RoundTripFunc

RoundTripFunc returns a RoundTripFunc that records request and response using the Recorder. f is the original RoundTripFunc to be called.

func (*Recorder) Save added in v1.64.0

func (r *Recorder) Save(filename string) error

Save saves the recorded entries to a JSON file.

type RecorderEntry added in v1.64.0

type RecorderEntry struct {
	Timestamp time.Time       `json:"timestamp"`
	Request   *RequestRecord  `json:"request"`
	Response  *ResponseRecord `json:"response"`
	Error     string          `json:"error,omitempty"`
}

RecorderEntry records a single request and response pair. It includes basic request and response information, and is used for (de)serialization to and from files.

type RequestRecord added in v1.64.0

type RequestRecord struct {
	Method string      `json:"method"`
	URL    string      `json:"url"`
	Header http.Header `json:"header"`
	Body   string      `json:"body,omitempty"`
}

type ResponseRecord added in v1.64.0

type ResponseRecord struct {
	Status     string      `json:"status"`
	StatusCode int         `json:"status_code"`
	Header     http.Header `json:"header"`
	Body       string      `json:"body,omitempty"`
}

type RoundTripFunc

type RoundTripFunc func(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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