httpx

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2019 License: BSD-3-Clause Imports: 21 Imported by: 6

Documentation

Index

Constants

View Source
const (
	LocationHeaderKey = "Location"
)

Variables

View Source
var (
	InvalidParameterNameEscape  = merry.New("invalid parameter name escape")
	InvalidParameterValueEscape = merry.New("invalid parameter value escape")
	MalformedQueryParamter      = merry.New("malformed query parameter")
	MissingQueryParamter        = merry.New("missing query parameter")
	UnknownQueryParamter        = merry.New("unknown query parameter")
)
View Source
var (
	UnexpectedValue = merry.New("unexpected value")
)

Functions

func BoolValidator

func BoolValidator(p QueryParameter) merry.Error

BoolValidator enforces that all input values are parsable as a boolean

func ClientIP

func ClientIP(r *http.Request) string

ClientIP attempts to extract the IP address of the user agent from the request. The "X-Real-IP" and "X-Forwarded-For" headers are checked followed by the `RemoteAddr` field of the request. An empty string will be returned if nothing can be found.

func GenerateID

func GenerateID(r *http.Request) string

GenerateID creates a globally unique string for the request. It creates a version 5 UUID with the concatenation of current unix nanos, three bytes of random data, the client ip address, the request method and the request URI. This function is *not* idempotent.

func HTTPListenerForAddress

func HTTPListenerForAddress(addr string) (net.Listener, merry.Error)

HTTPListenerForAddress returns a TCP listener for the given address. If the address is empty `":http"` is used.

func PutRequest

func PutRequest(request *Request)

PutRequest returns the given Request back to the shared pool.

func WriteResponse

func WriteResponse(w http.ResponseWriter, response Response) (err merry.Error)

WriteResponse serializes a response instance to the ResponseWriter. `ResponseWriter.WriteHeader` will always be called with the value of `Response.StatusCode()` so it is not safe to use the `ResponseWriter` after calling this function. Any error returned from `Response.Serialize` will be returned.

Types

type BasicResponse

type BasicResponse struct {
	Code int

	Error error
	// contains filtered or unexported fields
}

func (*BasicResponse) Err

func (r *BasicResponse) Err() error

func (*BasicResponse) Headers

func (r *BasicResponse) Headers() http.Header

func (*BasicResponse) Serialize

func (r *BasicResponse) Serialize(io.Writer) merry.Error

func (*BasicResponse) StatusCode

func (r *BasicResponse) StatusCode() int

func (*BasicResponse) Trailers

func (r *BasicResponse) Trailers() http.Header

type CompletionHook

type CompletionHook func(context.Context, *Request, ResponseSnapshot)

func (CompletionHook) InvokeSafely

func (h CompletionHook) InvokeSafely(ctx context.Context, request *Request, snapshot ResponseSnapshot) (exception merry.Error)

type ErrorHandler

type ErrorHandler func(context.Context, *Request, merry.Error) Response

ErrorHandler creates a response for the given error condition.

func (ErrorHandler) InvokeSafely

func (h ErrorHandler) InvokeSafely(ctx context.Context, request *Request, err merry.Error) (_ Response, exception merry.Error)

type ErrorHook

type ErrorHook func(context.Context, *Request, merry.Error)

func (ErrorHook) InvokeSafely

func (h ErrorHook) InvokeSafely(ctx context.Context, request *Request, err merry.Error) (exception merry.Error)

type FakeHandler added in v0.4.0

type FakeHandler struct {
	ServeHTTPHook func(http.ResponseWriter, *http.Request)

	ServeHTTPCalls []*HandlerServeHTTPInvocation
}

FakeHandler is a mock implementation of Handler for testing. Use it in your tests as in this example:

package example

func TestWithHandler(t *testing.T) {
	f := &httpx.FakeHandler{
		ServeHTTPHook: func(ident25 http.ResponseWriter, ident26 *http.Request) () {
			// ensure parameters meet expections, signal errors using t, etc
			return
		},
	}

	// test code goes here ...

	// assert state of FakeServeHTTP ...
	f.AssertServeHTTPCalledOnce(t)
}

Create anonymous function implementations for only those interface methods that should be called in the code under test. This will force a panic if any unexpected calls are made to FakeServeHTTP.

func NewFakeHandlerDefaultError added in v0.4.0

func NewFakeHandlerDefaultError(t HandlerTestingT) *FakeHandler

NewFakeHandlerDefaultError returns an instance of FakeHandler with all hooks configured to call t.Error

func NewFakeHandlerDefaultFatal added in v0.4.0

