assist

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: BSD-2-Clause Imports: 6 Imported by: 0

README

Assist

Assist provides convenience and testing functions.

Types

  • ResponseWrapper
    • This is a wrapper that implements the http.ResponseWriter interface, allowing for the retrieval of the http.ResponseWriter numerical HTTP status code. This is useful for logging and testing.

Functions

  • MakePath(basePath, childPath)
    • Returns a new path, combining basePath and childPath, removing any // occurrences that may exist.
    • If basePath is missing a leading /, one will be added.
    • This is meant to be run while adding paths to an HTTP router during startup.
    • Functionality is similar to net/url.JoinPath(), dropping the error return.
  • NewResponseWraper
    • Returns the above described ResponseWrapper
  • WriteJSONResponse
    • Marshalls JSON data to a buffer and writes it to the provided http.ResponseWriter, setting the Content-Type header accordingly.
      • If the marshalling fails, an InternalServerError response is written with an empty body.
  • WriteResponse
    • Writes data to the provided http.ResponseWriter
      • If a write fails, an InternalServerError response is written with an empty body.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakePath added in v0.7.0

func MakePath(basePath string, subPath string) string

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

func NewBufferedTestResponseWriter(h http.Header, b *bytes.Buffer) http.ResponseWriter

NewBufferedTestResponseWriter returns an http.ResponseWriter instance that has an internal buffer pointer for verifying written data

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, data any, code int, logger log.Logger)

WriteJSONResponse marshals the provided data to a JSON string and writes the data to the http.ResponseWriter

func WriteResponse

func WriteResponse(w http.ResponseWriter, data []byte, code int, logger log.Logger)

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.

Jump to

Keyboard shortcuts

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