package httpreplay
import "github.com/google/go-replayers/httpreplay"
Package httpreplay provides an API for recording and replaying traffic from HTTP-based clients.
To record:
- Call NewRecorder to get a Recorder.
- Use its Client method to obtain an HTTP client to use when making API calls.
- Close the Recorder when you're done. That will save the log of interactions to the file you provided to NewRecorder.
To replay:
- Call NewReplayer with the same filename you used to record to get a Replayer.
- Call its Client method and use the client to make the same API calls. You will get back the recorded responses.
- Close the Replayer when you're done.
Although it has some features specific to Google HTTP APIs, this package can be used more generally.
Index ¶
- func DebugHeaders()
- func RecorderCert(fileName string) recorderOption
- func RecorderInitial(initial []byte) recorderOption
- func RecorderKey(fileName string) recorderOption
- func RecorderPort(port int) recorderOption
- func ReplayerCert(fileName string) replayerOption
- func ReplayerKey(fileName string) replayerOption
- func ReplayerPort(port int) replayerOption
- type Recorder
- func NewRecorder(filename string, initial []byte) (*Recorder, error)
- func NewRecorderWithOpts(filename string, opts ...recorderOption) (*Recorder, error)
- func (r *Recorder) ClearHeaders(patterns ...string)
- func (r *Recorder) ClearQueryParams(patterns ...string)
- func (r *Recorder) Client() *http.Client
- func (r *Recorder) Close() error
- func (r *Recorder) ProxyURL() *url.URL
- func (r *Recorder) RemoveQueryParams(patterns ...string)
- func (r *Recorder) RemoveRequestHeaders(patterns ...string)
- func (r *Recorder) RemoveResponseHeaders(patterns ...string)
- func (r *Recorder) ScrubBody(regexps ...string)
- type Replayer
- func NewReplayer(filename string) (*Replayer, error)
- func NewReplayerWithOpts(filename string, opts ...replayerOption) (*Replayer, error)
- func (r *Replayer) Client() *http.Client
- func (r *Replayer) Close() error
- func (r *Replayer) IgnoreHeader(h string)
- func (r *Replayer) Initial() []byte
- func (r *Replayer) ProxyURL() *url.URL
Functions ¶
func DebugHeaders ¶
func DebugHeaders()
DebugHeaders helps to determine whether a header should be ignored. When true, if requests have the same method, URL and body but differ in a header, the first mismatched header is logged.
func RecorderCert ¶
func RecorderCert(fileName string) recorderOption
The custom CA cert filename to use for the MITM proxy to create certs on the fly
func RecorderInitial ¶
func RecorderInitial(initial []byte) recorderOption
The recorder file will store initial data that can be retrieved during replay
func RecorderKey ¶
func RecorderKey(fileName string) recorderOption
The key filename to the custom CA cert to use for the MITM proxy to create certs on the fly
func RecorderPort ¶
func RecorderPort(port int) recorderOption
The port number for the MITM proxy
func ReplayerCert ¶
func ReplayerCert(fileName string) replayerOption
The custom CA cert filename to use for the MITM proxy to create certs on the fly
func ReplayerKey ¶
func ReplayerKey(fileName string) replayerOption
The key filename to the custom CA cert to use for the MITM proxy to create certs on the fly
func ReplayerPort ¶
func ReplayerPort(port int) replayerOption
The port number for the MITM proxy
Types ¶
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
A Recorder records HTTP interactions.
func NewRecorder ¶
NewRecorder creates a recorder that writes to filename. The file will also store initial state that can be retrieved to configure replay.
You must call Close on the Recorder to ensure that all data is written.
func NewRecorderWithOpts ¶
NewRecorderWithOpts creates a recorder that writes to filename. The default Recorder MITM proxy can be customised with one or more recorderOption.
You must call Close on the Recorder to ensure that all data is written.
func (*Recorder) ClearHeaders ¶
ClearHeaders will replace the value of request and response headers that match any of the patterns with CLEARED, on both recording and replay. Use ClearHeaders when the header information is secret or may change from run to run, but you still want to verify that the headers are being sent and received.
Pattern is taken literally except for *, which matches any sequence of characters.
func (*Recorder) ClearQueryParams ¶
ClearQueryParams will replace the value of URL query parametrs that match any of the patterns with CLEARED, on both recording and replay. Use ClearQueryParams when the parameter information is secret or may change from run to run, but you still want to verify that it are being sent.
Pattern is taken literally except for *, which matches any sequence of characters.
func (*Recorder) Client ¶
Client returns an http.Client to be used for recording.
func (*Recorder) Close ¶
Close closes the Recorder and saves the log file.
Since Close writes a file, you should always check that it returns a non-nil error.
func (*Recorder) ProxyURL ¶
ProxyURL will return the MITM proxy address
func (*Recorder) RemoveQueryParams ¶
RemoveQueryParams will remove URL query parameters matching patterns from the log, and skip matching them during replay.
Pattern is taken literally except for *, which matches any sequence of characters.
func (*Recorder) RemoveRequestHeaders ¶
RemoveRequestHeaders will remove request headers matching patterns from the log, and skip matching them during replay.
Pattern is taken literally except for *, which matches any sequence of characters.
func (*Recorder) RemoveResponseHeaders ¶
RemoveResponseHeaders will remove response headers matching patterns from the log, and skip matching them during replay.
Pattern is taken literally except for *, which matches any sequence of characters.
func (*Recorder) ScrubBody ¶
ScrubBody will replace all parts of the request body that match any of the provided regular expressions with CLEARED, on both recording and replay. Use ScrubBody when the body information is secret or may change from run to run.
You may also need to RemoveRequestHeaders for the "Content-Length" header if the body length changes from run to run.
Regexps are parsed as regexp.Regexp.
type Replayer ¶
type Replayer struct {
// contains filtered or unexported fields
}
A Replayer replays previously recorded HTTP interactions.
func NewReplayer ¶
NewReplayer creates a replayer that reads from filename.
func NewReplayerWithOpts ¶
NewReplayerWithOpts creates a replayer that reads from filename. The default Replayer MITM proxy can be customised with one or more replayerOption.
func (*Replayer) Client ¶
Client returns an HTTP client for replaying.
The client does not need to be configured with credentials for authenticating to a server, since it never contacts a real backend.
func (*Replayer) Close ¶
Close closes the replayer.
func (*Replayer) IgnoreHeader ¶
IgnoreHeader will not use h when matching requests.
func (*Replayer) Initial ¶
Initial returns the initial state saved by the Recorder.
func (*Replayer) ProxyURL ¶
ProxyURL will return the MITM proxy address
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd | |
cmd/httpr | |
Package google provides a way to obtain an http.Client for Google Cloud Platform APIs. | |
internal |
- Version
- v1.2.0 (latest)
- Published
- Mar 7, 2023
- Platform
- linux/amd64
- Imports
- 3 packages
- Last checked
- 3 months ago –
Tools for package owners.