proxytest

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithMITMCA

func WithMITMCA(priv crypto.PrivateKey, cert *x509.Certificate) func(o *options)

WithMITMCA sets the CA used for MITM (men in the middle) when proxying HTTPS requests. It's used to generate TLS certificates matching the target host. Ideally the CA is the same as the one issuing the TLS certificate for the proxy set by WithServerTLSConfig.

Types

type Option

type Option func(o *options)

func WithAddress

func WithAddress(addr string) Option

WithAddress will set the address the server will listen on. The format is as defined by net.Listen for a tcp connection.

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient sets http.Client used to proxy requests to the target host.

func WithRequestLog

func WithRequestLog(name string, logFn func(format string, a ...any)) Option

WithRequestLog sets the proxy to log every request using logFn. It uses name as a prefix to the log.

func WithRewrite

func WithRewrite(old, new string) Option

WithRewrite will replace old by new on the request URL host when forwarding it.

func WithRewriteFn

func WithRewriteFn(f func(u *url.URL)) Option

WithRewriteFn calls f on the request *url.URL before forwarding it. It takes precedence over WithRewrite. Use if more control over the rewrite is needed.

func WithServerTLSConfig

func WithServerTLSConfig(tc *tls.Config) Option

WithServerTLSConfig sets the TLS config for the server.

func WithVerboseLog

func WithVerboseLog() Option

WithVerboseLog sets the proxy to log every request verbosely and enables debug level logging. WithRequestLog must be used as well, otherwise WithVerboseLog will not take effect.

type Proxy

type Proxy struct {
	*httptest.Server

	// Port is the port Server is listening on.
	Port string

	// LocalhostURL is the server URL as "http(s)://localhost:PORT".
	// Deprecated. Use Proxy.URL instead.
	LocalhostURL string
	// contains filtered or unexported fields
}

func New

func New(t *testing.T, optns ...Option) *Proxy

New returns a new Proxy ready for use. Use:

  • WithAddress to set the proxy's address,
  • WithRewrite or WithRewriteFn to rewrite the URL before forwarding the request.

Check the other With* functions for more options.

func (*Proxy) Close

func (p *Proxy) Close()

func (*Proxy) ProxiedRequests

func (p *Proxy) ProxiedRequests() []string

ProxiedRequests returns a slice with the "request log" with every request the proxy received.

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Proxy) Start

func (p *Proxy) Start() error

func (*Proxy) StartTLS

func (p *Proxy) StartTLS() error

Jump to

Keyboard shortcuts

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