Documentation ¶
Index ¶
- func LogAndReturnError(handler string, statusCode int, err error, w http.ResponseWriter)
- func LogHandlerEntry(handler string, r *http.Request)
- func LogHandlerExit(handler string, statusCode int, status string, w http.ResponseWriter)
- func LogHandlerExitWithJSON(handler string, w http.ResponseWriter, v interface{}, statusCode int)
- func LogHandlerExitWithYAML(handler string, w http.ResponseWriter, v interface{}, statusCode int)
- func MarshalAndWriteJSON(handler string, w http.ResponseWriter, v interface{}, statusCode int) []byte
- func MarshalAndWriteYAML(handler string, w http.ResponseWriter, v interface{}, statusCode int) []byte
- func TestHandlerWithURL(handler http.Handler, method, urlString string) (*httptest.ResponseRecorder, error)
- func ToJSONOrError(v interface{}) string
- func ToYAMLOrError(v interface{}) string
- func WriteJSON(handler string, w http.ResponseWriter, j []byte, status int)
- func WriteResponse(handler string, w http.ResponseWriter, v []byte, ct string, status int)
- func WriteYAML(handler string, w http.ResponseWriter, y []byte, status int)
- type HTTPClient
- type HTTPDoer
- type HandlerTester
- type ServerTester
- type Sleeper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LogAndReturnError ¶
func LogAndReturnError(handler string, statusCode int, err error, w http.ResponseWriter)
LogAndReturnError logs the given error and status to stderr, and then returns them as the HTTP response.
func LogHandlerEntry ¶
LogHandlerEntry logs the start of the given handler handling the given request.
func LogHandlerExit ¶
func LogHandlerExit(handler string, statusCode int, status string, w http.ResponseWriter)
LogHandlerExit logs the response from the given handler with the given results.
func LogHandlerExitWithJSON ¶
func LogHandlerExitWithJSON(handler string, w http.ResponseWriter, v interface{}, statusCode int)
LogHandlerExitWithJSON marshals the given object as JSON, writes it to the response body, returns the given status, and then logs the response.
func LogHandlerExitWithYAML ¶
func LogHandlerExitWithYAML(handler string, w http.ResponseWriter, v interface{}, statusCode int)
LogHandlerExitWithYAML marshals the given object as YAML, writes it to the response body, returns the given status, and then logs the response.
func MarshalAndWriteJSON ¶
func MarshalAndWriteJSON(handler string, w http.ResponseWriter, v interface{}, statusCode int) []byte
MarshalAndWriteJSON marshals the given object as JSON, writes it to the response body, and then returns the given status.
func MarshalAndWriteYAML ¶
func MarshalAndWriteYAML(handler string, w http.ResponseWriter, v interface{}, statusCode int) []byte
MarshalAndWriteYAML marshals the given object as YAML, writes it to the response body, and then returns the given status.
func TestHandlerWithURL ¶
func TestHandlerWithURL(handler http.Handler, method, urlString string) (*httptest.ResponseRecorder, error)
TestHandlerWithURL creates a HandlerTester with the given handler, and tests it with the given HTTP method and URL string using HandlerTester.TestWithURL.
func ToJSONOrError ¶
func ToJSONOrError(v interface{}) string
ToJSONOrError marshals the given object to JSON and returns either the resulting YAML or an error message. Useful when marshaling an object for a log entry.
func ToYAMLOrError ¶
func ToYAMLOrError(v interface{}) string
ToYAMLOrError marshals the given object to YAML and returns either the resulting YAML or an error message. Useful when marshaling an object for a log entry.
func WriteJSON ¶
func WriteJSON(handler string, w http.ResponseWriter, j []byte, status int)
WriteJSON writes the given bytes to the response body, sets the content type to "application/json; charset=UTF-8", and then returns the given status.
func WriteResponse ¶
WriteResponse writes the given bytes to the response body, sets the content type to the given value, and then returns the given status.
Types ¶
type HTTPClient ¶
HTTPClient is a higher level HTTP client which takes a URL and returns the response body as a string, along with the resulting status code and any errors.
func NewHTTPClient ¶
func NewHTTPClient(retries uint, c HTTPDoer, s Sleeper) HTTPClient
NewHTTPClient returns a new HTTPClient.
type HTTPDoer ¶
HTTPDoer is an interface for something that can 'Do' an http.Request and return an http.Response and error.
type HandlerTester ¶
type HandlerTester func(method, path, ctype string, reader io.Reader) (*httptest.ResponseRecorder, error)
A HandlerTester is a function that takes an HTTP method, an URL path, and a reader for a request body, creates a request from them, and serves it to the handler to which it was bound and returns a response recorder describing the outcome.
func NewHandlerTester ¶
func NewHandlerTester(handler http.Handler) HandlerTester
NewHandlerTester creates and returns a new HandlerTester for an http.Handler.
func (HandlerTester) TestWithURL ¶
func (h HandlerTester) TestWithURL(method, urlString string) (*httptest.ResponseRecorder, error)
TestWithURL invokes a HandlerTester with the given HTTP method, an URL path parsed from the given URL string, and a string reader on the query parameters parsed from the given URL string.
type ServerTester ¶
A ServerTester is a function that takes an HTTP method, an URL path, and a reader for a request body, creates a request from them, and serves it to a test server using the handler to which it was bound and returns the response.
func NewServerTester ¶
func NewServerTester(handler http.Handler) ServerTester
NewServerTester creates and returns a new NewServerTester for an http.Handler.