func NewFakeHandlerDefaultFatal(t HandlerTestingT) *FakeHandler

NewFakeHandlerDefaultFatal returns an instance of FakeHandler with all hooks configured to call t.Fatal

func NewFakeHandlerDefaultPanic added in v0.4.0

func NewFakeHandlerDefaultPanic() *FakeHandler

NewFakeHandlerDefaultPanic returns an instance of FakeHandler with all hooks configured to panic

func (*FakeHandler) AssertServeHTTPCalled added in v0.4.0

func (f *FakeHandler) AssertServeHTTPCalled(t HandlerTestingT)

AssertServeHTTPCalled calls t.Error if FakeHandler.ServeHTTP was not called

func (*FakeHandler) AssertServeHTTPCalledN added in v0.4.0

func (f *FakeHandler) AssertServeHTTPCalledN(t HandlerTestingT, n int)

AssertServeHTTPCalledN calls t.Error if FakeHandler.ServeHTTP was called less than n times

func (*FakeHandler) AssertServeHTTPCalledOnce added in v0.4.0

func (f *FakeHandler) AssertServeHTTPCalledOnce(t HandlerTestingT)

AssertServeHTTPCalledOnce calls t.Error if FakeHandler.ServeHTTP was not called exactly once

func (*FakeHandler) AssertServeHTTPCalledOnceWith added in v0.4.0

func (_f5 *FakeHandler) AssertServeHTTPCalledOnceWith(t HandlerTestingT, ident25 http.ResponseWriter, ident26 *http.Request)

AssertServeHTTPCalledOnceWith calls t.Error if FakeHandler.ServeHTTP was not called exactly once with the given values

func (*FakeHandler) AssertServeHTTPCalledWith added in v0.4.0

func (_f3 *FakeHandler) AssertServeHTTPCalledWith(t HandlerTestingT, ident25 http.ResponseWriter, ident26 *http.Request)

AssertServeHTTPCalledWith calls t.Error if FakeHandler.ServeHTTP was not called with the given values

func (*FakeHandler) AssertServeHTTPNotCalled added in v0.4.0

func (f *FakeHandler) AssertServeHTTPNotCalled(t HandlerTestingT)

AssertServeHTTPNotCalled calls t.Error if FakeHandler.ServeHTTP was called

func (*FakeHandler) Reset added in v0.4.0

func (f *FakeHandler) Reset()

func (*FakeHandler) ServeHTTP added in v0.4.0

func (_f1 *FakeHandler) ServeHTTP(ident25 http.ResponseWriter, ident26 *http.Request)

func (*FakeHandler) ServeHTTPCalled added in v0.4.0

func (f *FakeHandler) ServeHTTPCalled() bool

ServeHTTPCalled returns true if FakeHandler.ServeHTTP was called

func (*FakeHandler) ServeHTTPCalledN added in v0.4.0

func (f *FakeHandler) ServeHTTPCalledN(n int) bool

ServeHTTPCalledN returns true if FakeHandler.ServeHTTP was called at least n times

func (*FakeHandler) ServeHTTPCalledOnce added in v0.4.0

func (f *FakeHandler) ServeHTTPCalledOnce() bool

ServeHTTPCalledOnce returns true if FakeHandler.ServeHTTP was called exactly once

func (*FakeHandler) ServeHTTPCalledOnceWith added in v0.4.0

func (_f4 *FakeHandler) ServeHTTPCalledOnceWith(ident25 http.ResponseWriter, ident26 *http.Request) bool

ServeHTTPCalledOnceWith returns true if FakeHandler.ServeHTTP was called exactly once with the given values

func (*FakeHandler) ServeHTTPCalledWith added in v0.4.0

func (_f2 *FakeHandler) ServeHTTPCalledWith(ident25 http.ResponseWriter, ident26 *http.Request) (found bool)

ServeHTTPCalledWith returns true if FakeHandler.ServeHTTP was called with the given values

func (*FakeHandler) ServeHTTPNotCalled added in v0.4.0

func (f *FakeHandler) ServeHTTPNotCalled() bool

ServeHTTPNotCalled returns true if FakeHandler.ServeHTTP was not called

type FakeResponse

type FakeResponse struct {
	StatusCodeHook func() int
	HeadersHook    func() http.Header
	TrailersHook   func() http.Header
	ErrHook        func() error
	SerializeHook  func(io.Writer) merry.Error

	StatusCodeCalls []*ResponseStatusCodeInvocation
	HeadersCalls    []*ResponseHeadersInvocation
	TrailersCalls   []*ResponseTrailersInvocation
	ErrCalls        []*ResponseErrInvocation
	SerializeCalls  []*ResponseSerializeInvocation
}

