Documentation
¶
Overview ¶
Package httprec records HTTP sessions and replays them in tests (docs/design/testing.md, DAV and Tasks recordings), as imapx's trace and replay do for IMAP. Transport appends every exchange a client makes to a trace, with credentials redacted; Serve answers a client from a trace. A trace holds the account's contacts, calendars and tasks: it never leaves the machine, and only copies scrubbed by tools/davrec are checked in.
Index ¶
Constants ¶
const Redacted = "[redacted]"
Redacted is what a credential header's value is replaced with.
Variables ¶
var ErrUnexpected = errors.New("httprec: unexpected request")
ErrUnexpected marks a request a replay has no exchange for.
Functions ¶
Types ¶
type Exchange ¶
type Exchange struct {
Method string `json:"method"`
URL string `json:"url"`
ReqHeader http.Header `json:"reqHeader,omitzero"`
// ReqBody is the request's body: text as is, anything else base64
// with ReqBase64 set.
ReqBody string `json:"reqBody,omitzero"`
ReqBase64 bool `json:"reqBase64,omitzero"`
// Status is 0 when the request failed without an answer (Err).
Status int `json:"status,omitzero"`
RespHeader http.Header `json:"respHeader,omitzero"`
RespBody string `json:"respBody,omitzero"`
RespBase64 bool `json:"respBase64,omitzero"`
Err string `json:"err,omitzero"`
}
Exchange is one request and the answer it got.
func Parse ¶
Parse reads a trace: one exchange per line; blank lines and lines starting with "#" are skipped.
func (Exchange) RequestBody ¶
RequestBody returns the request's body bytes.
func (Exchange) ResponseBody ¶
ResponseBody returns the response's body bytes.
type Server ¶
type Server struct {
URL string
// contains filtered or unexported fields
}
Server answers a client from a trace on a loopback port. Each request is answered with the first unused exchange with the same method and path (with its query), whatever the host: one server stands in for every host the session reached. PROPFIND and REPORT requests must also carry the recorded body, the queries a sync depends on; other bodies (writes, which hold timestamps) are not compared. The recorded origins (scheme://host) in answers' headers and text bodies become the server's URL. A request the trace has no exchange for fails the test, and so, when the test ends, does an exchange the client never asked for, unless AllowUnused was called. An exchange recorded with an error closes the connection.
func (*Server) AllowUnused ¶
func (s *Server) AllowUnused()
AllowUnused lets the test end with exchanges the client never asked for.
func (*Server) Rewrite ¶
Rewrite returns u with the recorded origins replaced by the server's URL: what a test configures in place of a recorded URL.
type TB ¶
type TB interface {
Helper()
Errorf(format string, args ...any)
Logf(format string, args ...any)
Cleanup(func())
}
TB is the part of testing.TB a Server reports to.
type Transport ¶
type Transport struct {
Base http.RoundTripper
W io.Writer
OnError func(error)
// contains filtered or unexported fields
}
Transport is an http.RoundTripper that sends each request through Base (nil: http.DefaultTransport) and appends the exchange to W as one JSON line. Bodies are read whole. Writes to W are serialized; a failure to write is reported to OnError, if set, and does not fail the request.