Documentation
¶
Index ¶
- func MakePath(basePath string, subPath string) string
- func NewBufferedTestResponseWriter(h http.Header, b *bytes.Buffer) http.ResponseWriter
- func WriteJSONResponse(w http.ResponseWriter, data any, code int, logger log.Logger)
- func WriteResponse(w http.ResponseWriter, data []byte, code int, logger log.Logger)
- type ResponseWrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MakePath ¶ added in v0.7.0
MakePath returns a combined path from basePath and subPath. The path is checked for `//` before returning. If the path does not start with a `/`, it will be added before returning.
The functionality of this is similar to net/url.JoinPath(), without the error return, and is not meant to be used for full URLs.
If the `basePath` is missing a leading `/`, one will be added.
Examples: MakePath("/", "test") -> "/test" MakePath("test", "path") -> "/test/path" MakePath("test/", "/path") -> "/test/path" MakePath("/test//", "/path////") -> "/test/path/"
This function is meant to be run at startup of a program, while adding a path to an HTTP router. The performance cost of creating the path and validating it will only be incurred once per added path.
func NewBufferedTestResponseWriter ¶ added in v0.6.5
NewBufferedTestResponseWriter returns an http.ResponseWriter instance that has an internal buffer pointer for verifying written data
func WriteJSONResponse ¶
WriteJSONResponse marshals the provided data to a JSON string and writes the data to the http.ResponseWriter
func WriteResponse ¶
WriteResponse writes a response to http.ResponseWriter, handling and logging any errors that may occur.
Types ¶
type ResponseWrapper ¶ added in v0.4.0
type ResponseWrapper interface {
// Header returns the stored http.Header
Header() http.Header
// ResponseWriter returns this interface as a http.ResponseWriter
// This is a convenience function only
ResponseWriter() http.ResponseWriter
// Status returns the written status code
Status() int
// Write simulates writing data to the writer and returns the predetermined error and write count
Write([]byte) (int, error)
// WriteHeader stores the statusCode to be returned for later checking with `ResponseWriter.Status`
WriteHeader(statusCode int)
}
ResponseWrapper is an interface that wraps an `http.ResponseWriter`, allowing for information to be retrieved for logging or to be used in testing
func NewResponseWrapper ¶ added in v0.4.0
func NewResponseWrapper(w http.ResponseWriter) ResponseWrapper
NewResponseWrapper returns a new ResponseWrapper that internally stores the provided http.ResponseWriter
func NewTestResponseWriter ¶ added in v0.6.2
func NewTestResponseWriter(h http.Header, writeCount int, writeError error) ResponseWrapper
NewTestResponseWriter returns a ResponseWrapper instance that can be used as an http.ResponseWriter in testing.