hit

package module
v0.0.0-...-92f40f7 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2018 License: BSD-3-Clause Imports: 17 Imported by: 0

README

wip

TODO:

  • add MultipartForm Body implementation
  • add a proper (decode/encode by type) CSV Body implementation

Documentation

Overview

The package hit provides utilities helpful for testing apis.

Index

Constants

View Source
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.

func JSON

func JSON(v interface{}) Body

JSON wraps the given value v and returns a Body that represents the value as json encoded data. The resulting Body uses encoding/json to encode and decode the given value, see the encoding/json documentation for more details.

func XML

func XML(v interface{}) Body

XML wraps the given value v and returns a Body that represents the value as xml encoded data. The resulting Body uses encoding/xml to encode and decode the given value, see the encoding/xml documentation for more details.

type CSVType

type CSVType [][]string

CSVType implements the Body interface.

func (CSVType) CompareContent

func (ct CSVType) CompareContent(r io.Reader) error

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

func (ct CSVType) ContentType() string

ContentType returns the media type (MIME) of the CSVType which in this case will always be text/csv.

func (CSVType) Reader

func (ct CSVType) Reader() (io.Reader, error)

Reader returns an io.Reader that can be used to read the CSVType's value as csv encoded data.

func (CSVType) String

func (ct CSVType) String() string

for debugging..

func (CSVType) Value

func (ct CSVType) Value() interface{}

Value returns the CSVType value as an interface{}.

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 Error

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

func (*Error) Error

func (e *Error) Error() (s string)

type ErrorList

type ErrorList []error

func (ErrorList) Error

func (el ErrorList) Error() (s string)

type FormBody

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

FormBody implements the Body interface.

func (FormBody) CompareContent

func (b FormBody) CompareContent(r io.Reader) error

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

func (b FormBody) ContentType() string

ContentType returns the media type (MIME) of the FormBody which in this case will always be "application/x-www-form-urlencoded".

func (FormBody) QueryEncode

func (b FormBody) QueryEncode() string

QueryEncode implements the QueryEncoder interface.

func (FormBody) Reader

func (b FormBody) Reader() (io.Reader, error)

Reader returns an io.Reader that can be used to read the FormBody's underlying value as form encoded data. Reader uses github.com/frk/form's Marshal func to encode the underlying value, see the documentation on that package's Marshal for more details.

func (FormBody) String

func (b FormBody) String() string

for debugging...

func (FormBody) Value

func (b FormBody) Value() interface{}

Value returns the underlying value of the FormBody.

type Header map[string][]string

A Header represents the key-value pairs in an HTTP header.

func (Header) AddTo

func (h Header) AddTo(r *http.Request)

AddTo sets all of the receiver's values to the specified http.Request's header.

func (Header) Compare

func (h Header) Compare(hh http.Header) error

Compare checks if all of the receiver's key-value pairs are present in the given http.Header returning an error if not.

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.

func (*Hit) Status

func (h *Hit) Status() Status

Status reports the test status of the Hit. It is intended to be used by the hit/doc package.

type JSONBody

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

JSONBody implements the Body interface.

func (JSONBody) CompareContent

func (b JSONBody) CompareContent(r io.Reader) error

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

func (b JSONBody) ContentType() string

ContentType returns the media type (MIME) of the JSONBody which in this case will always be "application/json".

func (JSONBody) Reader

func (b JSONBody) Reader() (io.Reader, error)

Reader returns an io.Reader that can be used to read the JSONBody's underlying value as json encoded data. Reader uses encoding/json's Marshal func to encode the underlying value, see the documentation on encoding/json's Marshal for more details.

func (JSONBody) String

func (b JSONBody) String() string

for debugging

func (JSONBody) Value

func (b JSONBody) Value() interface{}

Value returns the underlying value of the JSONBody.

type ParamSetter

type ParamSetter interface {
	SetParams(pattern string) string
}

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

func (pp Params) SetParams(pattern string) (path string)

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

type Query url.Values

Query is a QueryEncoder that returns its' contents encoded into "URL encoded" form.

func (Query) QueryEncode

func (q Query) QueryEncode() string

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 Status

type Status int

Status reports the test status of a Hit value.

const (
	StatusUntested Status = iota
	StatusSkipped
	StatusFailed
	StatusPassed
)

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.

func SkipAll

func SkipAll(tt []*Test) []*Test

type TestList

type TestList []*Test

func NewTestList

func NewTestList(tt ...*Test) TestList

func (*TestList) Add

func (ls *TestList) Add(tt ...*Test)

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".

func (*TestRunner) Run

func (tr *TestRunner) Run(t *testing.T, tests []*Test)

type XMLBody

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

XMLBody implements the Body interface.

func (XMLBody) CompareContent

func (b XMLBody) CompareContent(r io.Reader) error

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

func (b XMLBody) ContentType() string

ContentType returns the media type (MIME) of the XMLBody which in this case will always be "application/xml".

func (XMLBody) Reader

func (b XMLBody) Reader() (io.Reader, error)

Reader returns an io.Reader that can be used to read the XMLBody's underlying value as xml encoded data. Reader uses encoding/xml's Marshal func to encode the underlying value, see the documentation on encoding/xml's Marshal for more details.

func (XMLBody) String

func (b XMLBody) String() string

for debugging

func (XMLBody) Value

func (b XMLBody) Value() interface{}

Value returns the underlying value of the XMLBody.

Directories

Path Synopsis
doc

Jump to

Keyboard shortcuts

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