Documentation ¶
Overview ¶
Package gdutils provides State object with utility methods that may be used for behavioral testing of HTTP API.
Main package struct is State that represents one testing scenario state. It can be initialized by 2 ways.
First, returns *State with default http.Client, DefaultCache and provided debug mode:
func NewDefaultState(isDebug bool) *State
Second, returns *State with provided http.Client, Cache and debug mode:
func NewState(httpClient *http.Client, cache Cache, isDebug bool) *State
Struct State contains of ¶
* cache with arbitrary values - State.Cache, * info whether scenario is in debug mode - State.IsDebug
Useful State methods are func (s *State) GetLastResponse() (*http.Response, error) func (s *State) GetLastResponseBody() []byte func (s *State) GetLastResponseHeaders() http.Header
Testing HTTP API usually consist the following aspects:
* Data generation:
func (s *State) IGenerateARandomIntInTheRangeToAndSaveItAs(from, to int, cacheKey string) error func (s *State) IGenerateARandomFloatInTheRangeToAndSaveItAs(from, to int, cacheKey string) error func (s *State) IGenerateARandomStringOfLengthWithoutUnicodeCharactersAndSaveItAs(strLength int, cacheKey string) error func (s *State) IGenerateARandomStringOfLengthWithUnicodeCharactersAndSaveItAs(strLength int, cacheKey string) error
* Sending HTTP(s) requests:
func (s *State) ISendRequestToWithBodyAndHeaders(method, urlTemplate string, bodyTemplate *godog.DocString) error
* Assertions:
func (s *State) TheResponseStatusCodeShouldBe(code int) error func (s *State) TheResponseBodyShouldHaveType(dataType string) error func (s *State) TheJSONResponseShouldHaveNode(expr string) error func (s *State) TheJSONNodeShouldNotBe(expr string, goType string) error func (s *State) TheJSONNodeShouldBe(expr string, goType string) error func (s *State) TheJSONResponseShouldHaveNodes(nodeExprs string) error func (s *State) TheJSONNodeShouldBeSliceOfLength(expr string, length int) error func (s *State) TheJSONNodeShouldBeOfValue(expr, dataType, dataValue string) error func (s *State) TheResponseShouldHaveHeader(name string) error func (s *State) TheResponseShouldHaveHeaderOfValue(name, value string) error
* Preserving JSON nodes:
func (s *State) ISaveFromTheLastResponseJSONNodeAs(expr, cacheKey string) error
* Temporary stopping scenario execution:
func (s *State) IWait(timeInterval string) error
* Debugging:
func (s *State) IPrintLastResponseBody() error func (s *State) IStartDebugMode() error func (s *State) IStopDebugMode() error
Index ¶
- Variables
- type Cache
- type DefaultCache
- type State
- func (s *State) GetLastResponse() (*http.Response, error)
- func (s *State) GetLastResponseBody() []byte
- func (s *State) GetLastResponseHeaders() http.Header
- func (s *State) IGenerateARandomFloatInTheRangeToAndSaveItAs(from, to int, cacheKey string) error
- func (s *State) IGenerateARandomIntInTheRangeToAndSaveItAs(from, to int, cacheKey string) error
- func (s *State) IGenerateARandomStringOfLengthWithUnicodeCharactersAndSaveItAs(strLength int, cacheKey string) error
- func (s *State) IGenerateARandomStringOfLengthWithoutUnicodeCharactersAndSaveItAs(strLength int, cacheKey string) error
- func (s *State) IPrintLastResponseBody() error
- func (s *State) ISaveFromTheLastResponseJSONNodeAs(expr, cacheKey string) error
- func (s *State) ISendRequestToWithBodyAndHeaders(method, urlTemplate string, bodyTemplate *godog.DocString) error
- func (s *State) IStartDebugMode() error
- func (s *State) IStopDebugMode() error
- func (s *State) IWait(timeInterval string) error
- func (s *State) ResetState(isDebug bool)
- func (s *State) TheJSONNodeShouldBe(expr string, goType string) error
- func (s *State) TheJSONNodeShouldBeOfValue(expr, dataType, dataValue string) error
- func (s *State) TheJSONNodeShouldBeSliceOfLength(expr string, length int) error
- func (s *State) TheJSONNodeShouldNotBe(expr string, goType string) error
- func (s *State) TheJSONResponseShouldHaveNode(expr string) error
- func (s *State) TheJSONResponseShouldHaveNodes(nodeExprs string) error
- func (s *State) TheResponseBodyShouldHaveType(dataType string) error
- func (s *State) TheResponseShouldHaveHeader(name string) error
- func (s *State) TheResponseShouldHaveHeaderOfValue(name, value string) error
- func (s *State) TheResponseStatusCodeShouldBe(code int) error
Constants ¶
This section is empty.
Variables ¶
var ErrJson = errors.New("invalid JSON format")
ErrJson tells that value has invalid JSON format.
var ErrJsonNode = errors.New("invalid JSON node")
ErrJsonNode tells that there is some kind of error with json node.
var ErrPreservedData = errors.New("preserved data error")
ErrPreservedData tells indices that there is some kind of error with scenario preserved data.
var ErrResponseCode = errors.New("invalid response code")
ErrResponseCode tells that response had invalid response code.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { //Save preserve provided value under given key Save(key string, value interface{}) //GetSaved retrieve value under given key GetSaved(key string) (interface{}, error) //Reset turns DefaultCache into init state Reset() //All returns all DefaultCache entries All() map[string]interface{} }
Cache is entity that has ability to store/retrieve arbitrary values
type DefaultCache ¶
type DefaultCache struct {
// contains filtered or unexported fields
}
DefaultCache is struct that has ability to store and retrieve arbitrary values
func NewDefaultCache ¶
func NewDefaultCache() *DefaultCache
func (*DefaultCache) All ¶
func (c *DefaultCache) All() map[string]interface{}
All returns all current DefaultCache data
func (*DefaultCache) GetSaved ¶
func (c *DefaultCache) GetSaved(key string) (interface{}, error)
GetSaved returns preserved value from DefaultCache if present, error otherwise.
func (*DefaultCache) Reset ¶
func (c *DefaultCache) Reset()
Reset turns DefaultCache into init state
func (*DefaultCache) Save ¶
func (c *DefaultCache) Save(key string, value interface{})
Save preserve value under given key in DefaultCache.
type State ¶
type State struct { //IsDebug determine whether scenario is in debug mode IsDebug bool //Cache is storage for scenario data. //It may hold any value from scenario steps or globally available environment variables Cache Cache // contains filtered or unexported fields }
State struct represents data shared across one scenario.
func NewDefaultState ¶
NewDefaultState returns *State with default http.Client, DefaultCache and provided debug mode
func (*State) GetLastResponse ¶
GetLastResponse retrieve last response from cache
func (*State) GetLastResponseBody ¶
GetLastResponseBody returns last HTTP response body as slice of bytes method is safe for multiple use
func (*State) GetLastResponseHeaders ¶
GetLastResponseHeaders returns last HTTP response headers
func (*State) IGenerateARandomFloatInTheRangeToAndSaveItAs ¶
IGenerateARandomFloatInTheRangeToAndSaveItAs generates random float from provided range and preserve it under given DefaultCache key
func (*State) IGenerateARandomIntInTheRangeToAndSaveItAs ¶
IGenerateARandomIntInTheRangeToAndSaveItAs generates random integer from provided range and preserve it under given DefaultCache key
func (*State) IGenerateARandomStringOfLengthWithUnicodeCharactersAndSaveItAs ¶
func (s *State) IGenerateARandomStringOfLengthWithUnicodeCharactersAndSaveItAs(strLength int, cacheKey string) error
IGenerateARandomStringOfLengthWithUnicodeCharactersAndSaveItAs generates random string of given length with unicode characters and preserve it under given DefaultCache key
func (*State) IGenerateARandomStringOfLengthWithoutUnicodeCharactersAndSaveItAs ¶
func (s *State) IGenerateARandomStringOfLengthWithoutUnicodeCharactersAndSaveItAs(strLength int, cacheKey string) error
IGenerateARandomStringOfLengthWithoutUnicodeCharactersAndSaveItAs generates random string of given length without unicode characters and preserve it under given DefaultCache key
func (*State) IPrintLastResponseBody ¶
IPrintLastResponseBody prints last response from request
func (*State) ISaveFromTheLastResponseJSONNodeAs ¶
ISaveFromTheLastResponseJSONNodeAs saves from last response body JSON node under given DefaultCache key. expr should be valid according to qjson library
func (*State) ISendRequestToWithBodyAndHeaders ¶
func (s *State) ISendRequestToWithBodyAndHeaders(method, urlTemplate string, bodyTemplate *godog.DocString) error
ISendRequestToWithBodyAndHeaders sends HTTP request with provided body and headers. Argument method indices HTTP request method for example: "POST", "GET" etc. Argument urlTemplate should be full url path. May include template values. Argument bodyTemplate should be slice of bytes marshallable on bodyHeaders struct
func (*State) IStartDebugMode ¶
IStartDebugMode starts debugging mode
func (*State) IStopDebugMode ¶
IStopDebugMode stops debugging mode
func (*State) IWait ¶
IWait waits for given timeInterval amount of time timeInterval should be string valid for time.ParseDuration func
func (*State) ResetState ¶
ResetState resets State struct instance to default values.
func (*State) TheJSONNodeShouldBe ¶
TheJSONNodeShouldBe checks whether JSON node from last response body is of provided type goType may be one of: nil, string, int, float, bool, map, slice expr should be valid according to qjson library
func (*State) TheJSONNodeShouldBeOfValue ¶
TheJSONNodeShouldBeOfValue compares json node value from expression to expected by user dataValue of given by user dataType available data types are listed in switch section in each case directive expr should be valid according to qjson library
func (*State) TheJSONNodeShouldBeSliceOfLength ¶
TheJSONNodeShouldBeSliceOfLength checks whether given key is slice and has given length expr should be valid according to qjson library
func (*State) TheJSONNodeShouldNotBe ¶
TheJSONNodeShouldNotBe checks whether JSON node from last response body is not of provided type goType may be one of: nil, string, int, float, bool, map, slice expr should be valid according to qjson library
func (*State) TheJSONResponseShouldHaveNode ¶
TheJSONResponseShouldHaveNode checks whether last response body contains given key
func (*State) TheJSONResponseShouldHaveNodes ¶
TheJSONResponseShouldHaveNodes checks whether last request body has keys defined in string separated by comma nodeExpr should be valid according to qjson library expressions separated by comma (,)
func (*State) TheResponseBodyShouldHaveType ¶
TheResponseBodyShouldHaveType checks whether last response body has given data type available data types are listed as package constants
func (*State) TheResponseShouldHaveHeader ¶
TheResponseShouldHaveHeader checks whether last HTTP response has given header
func (*State) TheResponseShouldHaveHeaderOfValue ¶
TheResponseShouldHaveHeaderOfValue checks whether last HTTP response has given header with provided value
func (*State) TheResponseStatusCodeShouldBe ¶
TheResponseStatusCodeShouldBe compare last response status code with given in argument.