util

package
v0.0.0-...-2900902 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2015 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

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

func LogHandlerEntry(handler string, r *http.Request)

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

func WriteResponse(handler string, w http.ResponseWriter, v []byte, ct string, status int)

WriteResponse writes the given bytes to the response body, sets the content type to the given value, and then returns the given status.

func WriteYAML

func WriteYAML(handler string, w http.ResponseWriter, y []byte, status int)

WriteYAML writes the given bytes to the response body, sets the content type to "application/x-yaml; charset=UTF-8", and then returns the given status.

Types

type HTTPClient

type HTTPClient interface {
	Get(url string) (body string, code int, err error)
}

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

type HTTPDoer interface {
	Do(req *http.Request) (resp *http.Response, err error)
}

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

type ServerTester func(method, path, ctype string, reader io.Reader) (*http.Response, error)

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.

type Sleeper

type Sleeper interface {
	Sleep(d time.Duration)
}

Sleeper exposes a Sleep func which causes the current goroutine to sleep for the requested duration.

func NewSleeper

func NewSleeper() Sleeper

NewSleeper returns a new Sleeper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL