Documentation ¶
Overview ¶
The package hit provides utilities helpful for testing apis.
Index ¶
- Constants
- type Body
- type CSVType
- type ContentError
- type Error
- type ErrorList
- type FormBody
- type Header
- type HeaderError
- type Hit
- type JSONBody
- type ParamSetter
- type Params
- type Query
- type QueryEncoder
- type QueryEncoderBody
- type Request
- type RequestError
- type Response
- type Status
- type StatusError
- type Test
- type TestList
- type TestRunner
- type XMLBody
Constants ¶
const ( // ANSI color values used to colorize terminal output for better readability. RedColor = "\033[91m" YellowColor = "\033[93m" PurpleColor = "\033[95m" CyanColor = "\033[96m" GreenColor = "\033[92m" StopColor = "\033[0m" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Body ¶
type Body interface { // Reader returns an io.Reader that can be used to read the contents of the body. Reader() (io.Reader, error) // ContentType returns the media type (MIME) that describes the data contained in the body. ContentType() string // CompareContent returns the result of the comparison between the // Body's contents and the contents of the given io.Reader. The level // of strictness of the comparison depends on the implementation. If // the contents are equivalent the returned error will be nil, otherwise // the error will describe the negative result of the comparison. CompareContent(io.Reader) error // Value returns the underlying value of the Body interface. Value() interface{} }
The Body type represents the contents of an HTTP request or response body and provides a number of methods that can be useful during testing.
type CSVType ¶
type CSVType [][]string
CSVType implements the Body interface.
func (CSVType) CompareContent ¶
CompareContent returns the result of the comparison between the CSVType value and the given io.Reader. CompareContent uses encoding/csv's Decoder.Decode to decode the Reader's contents.
func (CSVType) ContentType ¶
ContentType returns the media type (MIME) of the CSVType which in this case will always be text/csv.
type ContentError ¶
type ContentError struct {
// contains filtered or unexported fields
}
ContentError describes the negative result of a content comparison.
func (*ContentError) Error ¶
func (e *ContentError) Error() string
Error implements the error interface.
type FormBody ¶
type FormBody struct {
// contains filtered or unexported fields
}
FormBody implements the Body interface.
func (FormBody) CompareContent ¶
CompareContent returns the result of the comparison between the FormBody's underlying value and the given io.Reader. CompareContent uses github.com/frk/form's Decoder.Decode to decode the Reader's contents into a newly allocated value of the same type as the FormBody's underlying value, see the documentation on github.com/frk/form's Decoder.Decode for more details.
CompareContent does a "loose" comparison where it checks only whether the underlying value can be recreated from the given reader, it does not care about any additional data that the reader might contain.
func (FormBody) ContentType ¶
ContentType returns the media type (MIME) of the FormBody which in this case will always be "application/x-www-form-urlencoded".
func (FormBody) QueryEncode ¶
QueryEncode implements the QueryEncoder interface.
type Header ¶
A Header represents the key-value pairs in an HTTP header.
type HeaderError ¶
type HeaderError struct {
// contains filtered or unexported fields
}
HeaderError describes the comparison state of a single header.
func (HeaderError) Error ¶
func (e HeaderError) Error() string
Error implements the error interface.
type Hit ¶
type Hit struct { Verb string // the HTTP verb/method of a request Skip bool // if true, flags the Hit as to be skipped by the test Label string // optional short description of the Hit Desc string // optional long description of the Hit (useful for hit/doc) // A list of Request values that are used to construct individual HTTP // requests with their specific data (headers, body, etc.) to be executed // against the endpoint under test. Reqs []*Request // DocFlags can optionally be set to one of the doc.FlagXxx values // to control what the hit/doc package documentation. DocFlags int // contains filtered or unexported fields }
Hit describes the HTTP requests that should be executed to test an endpoint.
type JSONBody ¶
type JSONBody struct {
// contains filtered or unexported fields
}
JSONBody implements the Body interface.
func (JSONBody) CompareContent ¶
CompareContent returns the result of the comparison between the JSONBody's underlying value and the given io.Reader. CompareContent uses encoding/json's Decoder.Decode to decode the Reader's contents into a newly allocated value of the same type as the JSONBody's underlying value, see the documentation on encoding/json's Decoder.Decode for more details.
CompareContent does a "loose" comparison where it checks only whether the underlying value can be recreated from the given reader, it does not care about any additional data that the reader might contain.
func (JSONBody) ContentType ¶
ContentType returns the media type (MIME) of the JSONBody which in this case will always be "application/json".
type ParamSetter ¶
The ParamSetter substitutes the placeholders of an endpoint pattern with parameter values.
SetParams should return a copy of the given pattern with all of its placeholders replaced with actual parameter values. How the placeholders should be demarcated in the pattern depends on the implementation of the interface.
type Params ¶
type Params map[string]interface{}
Params is a ParamSetter that substitues an endpoint pattern's placeholders with its mapped values. The Params' keys represent the placeholders while the values are the actual parameters to be used to substitue those placeholders.
func (Params) SetParams ¶
SetParams returns a copy of the given pattern replacing all of its placeholders with the actual parameter values contained in Params. The placeholders, used as keys to get the corresponding parameter values, are expected to be demarcated with curly braces; e.g.
pattern := "/users/{user_id}" params := Params{"user_id": 123} path := params.SetParams(pattern) fmt.Println(path) // outputs "/users/123"
type Query ¶
Query is a QueryEncoder that returns its' contents encoded into "URL encoded" form.
func (Query) QueryEncode ¶
QueryEncode encodes the Query's underlying values into "URL encoded" form. QueryEncode uses net/url's Values.Encode to encode the values, see the net/url documentation for more info.
type QueryEncoder ¶
type QueryEncoder interface {
QueryEncode() string
}
The QueryEncoder returns a string of query parameters in the "URL encoded" form.
type QueryEncoderBody ¶
type QueryEncoderBody interface { QueryEncoder Body }
The QueryEncoderBody is an interface that groups the QueryEncoder and Body interfaces.
func Form ¶
func Form(v interface{}) QueryEncoderBody
Form wraps the given value v and returns a Body that represents the value as form encoded data. At the moment the resulting Body uses github.com/frk/form to encode and decode the given value, see the package's documentation for more details.
type Request ¶
type Request struct { Skip bool // if true, flags the Request as to be skipped by the test Desc string // optional description specific to this Request Body Body // the request body to be sent Header Header // the HTTP headers to be sent with the request Params ParamSetter // the path parameters to be substituted in an endpoint pattern Query QueryEncoder // the URL query parameters to be appended to an endpoint's path Want Response // the expected response }
Request describes the data to be sent in a single HTTP request as well as the expected corresponding Response.
type RequestError ¶
type RequestError struct {
// contains filtered or unexported fields
}
func (*RequestError) Error ¶
func (re *RequestError) Error() string
type Response ¶
type Response struct { Status int // the expected HTTP status code Header Header // the expected HTTP response headers Body Body // the expected response body }
Response is used to describe the expected HTTP response to a request.
type StatusError ¶
type StatusError struct {
// contains filtered or unexported fields
}
func (*StatusError) Error ¶
func (se *StatusError) Error() string
type Test ¶
type Test struct { // Ept is the API endpoint path to be tested. Ept can also be a "pattern" // of the endpoint path that, together with a value that implements the // ParamSetter interface, will be used to construct the actual endpoint path. Ept string // A List of Hits to be executed against the endpoint. Hits []*Hit }
Test describes how an API endpoint should be tested.
type TestRunner ¶
type TestRunner struct {
// contains filtered or unexported fields
}
func NewTestRunner ¶
func NewTestRunner(host string, cl *http.Client) *TestRunner
func (*TestRunner) LogReport ¶
func (tr *TestRunner) LogReport()
LogReport logs a summary of the test to stdout. It is intended to be called at "teardown".
type XMLBody ¶
type XMLBody struct {
// contains filtered or unexported fields
}
XMLBody implements the Body interface.
func (XMLBody) CompareContent ¶
CompareContent returns the result of the comparison between the XMLBody's underlying value and the given io.Reader. CompareContent uses encoding/xml's Decoder.Decode to decode the Reader's contents into a newly allocated value of the same type as the XMLBody's underlying value, see the documentation on encoding/xml's Decoder.Decode for more details.
CompareContent does a "loose" comparison where it checks only whether the underlying value can be recreated from the given reader, it does not care about any additional data that the reader might contain.
func (XMLBody) ContentType ¶
ContentType returns the media type (MIME) of the XMLBody which in this case will always be "application/xml".