httptestutil

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2018 License: MIT Imports: 7 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 Requester

func Requester(ts *httptest.Server) *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) 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.MiddlewareFunc(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) MiddlewareFunc added in v0.2.1

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

MiddlewareFunc 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.MiddlewareFunc(mux))
defer ts.Close()

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.MiddlewareFunc(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>

Jump to

Keyboard shortcuts

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