mockhttp

package
v0.0.0-...-9b8094e Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateErrors

func ValidateErrors(expected, result ServerError) error

Types

type JSONResponse

type JSONResponse[T any] struct {
	Val *T
	// contains filtered or unexported fields
}

func NewJSONResponse

func NewJSONResponse[T any]() *JSONResponse[T]

func ToJSONResponse

func ToJSONResponse[T any](res *http.Response) (*JSONResponse[T], error)

ToJSONResponse takes a httpResponse and maps it to a JSONResponse object It saves the status and parses the body into the expected type It will return an error if the http.Response is not a 200

func (*JSONResponse[T]) Body

func (r *JSONResponse[T]) Body() string

func (*JSONResponse[T]) Status

func (r *JSONResponse[T]) Status() int

func (*JSONResponse[T]) Validate

func (expected *JSONResponse[T]) Validate(result *JSONResponse[T]) error

Validate performs validation for two JSON Responses

func (*JSONResponse[T]) WithFailure

func (r *JSONResponse[T]) WithFailure(status int, val *T) *JSONResponse[T]

func (*JSONResponse[T]) WithStatus

func (r *JSONResponse[T]) WithStatus(status int) *JSONResponse[T]

func (*JSONResponse[T]) WithSuccess

func (r *JSONResponse[T]) WithSuccess(val *T) *JSONResponse[T]

func (*JSONResponse[T]) WithValidationFunc

func (r *JSONResponse[T]) WithValidationFunc(f func(expected, result T) error) *JSONResponse[T]

type PathParamType

type PathParamType int64
const (
	Chi PathParamType = iota
)

func (PathParamType) String

func (s PathParamType) String() string

type RawResponse

type RawResponse struct {
	// contains filtered or unexported fields
}

func NewRawResponse

func NewRawResponse() *RawResponse

func ToResponse

func ToResponse(res *http.Response) (*RawResponse, error)

ToResponse takes a httpResponse and maps it to a JSONResponse object It saves the status and saves the body

func (*RawResponse) Body

func (r *RawResponse) Body() string

func (*RawResponse) Status

func (r *RawResponse) Status() int

func (*RawResponse) WithBody

func (r *RawResponse) WithBody(body string) *RawResponse

func (*RawResponse) WithStatus

func (r *RawResponse) WithStatus(status int) *RawResponse

type Request

type Request struct {
	W *httptest.ResponseRecorder
	R *http.Request
}

func NewRequest

func NewRequest(method, path, body string) *Request

NewRequest creates a wrapper object around objects necessary to do a mock http request

func (*Request) Context

func (r *Request) Context() context.Context

func (*Request) Result

func (r *Request) Result() *http.Response

Result returns the *http.Response associated with the Http Request

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

SetHeader sets HTTP Header for wrapper object

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

func (*Request) WithHeaders

func (r *Request) WithHeaders(vals map[string]string) *Request

WithHeaders sets HTTP Header for wrapper object

func (*Request) WithPathParams

func (r *Request) WithPathParams(ptype PathParamType, vals map[string]string) *Request

With path params sets path params for the request The PathParamType denotes the routing package used to store path params

func (*Request) WithValues

func (r *Request) WithValues(vals map[string]interface{}) *Request

Inserts each key/value pair into context that gets inserted into the Request

type Response

type Response interface {
	Status() int
	Body() string
}

type ServerError

type ServerError struct {
	Status       string `json:"status"`
	DebugMessage string `json:"message"`
	Error        string `json:"error"`
}

type TestStruct

type TestStruct struct {
	Name     string
	Input    *Request
	Expected Response
}

Jump to

Keyboard shortcuts

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