FakeResponse is a mock implementation of Response for testing. Use it in your tests as in this example:

package example

func TestWithResponse(t *testing.T) {
	f := &httpx.FakeResponse{
		StatusCodeHook: func() (ident1 int) {
			// ensure parameters meet expections, signal errors using t, etc
			return
		},
	}

	// test code goes here ...

	// assert state of FakeStatusCode ...
	f.AssertStatusCodeCalledOnce(t)
}

Create anonymous function implementations for only those interface methods that should be called in the code under test. This will force a panic if any unexpected calls are made to FakeStatusCode.

func NewFakeResponseDefaultError

func NewFakeResponseDefaultError(t ResponseTestingT) *FakeResponse

NewFakeResponseDefaultError returns an instance of FakeResponse with all hooks configured to call t.Error

func NewFakeResponseDefaultFatal

func NewFakeResponseDefaultFatal(t ResponseTestingT) *FakeResponse

NewFakeResponseDefaultFatal returns an instance of FakeResponse with all hooks configured to call t.Fatal

func NewFakeResponseDefaultPanic

func NewFakeResponseDefaultPanic() *FakeResponse

NewFakeResponseDefaultPanic returns an instance of FakeResponse with all hooks configured to panic

func (*FakeResponse) AssertErrCalled

func (f *FakeResponse) AssertErrCalled(t ResponseTestingT)

AssertErrCalled calls t.Error if FakeResponse.Err was not called

func (*FakeResponse) AssertErrCalledN

func (f *FakeResponse) AssertErrCalledN(t ResponseTestingT, n int)

AssertErrCalledN calls t.Error if FakeResponse.Err was called less than n times

func (*FakeResponse) AssertErrCalledOnce

func (f *FakeResponse) AssertErrCalledOnce(t ResponseTestingT)

AssertErrCalledOnce calls t.Error if FakeResponse.Err was not called exactly once

func (*FakeResponse) AssertErrNotCalled

func (f *FakeResponse) AssertErrNotCalled(t ResponseTestingT)

AssertErrNotCalled calls t.Error if FakeResponse.Err was called

func (*FakeResponse) AssertHeadersCalled

func (f *FakeResponse) AssertHeadersCalled(t ResponseTestingT)

AssertHeadersCalled calls t.Error if FakeResponse.Headers was not called

func (*FakeResponse) AssertHeadersCalledN

func (f *FakeResponse) AssertHeadersCalledN(t ResponseTestingT, n int)

AssertHeadersCalledN calls t.Error if FakeResponse.Headers was called less than n times

func (*FakeResponse) AssertHeadersCalledOnce

func (f *FakeResponse) AssertHeadersCalledOnce(t ResponseTestingT)

AssertHeadersCalledOnce calls t.Error if FakeResponse.Headers was not called exactly once

func (*FakeResponse) AssertHeadersNotCalled

func (f *FakeResponse) AssertHeadersNotCalled(t ResponseTestingT)

AssertHeadersNotCalled calls t.Error if FakeResponse.Headers was called

func (*FakeResponse) AssertSerializeCalled

func (f *FakeResponse) AssertSerializeCalled(t ResponseTestingT)

AssertSerializeCalled calls t.Error if FakeResponse.Serialize was not called

func (*FakeResponse) AssertSerializeCalledN

func (f *FakeResponse) AssertSerializeCalledN(t ResponseTestingT, n int)

AssertSerializeCalledN calls t.Error if FakeResponse.Serialize was called less than n times

func (*FakeResponse) AssertSerializeCalledOnce

func (f *FakeResponse) AssertSerializeCalledOnce(t ResponseTestingT)

AssertSerializeCalledOnce calls t.Error if FakeResponse.Serialize was not called exactly once

func (*FakeResponse) AssertSerializeCalledOnceWith

func (_f17 *FakeResponse) AssertSerializeCalledOnceWith(t ResponseTestingT, ident1 io.Writer)

AssertSerializeCalledOnceWith calls t.Error if FakeResponse.Serialize was not called exactly once with the given values

func (*FakeResponse) AssertSerializeCalledWith

func (_f15 *FakeResponse) AssertSerializeCalledWith(t ResponseTestingT, ident1 io.Writer)

AssertSerializeCalledWith calls t.Error if FakeResponse.Serialize was not called with the given values

func (*FakeResponse) AssertSerializeNotCalled

func (f *FakeResponse) AssertSerializeNotCalled(t ResponseTestingT)

AssertSerializeNotCalled calls t.Error if FakeResponse.Serialize was called

