Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Client ¶
Client is a helper function that returns an http.Client. The client is configured with the Transport from this package.
Example ¶
mux := http.NewServeMux()
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "index")
})
client := Client(nil, mux)
resp := must.OK1(client.Get("/"))
defer resp.Body.Close()
fmt.Println("GET / response status:", resp.Status)
fmt.Println("GET / response body:", string(must.OK1(io.ReadAll(resp.Body))))
Output: GET / response status: 200 OK GET / response body: index
Types ¶
type Transport ¶
type Transport struct {
// Context, when specified, limits the lifetime of handler
// invocation. It ensures that any request passed to the handler
// gets cancelled if this context is cancelled.
//
// Deadlines and values from this context are disregarded.
//
// This is particularly useful for timely and clean shutdown of tests.
Context context.Context //nolint:containedctx
// Handler is the handler to be invoked
Handler http.Handler
}
Transport is an http.RoundTripper that directly calls the HTTP handler
This transport is useful for creating http.Client instances for testing: either short-cutting clients to existing HTTP server handlers, or mocking external HTTP endpoints.
Example ¶
mux := http.NewServeMux()
mux.HandleFunc("POST /size", func(w http.ResponseWriter, r *http.Request) {
// Check passed header
if r.Header.Get("Content-Type") != "text/plain" {
http.Error(w, "", http.StatusUnsupportedMediaType)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "x-text/digits")
fmt.Fprintf(w, "%d", len(body))
})
// Create a HTTP client that uses mux
client := &http.Client{
Transport: Transport{
Handler: mux,
},
}
respPost := must.OK1(client.Post("/size", "text/plain", strings.NewReader("1234")))
defer respPost.Body.Close()
fmt.Println("POST /size response status:", respPost.Status)
fmt.Println("POST /size response Content-Type:", respPost.Header.Get("Content-Type"))
fmt.Println("POST /size response body:", string(must.OK1(io.ReadAll(respPost.Body))))
Output: POST /size response status: 200 OK POST /size response Content-Type: x-text/digits POST /size response body: 4
Click to show internal directories.
Click to hide internal directories.