proxy

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package proxy provides a record/replay HTTP proxy. It is designed to support both an in-memory API (github.com/google/go-replayers/httpreplay) and a standalone server (github.com/google/go-replayers/httpreplay/cmd/httpr).

Index

Constants

View Source
const LogVersion = "0.2"

LogVersion is the current version of the log format. It can be used to support changes to the format over time, so newer code can read older files.

Variables

View Source
var DebugHeaders = false

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.

Functions

This section is empty.

Types

type Converter

type Converter struct {
	ScrubBody []tRegexp // replace all matching parts of the body with "CLEARED"
	// These all apply to both headers and trailers.
	ClearHeaders          []tRegexp // replace matching headers with "CLEARED"
	RemoveRequestHeaders  []tRegexp // remove matching headers in requests
	RemoveResponseHeaders []tRegexp // remove matching headers in responses
	ClearParams           []tRegexp // replace matching query params with "CLEARED"
	RemoveParams          []tRegexp // remove matching query params
}

A Converter converts HTTP requests and responses to the Request and Response types of this package, while removing or redacting information.

type Entry

type Entry struct {
	ID       string // unique ID
	Request  *Request
	Response *Response
}

An Entry single request-response pair.

type Log

type Log struct {
	Initial   []byte // initial data for replay
	Version   string // version of this log format
	Converter *Converter
	Entries   []*Entry
}

A Log is a record of HTTP interactions, suitable for replay. It can be serialized to JSON.

type Logger

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

A Logger maintains a request-response log.

func (*Logger) Extract

func (l *Logger) Extract() *Log

Extract returns the Log and removes it. The Logger is not usable after this call.

func (*Logger) ModifyRequest

func (l *Logger) ModifyRequest(req *http.Request) error

ModifyRequest logs requests.

func (*Logger) ModifyResponse

func (l *Logger) ModifyResponse(res *http.Response) error

ModifyResponse logs responses.

type Proxy

type Proxy struct {
	// The certificate that the proxy uses to participate in TLS.
	CACert *x509.Certificate

	// The URL of the proxy.
	URL *url.URL

	// Initial state of the client.
	Initial []byte
	// contains filtered or unexported fields
}

A Proxy is an HTTP proxy that supports recording or replaying requests.

func ForRecording

func ForRecording(filename string, port int, cert, key string) (*Proxy, error)

ForRecording returns a Proxy configured to record.

func ForReplaying

func ForReplaying(filename string, port int, cert, key string) (*Proxy, error)

ForReplaying returns a Proxy configured to replay.

func (*Proxy) ClearHeaders

func (p *Proxy) ClearHeaders(patterns []string)

ClearHeaders will replace matching headers with CLEARED.

This only needs to be called during recording; the patterns will be saved to the log for replay.

func (*Proxy) ClearQueryParams

func (p *Proxy) ClearQueryParams(patterns []string)

ClearQueryParams will replace matching query params in the request URL with CLEARED.

This only needs to be called during recording; the patterns will be saved to the log for replay.

func (*Proxy) Close

func (p *Proxy) Close() error

Close closes the proxy. If the proxy is recording, it also writes the log.

func (*Proxy) IgnoreHeader

func (p *Proxy) IgnoreHeader(h string)

IgnoreHeader will cause h to be ignored during matching on replay. Deprecated: use RemoveRequestHeaders instead.

func (*Proxy) RemoveQueryParams

func (p *Proxy) RemoveQueryParams(patterns []string)

RemoveQueryParams will remove query parameters matching patterns from the request URL before logging, and skip matching them. Pattern is taken literally except for *, which matches any sequence of characters.

This only needs to be called during recording; the patterns will be saved to the log for replay.

func (*Proxy) RemoveRequestHeaders

func (p *Proxy) RemoveRequestHeaders(patterns []string)

RemoveRequestHeaders will remove request headers matching patterns from the log, and skip matching them. Pattern is taken literally except for *, which matches any sequence of characters.

This only needs to be called during recording; the patterns will be saved to the log for replay.

func (*Proxy) RemoveResponseHeaders added in v1.2.0

func (p *Proxy) RemoveResponseHeaders(patterns []string)

RemoveResponseHeaders will remove response headers matching patterns from the log, and skip matching them. Pattern is taken literally except for *, which matches any sequence of characters.

This only needs to be called during recording; the patterns will be saved to the log for replay.

func (*Proxy) ScrubBody added in v0.1.2

func (p *Proxy) ScrubBody(regexps []string)

ScrubBody will replace all matching parts of the body with CLEARED. regexps are parsed as regexp.Regexp.

This only needs to be called during recording; the patterns will be saved to the log for replay.

func (*Proxy) Transport

func (p *Proxy) Transport() *http.Transport

Transport returns an http.Transport for clients who want to talk to the proxy.

type Request

type Request struct {
	Method string      // http.Request.Method
	URL    string      // http.Request.URL, as a string
	Header http.Header // http.Request.Header
	// We need to understand multipart bodies because the boundaries are
	// generated randomly, so we can't just compare the entire bodies for equality.
	MediaType string      // the media type part of the Content-Type header
	BodyParts [][]byte    // http.Request.Body, read to completion and split for multipart
	Trailer   http.Header `json:",omitempty"` // http.Request.Trailer
}

A Request represents an http.Request in the log.

type Response

type Response struct {
	StatusCode int         // http.Response.StatusCode
	Proto      string      // http.Response.Proto
	ProtoMajor int         // http.Response.ProtoMajor
	ProtoMinor int         // http.Response.ProtoMinor
	Header     http.Header // http.Response.Header
	Body       []byte      // http.Response.Body, read to completion
	Trailer    http.Header `json:",omitempty"` // http.Response.Trailer
}

A Response represents an http.Response in the log.

Jump to

Keyboard shortcuts

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