func (*FakeResponse) AssertStatusCodeCalled

func (f *FakeResponse) AssertStatusCodeCalled(t ResponseTestingT)

AssertStatusCodeCalled calls t.Error if FakeResponse.StatusCode was not called

func (*FakeResponse) AssertStatusCodeCalledN

func (f *FakeResponse) AssertStatusCodeCalledN(t ResponseTestingT, n int)

AssertStatusCodeCalledN calls t.Error if FakeResponse.StatusCode was called less than n times

func (*FakeResponse) AssertStatusCodeCalledOnce

func (f *FakeResponse) AssertStatusCodeCalledOnce(t ResponseTestingT)

AssertStatusCodeCalledOnce calls t.Error if FakeResponse.StatusCode was not called exactly once

func (*FakeResponse) AssertStatusCodeNotCalled

func (f *FakeResponse) AssertStatusCodeNotCalled(t ResponseTestingT)

AssertStatusCodeNotCalled calls t.Error if FakeResponse.StatusCode was called

func (*FakeResponse) AssertTrailersCalled

func (f *FakeResponse) AssertTrailersCalled(t ResponseTestingT)

AssertTrailersCalled calls t.Error if FakeResponse.Trailers was not called

func (*FakeResponse) AssertTrailersCalledN

func (f *FakeResponse) AssertTrailersCalledN(t ResponseTestingT, n int)

AssertTrailersCalledN calls t.Error if FakeResponse.Trailers was called less than n times

func (*FakeResponse) AssertTrailersCalledOnce

func (f *FakeResponse) AssertTrailersCalledOnce(t ResponseTestingT)

AssertTrailersCalledOnce calls t.Error if FakeResponse.Trailers was not called exactly once

func (*FakeResponse) AssertTrailersNotCalled

func (f *FakeResponse) AssertTrailersNotCalled(t ResponseTestingT)

AssertTrailersNotCalled calls t.Error if FakeResponse.Trailers was called

func (*FakeResponse) Err

func (_f7 *FakeResponse) Err() (ident1 error)

func (*FakeResponse) ErrCalled

func (f *FakeResponse) ErrCalled() bool

ErrCalled returns true if FakeResponse.Err was called

func (*FakeResponse) ErrCalledN

func (f *FakeResponse) ErrCalledN(n int) bool

ErrCalledN returns true if FakeResponse.Err was called at least n times

func (*FakeResponse) ErrCalledOnce

func (f *FakeResponse) ErrCalledOnce() bool

ErrCalledOnce returns true if FakeResponse.Err was called exactly once

func (*FakeResponse) ErrNotCalled

func (f *FakeResponse) ErrNotCalled() bool

ErrNotCalled returns true if FakeResponse.Err was not called

func (*FakeResponse) Headers

func (_f3 *FakeResponse) Headers() (ident1 http.Header)

func (*FakeResponse) HeadersCalled

func (f *FakeResponse) HeadersCalled() bool

HeadersCalled returns true if FakeResponse.Headers was called

func (*FakeResponse) HeadersCalledN

func (f *FakeResponse) HeadersCalledN(n int) bool

HeadersCalledN returns true if FakeResponse.Headers was called at least n times

func (*FakeResponse) HeadersCalledOnce

func (f *FakeResponse) HeadersCalledOnce() bool

HeadersCalledOnce returns true if FakeResponse.Headers was called exactly once

func (*FakeResponse) HeadersNotCalled

func (f *FakeResponse) HeadersNotCalled() bool

HeadersNotCalled returns true if FakeResponse.Headers was not called

func (*FakeResponse) Reset

func (f *FakeResponse) Reset()

func (*FakeResponse) Serialize

func (_f9 *FakeResponse) Serialize(ident1 io.Writer) (ident2 merry.Error)

func (*FakeResponse) SerializeCalled

func (f *FakeResponse) SerializeCalled() bool

SerializeCalled returns true if FakeResponse.Serialize was called

func (*FakeResponse) SerializeCalledN

func (f *FakeResponse) SerializeCalledN(n int) bool

SerializeCalledN returns true if FakeResponse.Serialize was called at least n times

func (*FakeResponse) SerializeCalledOnce

func (f *FakeResponse) SerializeCalledOnce() bool

SerializeCalledOnce returns true if FakeResponse.Serialize was called exactly once

func (*FakeResponse) SerializeCalledOnceWith

func (_f16 *FakeResponse) SerializeCalledOnceWith(ident1 io.Writer) bool

SerializeCalledOnceWith returns true if FakeResponse.Serialize was called exactly once with the given values

func (*FakeResponse) SerializeCalledWith

func (_f14 *FakeResponse) SerializeCalledWith(ident1 io.Writer) (found bool)

SerializeCalledWith returns true if FakeResponse.Serialize was called with the given values

func (*FakeResponse) SerializeNotCalled

func (f *FakeResponse) SerializeNotCalled() bool

SerializeNotCalled returns true if FakeResponse.Serialize was not called

func (*FakeResponse) SerializeResultsForCall

func (_f18 *FakeResponse) SerializeResultsForCall(ident1 io.Writer) (ident2 merry.Error, found bool)

SerializeResultsForCall returns the result values for the first call to FakeResponse.Serialize with the given values

func (*FakeResponse) SetErrStub added in v0.3.1

func (_f8 *FakeResponse) SetErrStub(ident1 error)

SetErrStub configures Response.Err to always return the given values

func (*FakeResponse) SetHeadersStub added in v0.3.1

func (_f4 *FakeResponse) SetHeadersStub(ident1 http.Header)

SetHeadersStub configures Response.Headers to always return the given values

func (*FakeResponse) SetSerializeInvocation added in v0.3.1

func (_f11 *FakeResponse) SetSerializeInvocation(calls_f12 []*ResponseSerializeInvocation, fallback_f13 func() merry.Error)

SetSerializeInvocation configures Response.Serialize to return the given results when called with the given parameters If no match is found for an invocation the result(s) of the fallback function are returned

func (*FakeResponse) SetSerializeStub added in v0.3.1

func (_f10 *FakeResponse) SetSerializeStub(ident2 merry.Error)

SetSerializeStub configures Response.Serialize to always return the given values

func (*FakeResponse) SetStatusCodeStub added in v0.3.1

func (_f2 *FakeResponse) SetStatusCodeStub(ident1 int)

SetStatusCodeStub configures Response.StatusCode to always return the given values

func (*FakeResponse) SetTrailersStub added in v0.3.1

func (_f6 *FakeResponse) SetTrailersStub(ident1 http.Header)

SetTrailersStub configures Response.Trailers to always return the given values

func (*FakeResponse) StatusCode

func (_f1 *FakeResponse) StatusCode() (ident1 int)

func (*FakeResponse) StatusCodeCalled

func (f *FakeResponse) StatusCodeCalled() bool

StatusCodeCalled returns true if FakeResponse.StatusCode was called

func (*FakeResponse) StatusCodeCalledN

func (f *FakeResponse) StatusCodeCalledN(n int) bool

StatusCodeCalledN returns true if FakeResponse.StatusCode was called at least n times

func (*FakeResponse) StatusCodeCalledOnce

func (f *FakeResponse) StatusCodeCalledOnce() bool

StatusCodeCalledOnce returns true if FakeResponse.StatusCode was called exactly once

func (*FakeResponse) StatusCodeNotCalled

func (f *FakeResponse) StatusCodeNotCalled() bool

StatusCodeNotCalled returns true if FakeResponse.StatusCode was not called

func (*FakeResponse) Trailers

func (_f5 *FakeResponse) Trailers() (ident1 http.Header)

func (*FakeResponse) TrailersCalled

func (f *FakeResponse) TrailersCalled() bool

TrailersCalled returns true if FakeResponse.Trailers was called

func (*FakeResponse) TrailersCalledN

func (f *FakeResponse) TrailersCalledN(n int) bool

TrailersCalledN returns true if FakeResponse.Trailers was called at least n times

func (*FakeResponse) TrailersCalledOnce

func (f *FakeResponse) TrailersCalledOnce() bool

TrailersCalledOnce returns true if FakeResponse.Trailers was called exactly once

func (*FakeResponse) TrailersNotCalled

func (f *FakeResponse) TrailersNotCalled() bool

TrailersNotCalled returns true if FakeResponse.Trailers was not called

type FixedStringValidator

type FixedStringValidator struct {
	Target string
}

FixedStringValidator enforces that all input values match a fixed string

func (FixedStringValidator) Validate

type Handler

type Handler func(context.Context, *Request) Response

Handler is a block of logic to apply to a request. Returning a non-nil Response indicates request processing should stop.

func AdaptStandardHandler added in v0.4.0

func AdaptStandardHandler(handler http.Handler) Handler

AdaptStandardHandler allows an http.Hander to be used as an httpx.Handler

func (Handler) InvokeSafely

func (h Handler) InvokeSafely(ctx context.Context, request *Request) (_ Response, exception merry.Error)

type HandlerServeHTTPInvocation added in v0.4.0

