Documentation ¶
Overview ¶
Package httphelpers contains convenience tools for testing HTTP client code.
Index ¶
- func ClientFromHandler(handler http.Handler) *http.Client
- func HandlerForMethod(method string, handlerForMethod http.Handler, defaultHandler http.Handler) http.Handler
- func HandlerForPath(path string, handlerForPath http.Handler, defaultHandler http.Handler) http.Handler
- func HandlerForPathRegex(pathRegex string, handlerForPath http.Handler, defaultHandler http.Handler) http.Handler
- func HandlerWithJSONResponse(contentToEncode interface{}, additionalHeaders http.Header) http.Handler
- func HandlerWithResponse(status int, headers http.Header, body []byte) http.Handler
- func HandlerWithStatus(status int) http.Handler
- func MakeSelfSignedCert(certFilePath, keyFilePath string) error
- func MakeServerWithCert(certFilePath, keyFilePath string, handler http.Handler) (*httptest.Server, error)
- func PanicHandler(err error) http.Handler
- func RecordingHandler(delegateToHandler http.Handler) (http.Handler, <-chan HTTPRequestInfo)
- func SequentialHandler(firstHandler http.Handler, remainingHandlers ...http.Handler) http.Handler
- func WithSelfSignedServer(handler http.Handler, action func(*httptest.Server, []byte, *x509.CertPool))
- func WithServer(handler http.Handler, action func(*httptest.Server))
- type DelegatingHandler
- type HTTPRequestInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientFromHandler ¶
ClientFromHandler returns an http.Client that does not do real network activity, but instead delegates to a http.Handler as if that handler were being used by a server.
This makes it possible to reuse the other handler-related functions in this package to control an http.Client rather than using the somewhat less convenient RoundTripper interface.
If the handler panics, it returns an error instead of a response. This can be used to simulate an I/O error (since the http.Handler interface does not provide any way *not* to return an actual HTTP response).
func HandlerForMethod ¶
func HandlerForMethod(method string, handlerForMethod http.Handler, defaultHandler http.Handler) http.Handler
HandlerForMethod is a simple alternative to using an HTTP router. It delegates to the specified handler if the method matches; otherwise to the default handler, or a 405 if that is nil.
func HandlerForPath ¶
func HandlerForPath(path string, handlerForPath http.Handler, defaultHandler http.Handler) http.Handler
HandlerForPath is a simple alternative to using an HTTP router. It delegates to the specified handler if the path matches; otherwise to the default handler, or a 404 if that is nil.
func HandlerForPathRegex ¶ added in v1.1.0
func HandlerForPathRegex(pathRegex string, handlerForPath http.Handler, defaultHandler http.Handler) http.Handler
HandlerForPathRegex is a simple alternative to using an HTTP router. It delegates to the specified handler if the path matches; otherwise to the default handler, or a 404 if that is nil.
func HandlerWithJSONResponse ¶
func HandlerWithJSONResponse(contentToEncode interface{}, additionalHeaders http.Header) http.Handler
HandlerWithJSONResponse creates an HTTP handler that returns a 200 status and the JSON encoding of the specified object.
func HandlerWithResponse ¶
HandlerWithResponse creates an HTTP handler that always returns the same status code, headers, and body.
func HandlerWithStatus ¶
HandlerWithStatus creates an HTTP handler that always returns the same status code.
func MakeSelfSignedCert ¶
MakeSelfSignedCert generates a self-signed certificate and writes it to the specified files. See: https://golang.org/src/crypto/tls/generate_cert.go
func MakeServerWithCert ¶
func MakeServerWithCert(certFilePath, keyFilePath string, handler http.Handler) (*httptest.Server, error)
MakeServerWithCert creates and starts a test HTTPS server using the specified certificate.
func PanicHandler ¶
PanicHandler creates an HTTP handler that will panic.
This is not useful inside an actual HTTP server, but when used in conjunction with NewHTTPClientFromHandler, it allows you to simulate an I/O error.
handler := PanicHandler(&net.AddrError{}) client := NewClientFromHandler(handler) // All requests made with this client will return an AddrError (or some other error that wraps that error)
func RecordingHandler ¶
func RecordingHandler(delegateToHandler http.Handler) (http.Handler, <-chan HTTPRequestInfo)
RecordingHandler wraps any HTTP handler in another handler that pushes received requests onto a channel.
handler, requestsCh := httphelpers.RecordingHandler(httphelpers.HandlerWithStatus(200)) httphelpers.WithServer(handler, func(server *http.TestServer) { doSomethingThatMakesARequest(server.URL) // request will receive a 200 status r := <-requestsCh verifyRequestPropertiesWereCorrect(r.Request, r.Body) })
func SequentialHandler ¶
SequentialHandler creates an HTTP handler that delegates to one handler per request, in the order given. If there are more requests than parameters, all subsequent requests go to the last handler.
In this example, the first HTTP request will get a 503, and all subsequent requests will get a 200.
handler := httphelpers.SequentialHandler( httphelpers.HandlerWithStatus(503), httphelpers.HandlerWithStatus(200) )
func WithSelfSignedServer ¶
func WithSelfSignedServer(handler http.Handler, action func(*httptest.Server, []byte, *x509.CertPool))
WithSelfSignedServer is a convenience function for starting a test HTTPS server with a self-signed certificate, running the specified function, and then closing the server and cleaning up the temporary certificate files. If for some reason creating the server fails, it panics. The action function's second and third parameters provide the CA certificate for configuring the client, and a preconfigured CertPool in case that is more convenient to use.
Types ¶
type DelegatingHandler ¶
DelegatingHandler is a struct that behaves as an http.Handler by delegating to the handler it wraps. Use this if you want to change the handler's behavior dynamically during a test.
dh := &httphelpers.DelegatingHandler{httphelpers.HandlerWithStatus(200)} server := httptest.NewServer(dh) // the server will return 200 dh.Handler = httphelpers.HandlerWithStatus(401) // now the server will return 401
func (*DelegatingHandler) ServeHTTP ¶
func (d *DelegatingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type HTTPRequestInfo ¶
type HTTPRequestInfo struct { Request *http.Request Body []byte // body has to be captured separately by server because you can't read it after the response is sent }
HTTPRequestInfo represents a request captured by NewRecordingHTTPHandler.