httptestutil

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package httptestutil contains utilities for use in HTTP tests, particular when using httptest.Server.

Inspect() can be used to intercept and inspect the traffic to and from an httptest.Server.

Example
package main

import (
	"fmt"
	"github.com/gemalto/requester"
	"github.com/gemalto/requester/httptestutil"
	"net/http"
	"net/http/httptest"
	"strconv"
)

func main() {

	mux := http.NewServeMux()
	mux.Handle("/echo", requester.MockHandler(201, requester.Body("pong")))

	ts := httptest.NewServer(mux)
	defer ts.Close()

	// inspect server traffic
	is := httptestutil.Inspect(ts)

	// construct a pre-configured Requester
	r := httptestutil.Requester(ts)
	resp, body, _ := r.Receive(requester.Get("/echo"), requester.Body("ping"))

	ex := is.LastExchange()
	fmt.Println("server received: " + ex.RequestBody.String())
	fmt.Println("server sent: " + strconv.Itoa(ex.StatusCode))
	fmt.Println("server sent: " + ex.ResponseBody.String())
	fmt.Println("client received: " + strconv.Itoa(resp.StatusCode))
	fmt.Println("client received: " + string(body))

}
Output:

server received: ping
server sent: 201
server sent: pong
client received: 201
client received: pong

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(ts *httptest.Server, to io.Writer)

Dump writes requests and responses to the writer.

func DumpTo

func DumpTo(handler http.Handler, writer io.Writer) http.Handler

DumpTo wraps an http.Handler in a new handler. The new handler dumps requests and responses to a writer, using the httputil.DumpRequest and httputil.DumpResponse functions.

func DumpToLog

func DumpToLog(ts *httptest.Server, logf func(a ...interface{}))

DumpToLog writes requests and responses to a logging function. The function signature is the same as testing.T.Log, so it can be used to pipe traffic to the test log:

func TestHandler(t *testing.T) {
    ...
    DumpToLog(ts, t.Log)

func DumpToStdout

func DumpToStdout(ts *httptest.Server)

DumpToStdout writes requests and responses to os.Stdout.

func Requester

func Requester(ts *httptest.Server, options ...requester.Option) *requester.Requester

Requester creates a Requester instance which is pre-configured to send requests to the test server. The Requester is configured with the server's base URL, and the server's TLS certs (if using a TLS server).

Types

type Exchange

type Exchange struct {
	Request     *http.Request
	RequestBody *bytes.Buffer

	StatusCode   int
	Header       http.Header
	ResponseBody *bytes.Buffer
}

Exchange is a snapshot of one request/response exchange with the server.

type Inspector

type Inspector struct {
	Exchanges chan Exchange
}

Inspector is server-side middleware which captures server exchanges in a buffer. Exchanges are captured in a buffered channel. If the channel buffer fills, subsequent server exchanges are not captured.

Exchanges can be received directly from the channel, or you can use the NextExchange() and LastExchange() convenience methods.

func Inspect

func Inspect(ts *httptest.Server) *Inspector

Inspect installs and returns an Inspector. The Inspector captures exchanges with the test server. It's useful in tests to inspect the incoming requests and request bodies and the outgoing responses and response bodies.

Inspect wraps and replaces the server's Handler. It should be called after the real Handler has been installed.

func NewInspector

func NewInspector(size int) *Inspector

NewInspector creates a new Inspector with the requested channel buffer size. If 0, the buffer size defaults to 50.

func (*Inspector) Clear

func (b *Inspector) Clear()

Clear drains the channel.

func (*Inspector) Drain added in v1.2.1

func (b *Inspector) Drain() []*Exchange

Drain reads all buffered exchanges from the channel.

func (*Inspector) LastExchange

func (b *Inspector) LastExchange() *Exchange

LastExchange receives the most recent exchange from channel. This also has the side effect of draining the channel completely. If no exchange is ready, nil is returned. It is non-blocking.

Example
i := NewInspector(0)

var h http.Handler = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
	writer.Write([]byte(`pong`))
})

h = i.Wrap(h)

ts := httptest.NewServer(h)
defer ts.Close()

requester.Receive(requester.Get(ts.URL), requester.Body("ping1"))
requester.Receive(requester.Get(ts.URL), requester.Body("ping2"))

fmt.Println(i.LastExchange().RequestBody.String())
fmt.Println(i.LastExchange())
Output:

ping2
<nil>

func (*Inspector) NextExchange

func (b *Inspector) NextExchange() *Exchange

NextExchange receives the next exchange from the channel, or returns nil if no exchange is ready. It is non-blocking.

Example
i := NewInspector(0)

var h http.Handler = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
	writer.Write([]byte(`pong`))
})

h = i.Wrap(h)

ts := httptest.NewServer(h)
defer ts.Close()

requester.Receive(requester.Get(ts.URL), requester.Body("ping1"))
requester.Receive(requester.Get(ts.URL), requester.Body("ping2"))

fmt.Println(i.NextExchange().RequestBody.String())
fmt.Println(i.NextExchange().RequestBody.String())
fmt.Println(i.NextExchange())
Output:

ping1
ping2
<nil>

func (*Inspector) Wrap

func (b *Inspector) Wrap(next http.Handler) http.Handler

Wrap installs the inspector in an HTTP server by wrapping the server's Handler.

Example
mux := http.NewServeMux()
// configure mux...

i := NewInspector(0)

ts := httptest.NewServer(i.Wrap(mux))
defer ts.Close()
Output:

Jump to

Keyboard shortcuts

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