type HandlerServeHTTPInvocation struct {
	Parameters struct {
		Ident25 http.ResponseWriter
		Ident26 *http.Request
	}
}

HandlerServeHTTPInvocation represents a single call of FakeHandler.ServeHTTP

type HandlerTestingT added in v0.4.0

type HandlerTestingT interface {
	Error(...interface{})
	Errorf(string, ...interface{})
	Fatal(...interface{})
	Helper()
}

HandlerTestingT represents the methods of "testing".T used by charlatan Fakes. It avoids importing the testing package.

type IntValidator

type IntValidator struct {
	Min *int
	Max *int
}

IntValidator enforces that all input values are parsable as integers. It also optionally enforces that values are within a range.

func (IntValidator) Validate

func (v IntValidator) Validate(p QueryParameter) merry.Error

type JsonResponse

type JsonResponse struct {
	BasicResponse
	Payload json.Marshaler
}

func (*JsonResponse) Serialize

func (r *JsonResponse) Serialize(w io.Writer) merry.Error

type ParameterSchema added in v0.4.0

type ParameterSchema struct {
	Name         string         // Match keys by exact value
	Regex        *regexp.Regexp // Match keys by pattern
	Default      string         `json:",omitempty"` // Default value for `Name`
	Validator    Validator      `json:"-"`          // Optional validator of value(s)
	Multiplicity uint           `json:",omitempty"` // Value count, 0 is unlimited
	Required     bool           // Is this input mandatory?
}

ParameterSchema is the schema to validate a query parameter or request. Either `Name` or `Regex` _should_ be provided. Providing `Default` *requires* `Name`. If `Default` is provided and a matching input is not presented then a syntheitic value will be created.

func (ParameterSchema) Match added in v0.4.0

func (f ParameterSchema) Match(name string) bool

Match returns true if the given key name is for this ParameterSchema

func (ParameterSchema) Validate added in v0.4.0

Validate returns an error if all input values don't meet the criteria of `ParameterSchema.Validator`. If the validator panics an error will be returned in the second result parameter.

type PathParameter

type PathParameter struct {
	Name  string
	Value string
}

PathParameter is a single URL path parameter.

type QueryParameter

type QueryParameter struct {
	Name   string      // the key of the query parameter pair
	Values []string    // the concatenated values
	Err    merry.Error // the param is unparsable or invalid
}

QueryParameter is a single URL query parameter passed to an endpoint.

func (*QueryParameter) Bool

func (p *QueryParameter) Bool(v *bool) merry.Error

func (*QueryParameter) BoolSlice

func (p *QueryParameter) BoolSlice(v *[]bool) merry.Error

func (*QueryParameter) CSV

func (p *QueryParameter) CSV(v *[]string) merry.Error

func (*QueryParameter) Int

func (p *QueryParameter) Int(v *int) merry.Error

func (*QueryParameter) Int16

func (p *QueryParameter) Int16(v *int16) merry.Error

func (*QueryParameter) Int16Slice

func (p *QueryParameter) Int16Slice(v *[]int16) merry.Error

func (*QueryParameter) Int32

func (p *QueryParameter) Int32(v *int32) merry.Error

func (*QueryParameter) Int32Slice

func (p *QueryParameter) Int32Slice(v *[]int32) merry.Error

func (*QueryParameter) Int64

func (p *QueryParameter) Int64(v *int64) merry.Error

func (*QueryParameter) Int64Slice

func (p *QueryParameter) Int64Slice(v *[]int64) merry.Error

func (*QueryParameter) Int8

func (p *QueryParameter) Int8(v *int8) merry.Error

func (*QueryParameter) Int8Slice

func (p *QueryParameter) Int8Slice(v *[]int8) merry.Error

func (*QueryParameter) IntSlice

func (p *QueryParameter) IntSlice(v *[]int) merry.Error

func (*QueryParameter) Time

func (p *QueryParameter) Time(v *time.Time, format string) merry.Error

func (*QueryParameter) TimeSlice

func (p *QueryParameter) TimeSlice(v *[]time.Time, format string) merry.Error

func (*QueryParameter) Uint

func (p *QueryParameter) Uint(v *uint) merry.Error

func (*QueryParameter) Uint16

func (p *QueryParameter) Uint16(v *uint16) merry.Error

func (*QueryParameter) Uint16Slice

func (p *QueryParameter) Uint16Slice(v *[]uint16) merry.Error

func (*QueryParameter) Uint32

func (p *QueryParameter) Uint32(v *uint32) merry.Error

func (*QueryParameter) Uint32Slice

func (p *QueryParameter) Uint32Slice(v *[]uint32) merry.Error

