httprec

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Redacted = "[redacted]"

Redacted is what a credential header's value is replaced with.

Variables

View Source
var ErrUnexpected = errors.New("httprec: unexpected request")

ErrUnexpected marks a request a replay has no exchange for.

Functions

func Write

func Write(w io.Writer, note string, xs []Exchange) error

Write writes exchanges as a trace, after note as "# " comment lines.

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 Load

func Load(path string) ([]Exchange, error)

Load reads a trace file.

func Parse

func Parse(r io.Reader) ([]Exchange, error)

Parse reads a trace: one exchange per line; blank lines and lines starting with "#" are skipped.

func (Exchange) RequestBody

func (x Exchange) RequestBody() ([]byte, error)

RequestBody returns the request's body bytes.

func (Exchange) ResponseBody

func (x Exchange) ResponseBody() ([]byte, error)

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 Serve

func Serve(t TB, xs []Exchange) *Server

Serve starts a Server for xs, closed when the test ends.

func (*Server) AllowUnused

func (s *Server) AllowUnused()

AllowUnused lets the test end with exchanges the client never asked for.

func (*Server) Rewrite

func (s *Server) Rewrite(u string) string

Rewrite returns u with the recorded origins replaced by the server's URL: what a test configures in place of a recorded URL.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

func (*Server) Unused

func (s *Server) Unused() int

Unused returns how many exchanges the client has not asked for yet.

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.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

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