Documentation
¶
Overview ¶
Package httptestutil contains utilities for use in HTTP tests, particular when using httptest.Server
Example ¶
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"github.com/theopenlane/httpsling"
"github.com/theopenlane/httpsling/httptestutil"
)
func main() {
mux := http.NewServeMux()
mux.Handle("/echo", httpsling.MockHandler(201, httpsling.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)
var out string
resp, _ := r.Receive(&out, httpsling.Get("/echo"), httpsling.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: " + out)
}
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 ...any))
- func DumpToStdout(ts *httptest.Server)
- func Requester(ts *httptest.Server, options ...httpsling.Option) *httpsling.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 DumpToStdout ¶
DumpToStdout writes requests and responses to os.Stdout
Types ¶
type Exchange ¶
type Exchange struct {
// Request is the request sent to the server
Request *http.Request
// RequestBody is the request body
RequestBody *bytes.Buffer
// StatusCode is the status code returned by the server
StatusCode int
// Header is the response header
Header http.Header
// ResponseBody is the response body
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
func Inspect ¶
Inspect installs and returns an Inspector; the Inspector captures exchanges with the test server
func NewInspector ¶
NewInspector creates a new Inspector with the requested channel buffer size
func (*Inspector) LastExchange ¶
LastExchange receives the most recent exchange from channel; this also has the side effect of draining the channel completely
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()
httpsling.Receive(httpsling.Get(ts.URL), httpsling.Body("ping1"))
httpsling.Receive(httpsling.Get(ts.URL), httpsling.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
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()
httpsling.Receive(httpsling.Get(ts.URL), httpsling.Body("ping1"))
httpsling.Receive(httpsling.Get(ts.URL), httpsling.Body("ping2"))
fmt.Println(i.NextExchange().RequestBody.String())
fmt.Println(i.NextExchange().RequestBody.String())
fmt.Println(i.NextExchange())
Output: ping1 ping2 <nil>