func (*QueryParameter) Uint64

func (p *QueryParameter) Uint64(v *uint64) merry.Error

func (*QueryParameter) Uint64Slice

func (p *QueryParameter) Uint64Slice(v *[]uint64) merry.Error

func (*QueryParameter) Uint8

func (p *QueryParameter) Uint8(v *uint8) merry.Error

func (*QueryParameter) Uint8Slice

func (p *QueryParameter) Uint8Slice(v *[]uint8) merry.Error

func (*QueryParameter) UintSlice

func (p *QueryParameter) UintSlice(v *[]uint) merry.Error

type RegexValidator

type RegexValidator struct {
	Regex *regexp.Regexp
}

RegexValidator enforces that all input values match the given regular expression.

func (RegexValidator) Validate

func (v RegexValidator) Validate(p QueryParameter) merry.Error

type Request

type Request struct {
	*http.Request
	PathParams  []PathParameter
	QueryParams []*QueryParameter
	// contains filtered or unexported fields
}

func GetRequest

func GetRequest(parent *http.Request) *Request

GetRequest returns a Request instance from the shared pool, ready for (re)use.

func (*Request) ClientIP

func (r *Request) ClientIP() string

ClientIP attempts to extract the IP address of the user agent from the request. The "X-Real-IP" and "X-Forwarded-For" headers are checked followed by the `RemoteAddr` field of the request. An empty string will be returned if nothing can be found.

func (*Request) ID

func (r *Request) ID() string

ID returns a globally unique string for the request. It creates a version 5 UUID with the concatenation of current unix nanos, three bytes of random data, the client ip address, the request method and the request URI. This method is idempotent.

func (*Request) ParseQueryParameters

func (r *Request) ParseQueryParameters() bool

ParseQueryParameters parses the URL-encoded query string and fills in the `QueryParams` field. Any existing values will be lost when this method is called.

func (*Request) PathParamExists

func (r *Request) PathParamExists(name string) bool

func (*Request) QueryParamExists

func (r *Request) QueryParamExists(name string) bool

func (*Request) ValidateQueryParameters

func (r *Request) ValidateQueryParameters(schemas []ParameterSchema) (malformed bool, unknown bool, exception merry.Error)

ValidateQueryParameters validates the values in `QueryParams` with the provided fields. If no fields are given, no action will be taken. Validation errors are assigned to the problematic parameter and placeholder instances are created for missing required or unknown parameters.

The `malformed` and `unknown` return values indicate if any paramters fail validation or do not match a field, respectively. If a validator panics an error will be returned in `err`.

type RequestPredicate

type RequestPredicate func(context.Context, *Request) bool

RequestPredicate examines the given context and request and returns a determination based on that analysis.

func (RequestPredicate) InvokeSafely

func (h RequestPredicate) InvokeSafely(ctx context.Context, request *Request) (_ bool, exception merry.Error)

type Response

type Response interface {
	Serializer
	StatusCode() int
	Headers() http.Header
	Trailers() http.Header
	Err() error
}

func NewEmpty

func NewEmpty(code int) Response

func NewEmptyError

func NewEmptyError(code int, err error) Response

func NewOK

func NewOK(body json.Marshaler) Response

func NewSeeOther

func NewSeeOther(location string) Response

func NewTemporaryRedirect

func NewTemporaryRedirect(location string) Response

type ResponseAdapter

type ResponseAdapter struct {
	*http.Response
}

ResponseAdapter is an adapter for `http.Response` to the `Response` interface.

func (ResponseAdapter) Err

func (r ResponseAdapter) Err() error

func (ResponseAdapter) Headers

func (r ResponseAdapter) Headers() http.Header

func (ResponseAdapter) Serialize

func (r ResponseAdapter) Serialize(w io.Writer) merry.Error

func (ResponseAdapter) StatusCode

func (r ResponseAdapter) StatusCode() int

func (ResponseAdapter) Trailers

func (r ResponseAdapter) Trailers() http.Header

type ResponseErrInvocation

type ResponseErrInvocation struct {
	Results struct {
		Ident1 error
	}
}

ResponseErrInvocation represents a single call of FakeResponse.Err

type ResponseHeadersInvocation

type ResponseHeadersInvocation struct {
	Results struct {
		Ident1 http.Header
	}
}

ResponseHeadersInvocation represents a single call of FakeResponse.Headers

type ResponseInterceptor

type ResponseInterceptor struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseInterceptor implements `http.ResponseWriter` to capture the outgoing response status code and size.

func (*ResponseInterceptor) Elapsed

func (i *ResponseInterceptor) Elapsed() time.Duration

