Documentation
¶
Overview ¶
Package rehapt allows to build REST HTTP API test cases by describing the request to execute and the expected response body. The library takes care of comparing the expected and actual response and reports any errors. It has been designed to work very well for JSON APIs
Example:
func TestAPISimple(t *testing.T) { r := NewRehapt(t, yourHttpServerMux) // Each testcase consist of a description of the request to execute // and a description of the expected response // By default the response description is exhaustive. // If an actual response field is not listed here, an error will be triggered // of course if an expected field described here is not present in response, an error will be triggered too. r.TestAssert(TestCase{ Request: TestRequest{ Method: "GET", Path: "/api/user/1", }, Response: TestResponse{ Code: http.StatusOK, Body: M{ "id": "1", "name": "John", "age": 51, "pets": S{ // S for slice, M for map. Easy right ? M{ "id": "2", "name": "Pepper the cat", "type": "cat", }, }, "weddingdate": "2019-06-22T16:00:00.000Z", }, }, }) }
See https://github.com/thib-ack/rehapt/tree/master/examples for more examples
Index ¶
- func RawMarshaler(v interface{}) ([]byte, error)
- func RawUnmarshaler(data []byte, out interface{}) error
- type CompareFn
- func And(cmp ...interface{}) CompareFn
- func Any() CompareFn
- func LoadVar(name string) CompareFn
- func Not(value interface{}) CompareFn
- func NumberDelta(value float64, delta float64) CompareFn
- func Or(cmp ...interface{}) CompareFn
- func Regexp(regex string) CompareFn
- func RegexpVars(regex string, vars map[int]string) CompareFn
- func StoreVar(name string) CompareFn
- func TimeDelta(t time.Time, delta time.Duration) CompareFn
- func TimeDeltaLayout(t time.Time, delta time.Duration, layout string) CompareFn
- type ErrorHandler
- type H
- type M
- type MarshalFn
- type PartialM
- type Rehapt
- func (r *Rehapt) AddDefaultHeader(name string, value string)
- func (r *Rehapt) GetDefaultHeader(name string) string
- func (r *Rehapt) GetDefaultHeaders() http.Header
- func (r *Rehapt) GetVariable(name string) interface{}
- func (r *Rehapt) GetVariableString(name string) string
- func (r *Rehapt) ReplaceVars(str string) string
- func (r *Rehapt) SetDefaultHeader(name string, value string)
- func (r *Rehapt) SetDefaultHeaders(headers http.Header)
- func (r *Rehapt) SetDefaultTimeDeltaFormat(format string)
- func (r *Rehapt) SetErrorHandler(errorHandler ErrorHandler)
- func (r *Rehapt) SetHttpHandler(handler http.Handler)
- func (r *Rehapt) SetLoadShortcutBounds(prefix string, suffix string) error
- func (r *Rehapt) SetLoadShortcutFloatPrecision(precision int)
- func (r *Rehapt) SetMarshaler(marshaler func(v interface{}) ([]byte, error))
- func (r *Rehapt) SetStoreShortcutBounds(prefix string, suffix string) error
- func (r *Rehapt) SetUnmarshaler(unmarshaler func(data []byte, v interface{}) error)
- func (r *Rehapt) SetVariable(name string, value interface{}) error
- func (r *Rehapt) Test(testcase TestCase) error
- func (r *Rehapt) TestAssert(testcase TestCase)
- type ReplaceFn
- type S
- type TestCase
- type TestRequest
- type TestResponse
- type UnmarshalFn
- type UnsortedS
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RawMarshaler ¶ added in v0.4.0
func RawUnmarshaler ¶ added in v0.4.0
Types ¶
type CompareFn ¶ added in v0.4.0
func Not ¶ added in v0.4.0
func Not(value interface{}) CompareFn
Not means we don't expect the given value it works as a boolean 'not' operator on the comparison
func NumberDelta ¶
NumberDelta allow to compare a number value with a given +/- delta. Delta is compared to math.Abs(expected - actual) which explain why if your expected value is 10 with a delta of 3, actual value will match from 7 to 13.
func Regexp ¶
Regexp allow to do advanced regexp expectation. If the regexp is invalid, an error is reported. If the actual value to compare with is not a string, an error is reported. If the actual value does not match the regexp, an error is reported
func RegexpVars ¶
RegexpVars is a mix between Regexp and StoreVar. It checks if the actual value matches the regexp. but all the groups defined in the regexp can be extracted to variables for later reuse The Vars hold the mapping groupid: varname. For example with Regexp: `^Hello (.*) !$` and Vars: map[int]string{0: "all", 1: "name"} then if the actual value is "Hello john !", it will match and 2 vars will be stored:
"all" = "Hello john !" (group 0 is the full match) "name" = "John"
func StoreVar ¶
StoreVar allow to store the actual value in a variable instead of checking its content
type ErrorHandler ¶
type ErrorHandler interface {
Errorf(format string, args ...interface{})
}
ErrorHandler is the interface used to report errors when found by TestAssert(). Note that *testing.T implements this interface
type M ¶
type M map[string]interface{}
M declare a Map. It is used to quickly build a map within your expected response body
type PartialM ¶
type PartialM map[string]interface{}
PartialM declare a Partial Map. It is used to expect some fields but ignore the un-listed ones instead of reporting missing
type Rehapt ¶
type Rehapt struct {
// contains filtered or unexported fields
}
Rehapt - REST HTTP API Test
This is the main structure of the library. You can build it using the NewRehapt() function.
func NewRehapt ¶
func NewRehapt(errorHandler ErrorHandler, handler http.Handler) *Rehapt
NewRehapt build a new Rehapt instance from the given http.Handler. `handler` must be your server global handler. For example, it could be a simple http.NewServeMux() or an complex third-party library mux. `errorHandler` can be the *testing.T parameter of your test, if value is nil, the errors are printed on stdout
func (*Rehapt) AddDefaultHeader ¶ added in v0.2.0
AddDefaultHeader allow to add a default request header. This header will be added to all requests, however each TestCase can override its value
func (*Rehapt) GetDefaultHeader ¶
GetDefaultHeader returns the default request header value from its name. Default headers are added automatically to all requests
func (*Rehapt) GetDefaultHeaders ¶ added in v0.2.0
GetDefaultHeaders allow to get all default request headers. These headers will be added to all requests, however each TestCase can override their values
func (*Rehapt) GetVariable ¶
GetVariable allow to retrieve a variable value from its name. nil is returned if variable is not found
func (*Rehapt) GetVariableString ¶
GetVariableString allow to retrieve a variable value as a string from its name empty string is returned if variable is not found
func (*Rehapt) ReplaceVars ¶ added in v0.4.0
func (*Rehapt) SetDefaultHeader ¶
SetDefaultHeader allow to set a default request header. This header will be added to all requests, however each TestCase can override its value
func (*Rehapt) SetDefaultHeaders ¶ added in v0.2.0
SetDefaultHeaders allow to set all default request headers. These headers will be added to all requests, however each TestCase can override their values
func (*Rehapt) SetDefaultTimeDeltaFormat ¶
SetDefaultTimeDeltaFormat allow to change the default time format It is used by TimeDelta, to parse the actual string value as a time.Time Default is set to time.RFC3339 which is ok for JSON. This default format can be changed manually for each TimeDelta
func (*Rehapt) SetErrorHandler ¶
func (r *Rehapt) SetErrorHandler(errorHandler ErrorHandler)
SetErrorHandler allow to change the object handling errors which is called when TestAssert() encounter an error. Setting ErrorHandler to nil will simply print the errors on stdout
func (*Rehapt) SetHttpHandler ¶
SetHttpHandler allow to change the http.Handler used to run requests
func (*Rehapt) SetLoadShortcutBounds ¶
SetLoadShortcutBounds modify the strings used as prefix and suffix to identify a shortcut version of the load variable operation. The default prefix and suffix is "_" which makes the default shortcut form like "_myvar_".
func (*Rehapt) SetLoadShortcutFloatPrecision ¶
SetLoadShortcutFloatPrecision change the precision of float formatting when used with a load shortcut. For example "value is _myvar_" can be replaced by "value is 10.50" or "value is 10.500000".
func (*Rehapt) SetMarshaler ¶
SetMarshaler allow to change the marshal function used to encode requests body. The default marshaler is json.Marshal
func (*Rehapt) SetStoreShortcutBounds ¶
SetStoreShortcutBounds modify the strings used as prefix and suffix to identify a shortcut version of the store variable operation. The default prefix and suffix is "$" which makes the default shortcut form like "$myvar$".
func (*Rehapt) SetUnmarshaler ¶
SetUnmarshaler allow to change the unmarshal function used to decode requests response. The default unmarshaler is json.Unmarshal
func (*Rehapt) SetVariable ¶
SetVariable allow to define manually a variable. Variable names are strings, however values can be any type
func (*Rehapt) Test ¶
Test is the main function of the library it executes a given TestCase, i.e. do the request and check if the actual response is matching the expected response
func (*Rehapt) TestAssert ¶
TestAssert works exactly like Test except it reports the error if not nil using the ErrorHandler Errorf() function
type ReplaceFn ¶ added in v0.4.0
func NoReplacement ¶ added in v0.4.0
type S ¶
type S []interface{}
S declare a Slice. It is used to quickly build a slice within your expected response body
type TestCase ¶
type TestCase struct { Request TestRequest Response TestResponse }
TestCase is the base type supported to describe a test. It is the object taken as parameters in Test() and TestAssert()
type TestRequest ¶
type TestRequest struct { Method string Path interface{} Headers H Body interface{} BodyMarshaler MarshalFn }
TestRequest describe the request to be executed
type TestResponse ¶
type TestResponse struct { Headers interface{} Code interface{} Body interface{} BodyUnmarshaler UnmarshalFn }
TestResponse describe the response expected