Documentation
¶
Index ¶
- Constants
- func ClearMockedResponses()
- func MockCatchAll(generator MockedResponseGenerator)
- func MockResponse(req *Request, generator MockedResponseGenerator)
- func MockResponseFromBinary(req *Request, statusCode int, responseBody []byte)
- func MockResponseFromFile(verb string, url string, statusCode int, responseFilePath string)
- func MockResponseFromString(verb string, url string, statusCode int, responseBody string)
- func NewRequestListener(listener func(Event)) logger.Listener
- type Buffer
- type CertInfo
- type CreateTransportHandler
- type Deserializer
- type Event
- type Meta
- type MockedResponse
- type MockedResponseGenerator
- type MockedResponseProvider
- type OutgoingRequestHandler
- type PostedFile
- type Request
- func (hr *Request) AsDelete() *Request
- func (hr *Request) AsGet() *Request
- func (hr *Request) AsOptions() *Request
- func (hr *Request) AsPatch() *Request
- func (hr *Request) AsPost() *Request
- func (hr *Request) AsPut() *Request
- func (hr *Request) Bytes() ([]byte, error)
- func (hr *Request) BytesWithMeta() ([]byte, *ResponseMeta, error)
- func (hr *Request) Deserialized(deserialize Deserializer) (*ResponseMeta, error)
- func (hr *Request) Equals(other *Request) bool
- func (hr *Request) Execute() error
- func (hr *Request) ExecuteWithMeta() (*ResponseMeta, error)
- func (hr *Request) Hash() uint32
- func (hr Request) Headers() http.Header
- func (hr *Request) JSON(destination interface{}) error
- func (hr *Request) JSONError(errorObject interface{}) (*ResponseMeta, error)
- func (hr *Request) JSONWithErrorHandler(successObject interface{}, errorObject interface{}) (*ResponseMeta, error)
- func (hr *Request) JSONWithMeta(destination interface{}) (*ResponseMeta, error)
- func (hr *Request) Logger() *logger.Logger
- func (hr Request) Meta() *Meta
- func (hr Request) PostBody() []byte
- func (hr *Request) Request() (*http.Request, error)
- func (hr *Request) Response() (*http.Response, error)
- func (hr *Request) String() (string, error)
- func (hr *Request) StringWithMeta() (string, *ResponseMeta, error)
- func (hr *Request) Transport() (*http.Transport, error)
- func (hr *Request) URL() *url.URL
- func (hr *Request) WithBasicAuth(username, password string) *Request
- func (hr *Request) WithClientTLSCert(cert []byte) *Request
- func (hr *Request) WithClientTLSCertPath(certPath string) *Request
- func (hr *Request) WithClientTLSKey(key []byte) *Request
- func (hr *Request) WithClientTLSKeyPath(keyPath string) *Request
- func (hr *Request) WithClientTrace(trace *httptrace.ClientTrace) *Request
- func (hr *Request) WithCombinedPath(components ...string) *Request
- func (hr *Request) WithContentType(contentType string) *Request
- func (hr *Request) WithContext(ctx context.Context) *Request
- func (hr *Request) WithCookie(cookie *http.Cookie) *Request
- func (hr *Request) WithHeader(field string, value string) *Request
- func (hr *Request) WithHost(host string) *Request
- func (hr *Request) WithKeepAliveTimeout(timeout time.Duration) *Request
- func (hr *Request) WithKeepAlives() *Request
- func (hr *Request) WithLabel(label string) *Request
- func (hr *Request) WithLogger(agent *logger.Logger) *Request
- func (hr *Request) WithMockProvider(provider MockedResponseProvider) *Request
- func (hr *Request) WithOnCreateTransport(hook CreateTransportHandler) *Request
- func (hr *Request) WithOnRequest(hook OutgoingRequestHandler) *Request
- func (hr *Request) WithOnResponse(hook ResponseHandler) *Request
- func (hr *Request) WithOnResponseStateful(hook StatefulResponseHandler) *Request
- func (hr *Request) WithPath(path string) *Request
- func (hr *Request) WithPathf(format string, args ...interface{}) *Request
- func (hr *Request) WithPostBody(body []byte) *Request
- func (hr *Request) WithPostBodyAsJSON(object interface{}) *Request
- func (hr *Request) WithPostBodyAsXML(object interface{}) *Request
- func (hr *Request) WithPostBodySerialized(object interface{}, serialize Serializer) *Request
- func (hr *Request) WithPostData(field string, value string) *Request
- func (hr *Request) WithPostDataFromObject(object interface{}) *Request
- func (hr *Request) WithPostedFile(key, fileName string, fileContents io.Reader) *Request
- func (hr *Request) WithQueryString(field string, value string) *Request
- func (hr *Request) WithResponseBuffer(buffer Buffer) *Request
- func (hr *Request) WithScheme(scheme string) *Request
- func (hr *Request) WithState(state interface{}) *Request
- func (hr *Request) WithTLSRootCAPool(certPool *x509.CertPool) *Request
- func (hr *Request) WithTimeout(timeout time.Duration) *Request
- func (hr *Request) WithTransport(transport *http.Transport) *Request
- func (hr *Request) WithURL(urlString string) *Request
- func (hr *Request) WithURLf(format string, args ...interface{}) *Request
- func (hr *Request) WithVerb(verb string) *Request
- func (hr *Request) WithVerifyTLS(shouldVerify bool) *Request
- func (hr *Request) XML(destination interface{}) error
- func (hr *Request) XMLWithErrorHandler(successObject interface{}, errorObject interface{}) (*ResponseMeta, error)
- func (hr *Request) XMLWithMeta(destination interface{}) (*ResponseMeta, error)
- type ResponseEvent
- func (re ResponseEvent) Body() []byte
- func (re ResponseEvent) Flag() logger.Flag
- func (re ResponseEvent) Request() *Meta
- func (re ResponseEvent) Response() *ResponseMeta
- func (re ResponseEvent) Timestamp() time.Time
- func (re ResponseEvent) WriteJSON() logger.JSONObj
- func (re ResponseEvent) WriteText(tf logger.TextFormatter, buf *bytes.Buffer)
- type ResponseHandler
- type ResponseMeta
- type Serializer
- type StatefulResponseHandler
Constants ¶
const ( // Flag is a logger event flag. Flag logger.Flag = "request" // FlagResponse is a logger event flag. FlagResponse logger.Flag = "request.response" )
Variables ¶
This section is empty.
Functions ¶
func ClearMockedResponses ¶
func ClearMockedResponses()
ClearMockedResponses clears any mocked responses that have been set up for the test.
func MockCatchAll ¶
func MockCatchAll(generator MockedResponseGenerator)
MockCatchAll sets a "catch all" mock generator.
func MockResponse ¶
func MockResponse(req *Request, generator MockedResponseGenerator)
MockResponse mocks are response with a given generator.
func MockResponseFromBinary ¶
MockResponseFromBinary mocks a service request response from a set of binary responses.
func MockResponseFromFile ¶
MockResponseFromFile mocks a service request response from a set of file paths.
func MockResponseFromString ¶
MockResponseFromString mocks a service request response from a string responseBody.
func NewRequestListener ¶
NewRequestListener creates a new request listener.
Types ¶
type Buffer ¶
type Buffer interface {
Write([]byte) (int, error)
Len() int64
ReadFrom(io.ReadCloser) (int64, error)
Bytes() []byte
}
Buffer is a type that supplies two methods found on bytes.Buffer.
type CertInfo ¶
type CertInfo struct {
IssuerCommonName string
DNSNames []string
NotAfter time.Time
NotBefore time.Time
}
CertInfo is the information for a certificate.
func NewCertInfo ¶
NewCertInfo returns a new cert info from a response.
type CreateTransportHandler ¶
CreateTransportHandler is a receiver for `OnCreateTransport`.
type Deserializer ¶
Deserializer is a function that does things with the response body.
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event is a logger event for outgoing requests.
type Meta ¶
Meta is a summary of the request meta useful for logging.
type MockedResponse ¶
type MockedResponse struct {
Meta ResponseMeta
Res []byte
Err error
}
MockedResponse is the metadata and response body for a response
func MockedResponseInjector ¶
func MockedResponseInjector(req *Request) *MockedResponse
MockedResponseInjector injects the mocked response into the request response.
func (MockedResponse) Response ¶
func (mr MockedResponse) Response() *http.Response
Response returns a response object for the mock response.
type MockedResponseGenerator ¶
type MockedResponseGenerator func(*Request) MockedResponse
MockedResponseGenerator is a function that returns a mocked response.
type MockedResponseProvider ¶
type MockedResponseProvider func(*Request) *MockedResponse
MockedResponseProvider is a mocked response provider.
type OutgoingRequestHandler ¶
type OutgoingRequestHandler func(req *Meta)
OutgoingRequestHandler is a receiver for `OnRequest`.
type PostedFile ¶
PostedFile represents a file to post with the request.
type Request ¶
type Request struct {
Verb string
Scheme string
Host string
Path string
QueryString url.Values
Cookies []*http.Cookie
Header http.Header
BasicAuthUsername string
BasicAuthPassword string
ContentType string
PostData url.Values
Body []byte
Timeout time.Duration
TLSClientCertPath string
TLSClientKeyPath string
TLSClientCert []byte
TLSClientKey []byte
TLSSkipVerify bool
TLSCAPool *x509.CertPool
KeepAlive bool
KeepAliveTimeout time.Duration
Label string
// contains filtered or unexported fields
}
Request makes http requests.
func (*Request) BytesWithMeta ¶
func (hr *Request) BytesWithMeta() ([]byte, *ResponseMeta, error)
BytesWithMeta fetches the response as bytes with meta.
func (*Request) Deserialized ¶
func (hr *Request) Deserialized(deserialize Deserializer) (*ResponseMeta, error)
Deserialized runs a deserializer with the response.
func (*Request) ExecuteWithMeta ¶
func (hr *Request) ExecuteWithMeta() (*ResponseMeta, error)
ExecuteWithMeta makes the request and returns the meta of the response.
func (*Request) JSONError ¶
func (hr *Request) JSONError(errorObject interface{}) (*ResponseMeta, error)
JSONError unmarshals the response as json to an object if the meta indiciates an error.
func (*Request) JSONWithErrorHandler ¶
func (hr *Request) JSONWithErrorHandler(successObject interface{}, errorObject interface{}) (*ResponseMeta, error)
JSONWithErrorHandler unmarshals the response as json to an object with metadata or an error object depending on the meta.
func (*Request) JSONWithMeta ¶
func (hr *Request) JSONWithMeta(destination interface{}) (*ResponseMeta, error)
JSONWithMeta unmarshals the response as json to an object with metadata.
func (*Request) Response ¶
Response makes the actual request but returns the underlying http.Response object.
func (*Request) StringWithMeta ¶
func (hr *Request) StringWithMeta() (string, *ResponseMeta, error)
StringWithMeta returns the body of the response as a string in addition to the response metadata.
func (*Request) WithBasicAuth ¶
WithBasicAuth sets the basic auth headers for a request.
func (*Request) WithClientTLSCert ¶
WithClientTLSCert sets a tls cert on the transport for the request.
func (*Request) WithClientTLSCertPath ¶
WithClientTLSCertPath sets a tls cert on the transport for the request.
func (*Request) WithClientTLSKey ¶
WithClientTLSKey sets a tls key on the transport for the request.
func (*Request) WithClientTLSKeyPath ¶
WithClientTLSKeyPath sets a tls key on the transport for the request.
func (*Request) WithClientTrace ¶
func (hr *Request) WithClientTrace(trace *httptrace.ClientTrace) *Request
WithClientTrace sets up a trace for the request.
func (*Request) WithCombinedPath ¶
WithCombinedPath sets the path component of the host url by combining the input path segments.
func (*Request) WithContentType ¶
WithContentType sets the `Content-Type` header for the request.
func (*Request) WithContext ¶
WithContext sets a context for the request.
func (*Request) WithCookie ¶
WithCookie sets a cookie for the request.
func (*Request) WithHeader ¶
WithHeader sets a header on the request.
func (*Request) WithKeepAliveTimeout ¶
WithKeepAliveTimeout sets a keep alive timeout for the requests transport.
func (*Request) WithKeepAlives ¶
WithKeepAlives sets if the request should use the `Connection=keep-alive` header or not.
func (*Request) WithLogger ¶
WithLogger enables logging with HTTPRequestLogLevelErrors.
func (*Request) WithMockProvider ¶
func (hr *Request) WithMockProvider(provider MockedResponseProvider) *Request
WithMockProvider mocks a request response.
func (*Request) WithOnCreateTransport ¶
func (hr *Request) WithOnCreateTransport(hook CreateTransportHandler) *Request
WithOnCreateTransport configures an event receiver.
func (*Request) WithOnRequest ¶
func (hr *Request) WithOnRequest(hook OutgoingRequestHandler) *Request
WithOnRequest configures an event receiver.
func (*Request) WithOnResponse ¶
func (hr *Request) WithOnResponse(hook ResponseHandler) *Request
WithOnResponse configures an event receiver.
func (*Request) WithOnResponseStateful ¶
func (hr *Request) WithOnResponseStateful(hook StatefulResponseHandler) *Request
WithOnResponseStateful configures an event receiver that includes the request state.
func (*Request) WithPathf ¶
WithPathf sets the path component of the host url by the format and arguments.
func (*Request) WithPostBody ¶
WithPostBody sets the post body directly.
func (*Request) WithPostBodyAsJSON ¶
WithPostBodyAsJSON sets the post body raw to be the json representation of an object.
func (*Request) WithPostBodyAsXML ¶
WithPostBodyAsXML sets the post body raw to be the xml representation of an object.
func (*Request) WithPostBodySerialized ¶
func (hr *Request) WithPostBodySerialized(object interface{}, serialize Serializer) *Request
WithPostBodySerialized sets the post body with the results of the given serializer.
func (*Request) WithPostData ¶
WithPostData sets a post data value for the request.
func (*Request) WithPostDataFromObject ¶
WithPostDataFromObject sets the post data for a request as json from a given object. Remarks; this differs from `WithJSONBody` in that it sets individual post form fields for each member of the object.
func (*Request) WithPostedFile ¶
WithPostedFile adds a posted file to the multipart form elements of the request.
func (*Request) WithQueryString ¶
WithQueryString sets a query string value for the host url of the request.
func (*Request) WithResponseBuffer ¶
WithResponseBuffer sets the response buffer for the request (if you want to re-use one). An example is if you're constantly pinging an endpoint with a similarly sized response, You can just re-use a buffer for reading the response.
func (*Request) WithScheme ¶
WithScheme sets the scheme, or protocol, of the request.
func (*Request) WithTLSRootCAPool ¶
WithTLSRootCAPool sets the root TLS ca pool for the request.
func (*Request) WithTimeout ¶
WithTimeout sets a timeout for the request. Remarks: This timeout is enforced on client connect, not on request read + response.
func (*Request) WithTransport ¶
WithTransport sets a transport for the request.
func (*Request) WithVerifyTLS ¶
WithVerifyTLS skips the bad certificate checking on TLS requests.
func (*Request) XMLWithErrorHandler ¶
func (hr *Request) XMLWithErrorHandler(successObject interface{}, errorObject interface{}) (*ResponseMeta, error)
XMLWithErrorHandler unmarshals the response as xml to an object with metadata or an error object depending on the meta.
func (*Request) XMLWithMeta ¶
func (hr *Request) XMLWithMeta(destination interface{}) (*ResponseMeta, error)
XMLWithMeta unmarshals the response as xml to an object with metadata.
type ResponseEvent ¶
type ResponseEvent struct {
// contains filtered or unexported fields
}
ResponseEvent is a response to outgoing requests.
func (ResponseEvent) Body ¶
func (re ResponseEvent) Body() []byte
Body returns the outgoing request body.
func (ResponseEvent) Request ¶
func (re ResponseEvent) Request() *Meta
Request returns the request meta.
func (ResponseEvent) Response ¶
func (re ResponseEvent) Response() *ResponseMeta
Response returns the response meta.
func (ResponseEvent) Timestamp ¶
func (re ResponseEvent) Timestamp() time.Time
Timestamp returns the event timestamp.
func (ResponseEvent) WriteJSON ¶
func (re ResponseEvent) WriteJSON() logger.JSONObj
WriteJSON implements logger.JSONWritable.
func (ResponseEvent) WriteText ¶
func (re ResponseEvent) WriteText(tf logger.TextFormatter, buf *bytes.Buffer)
WriteText writes the event to a text writer.
type ResponseHandler ¶
type ResponseHandler func(req *Meta, meta *ResponseMeta, content []byte)
ResponseHandler is a receiver for `OnResponse`.
type ResponseMeta ¶
type ResponseMeta struct {
Cert *CertInfo
CompleteTime time.Time
StatusCode int
ContentLength int64
ContentEncoding string
ContentType string
Headers http.Header
}
ResponseMeta is just the meta information for an http response.
func NewResponseMeta ¶
func NewResponseMeta(res *http.Response) *ResponseMeta
NewResponseMeta returns a new meta object for a response.
type Serializer ¶
Serializer is a function that turns an object into raw data.
type StatefulResponseHandler ¶
type StatefulResponseHandler func(req *Meta, res *ResponseMeta, content []byte, state interface{})
StatefulResponseHandler is a receiver for `OnResponse` that includes a state object.