Elapsed returns the amount of time since this instance was created.

func (*ResponseInterceptor) Flush

Flush attempts to call the `Flush` method on the underlying `ResponseWriter`, if it implments the `http.Flusher` interface.

func (*ResponseInterceptor) Header

func (i *ResponseInterceptor) Header() http.Header

func (*ResponseInterceptor) Snapshot

func (i *ResponseInterceptor) Snapshot() ResponseSnapshot

func (*ResponseInterceptor) Start added in v0.3.1

func (i *ResponseInterceptor) Start() time.Time

Start returns the time this instance was created.

func (*ResponseInterceptor) Write

func (i *ResponseInterceptor) Write(data []byte) (int, error)

func (*ResponseInterceptor) WriteHeader

func (i *ResponseInterceptor) WriteHeader(status int)

func (*ResponseInterceptor) WriteResponse

func (i *ResponseInterceptor) WriteResponse(response Response) merry.Error

WriteResponse serializes a response instance The `WriteHeader` method will *always* be called with the value of `Response.StatusCode()` so it is not safe to use the `ResponseWriter` methods of this instance after calling this method. Any error returned from `Response.Serialize` will be returned.

type ResponseSerializeInvocation

type ResponseSerializeInvocation struct {
	Parameters struct {
		Ident1 io.Writer
	}
	Results struct {
		Ident2 merry.Error
	}
}

ResponseSerializeInvocation represents a single call of FakeResponse.Serialize

func NewResponseSerializeInvocation added in v0.3.1

func NewResponseSerializeInvocation(ident1 io.Writer, ident2 merry.Error) *ResponseSerializeInvocation

NewResponseSerializeInvocation creates a new instance of ResponseSerializeInvocation

type ResponseSnapshot

type ResponseSnapshot struct {
	// Status code returned to the user agent
	StatusCode int
	// Size of the response body in bytes
	Size int
	// Time request servicing began
	Start time.Time
	// Duration of request servicing
	Elapsed time.Duration
}

ResponseSnapshot captures details about the response sent to the user agent.

type ResponseStatusCodeInvocation

type ResponseStatusCodeInvocation struct {
	Results struct {
		Ident1 int
	}
}

ResponseStatusCodeInvocation represents a single call of FakeResponse.StatusCode

type ResponseTestingT

type ResponseTestingT interface {
	Error(...interface{})
	Errorf(string, ...interface{})
	Fatal(...interface{})
	Helper()
}

ResponseTestingT represents the methods of "testing".T used by charlatan Fakes. It avoids importing the testing package.

type ResponseTrailersInvocation

type ResponseTrailersInvocation struct {
	Results struct {
		Ident1 http.Header
	}
}

ResponseTrailersInvocation represents a single call of FakeResponse.Trailers

type Serializer added in v0.4.2

type Serializer interface {
	Serialize(io.Writer) merry.Error
}

type StringExtractor

type StringExtractor func(context.Context, *Request) (string, merry.Error)

StringExtractor is a function type that extracts a string from the given `context.Context` and `*httpx.Request`. An error is returned if the string could not be extracted.

func (StringExtractor) InvokeSafely

func (h StringExtractor) InvokeSafely(ctx context.Context, request *Request) (str string, err merry.Error, exception merry.Error)

type StringSliceValidator

type StringSliceValidator struct {
	Target []string
}

StringSliceValidator enforces that all input values match at least one of the given strings

func (StringSliceValidator) Validate

type StringValidator

type StringValidator struct {
	MinLen uint
	MaxLen uint
}

StringValidator enforces that all input values have a certain length

func (StringValidator) Validate

func (v StringValidator) Validate(p QueryParameter) merry.Error

type TimestampValidator

type TimestampValidator struct {
	Format string
	Min    *time.Time
	Max    *time.Time
}

TimestampValidator enforces that all input values are parsable as a timestamp with a certain format. It also optionally enforces the time value falls within a range.

func (TimestampValidator) Validate

type UIntValidator added in v0.4.0

type UIntValidator struct {
	Min *uint
	Max *uint
}

UIntValidator enforces that all input values are parsable as uints. It also optionally enforces that values are within a range.

func (UIntValidator) Validate added in v0.4.0

func (v UIntValidator) Validate(p QueryParameter) merry.Error

type Validator

type Validator func(QueryParameter) merry.Error

Validator ensures that the given strings all meet certain criteria.

func (Validator) InvokeSafely

func (v Validator) InvokeSafely(p QueryParameter) (_ merry.Error, exception merry.Error)

Jump to

Keyboard shortcuts

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