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 ¶
- func Dump(ts *httptest.Server, to io.Writer)
- func DumpTo(handler http.Handler, writer io.Writer) http.Handler
- func DumpToLog(ts *httptest.Server, logf func(a ...interface{}))
- func DumpToStdout(ts *httptest.Server)
- func Requester(ts *httptest.Server, options ...requester.Option) *requester.Requester
- type Exchange
- type Inspector
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DumpTo ¶
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 ¶
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 ¶
DumpToStdout writes requests and responses to os.Stdout.
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 ¶
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 ¶
NewInspector creates a new Inspector with the requested channel buffer size. If 0, the buffer size defaults to 50.
func (*Inspector) LastExchange ¶
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 ¶
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>