Documentation
¶
Index ¶
- Variables
- func BuildResponse(code int, payload []byte) *http.Response
- func BuildResponseFromRecord(respRec *ResponseRecord) *http.Response
- func BuildResponseJson(code int, payload interface{}) *http.Response
- func BuildResponseString(code int, payload string) *http.Response
- func DeleteHeader(resp *http.Response, name string) *http.Response
- func HttpClientFromFile(filename string) *http.Client
- func HttpClientSaver(saved **http.Request, content string) *http.Client
- func HttpClientSaverWithJson(saved **http.Request, jsonData interface{}) *http.Client
- func HttpClientWithContent(content string) *http.Client
- func HttpClientWithError(err error) *http.Client
- func HttpClientWithJson(jsonData interface{}) *http.Client
- func MockWithJsonReply(url string, rawData interface{}) *http.Client
- func RunHttpTestWithRecorder(t *testing.T, client *http.Client, recordFile string) (bool, func())
- func SetHeader(resp *http.Response, name string, value string) *http.Response
- type EchoSave
- type MockTransport
- func (transport *MockTransport) DELETE(path string, f RoundTripFunc)
- func (transport *MockTransport) GET(path string, f RoundTripFunc)
- func (transport *MockTransport) LoadFromRecorder(recorder *Recorder) error
- func (transport *MockTransport) POST(path string, f RoundTripFunc)
- func (transport *MockTransport) PUT(path string, f RoundTripFunc)
- func (transport *MockTransport) PostOrDelete(isDelete bool, path string, f RoundTripFunc)
- func (transport *MockTransport) RoundTrip(req *http.Request) (*http.Response, error)
- type Recorder
- func (r *Recorder) Load(filename string) error
- func (r *Recorder) RecordEntry(req *http.Request, resp *http.Response, err error)
- func (r *Recorder) RoundTrip(req *http.Request) (*http.Response, error)
- func (r *Recorder) RoundTripFunc(f RoundTripFunc) RoundTripFunc
- func (r *Recorder) Save(filename string) error
- type RecorderEntry
- type RequestRecord
- type ResponseRecord
- type RoundTripFunc
Constants ¶
This section is empty.
Variables ¶
var AlwaysRecord = false
var RecordIfFileNotFound = false
Functions ¶
func BuildResponseFromRecord ¶ added in v1.64.0
func BuildResponseFromRecord(respRec *ResponseRecord) *http.Response
BuildResponseFromRecord creates an http.Response from a ResponseRecord.
func BuildResponseJson ¶
func HttpClientFromFile ¶
func HttpClientSaver ¶
"Saver" refers to saving the *http.Request in a local variable provided by the caller.
func HttpClientSaverWithJson ¶
func HttpClientWithContent ¶
func HttpClientWithError ¶
func HttpClientWithJson ¶
func MockWithJsonReply ¶
func RunHttpTestWithRecorder ¶ added in v1.64.0
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.
Types ¶
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).
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) RecordEntry ¶ added in v1.64.0
RecordEntry records a request and response, and automatically filters sensitive credentials from the request header.
func (*Recorder) RoundTrip ¶ added in v1.64.0
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.
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.