gdutils

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 26 Imported by: 1

README

gdutils

GDUTILS

Simple library with methods useful for e2e testing of HTTP(s) API.

Library is suitable for steps in godog framework.

Downloading

go get github.com/pawelWritesCode/gdutils

Documentation

Overview

Package gdutils provides State struct with 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 HttpClient, cache Cache, isDebug bool) *State

Struct State contains of

* State.Cache. - cache with arbitrary values,

* State.Debugger - debugging tool,

* State.HttpContext - tool for working with HTTP protocol,

* State.TemplateEngine - tool for working with templates,

* State.JSONSchemaValidator - tool that has ability to validate JSON schemas.

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

or

func (s *State) IPrepareNewRequestToAndSaveItAs(method, urlTemplate, cacheKey string) error
func (s *State) ISetFollowingHeadersForPreparedRequest(cacheKey string, headersTemplate *godog.DocString) error
func (s *State) ISetFollowingBodyForPreparedRequest(cacheKey string, bodyTemplate *godog.DocString) error
func (s *State) ISendRequest(cacheKey string) 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

State has also some useful utility methods like:

func (s *State) GetLastResponse() (*http.Response, error)
func (s *State) GetLastResponseBody() []byte
func (s *State) GetLastResponseHeaders() http.Header

Index

Constants

This section is empty.

Variables

View Source
var ErrGdutils = errors.New("gdutils")

ErrGdutils is general package error and can be tested against

View Source
var ErrHTTPReqRes = fmt.Errorf("%w: something wrong with HTTP(s) request/response", ErrGdutils)

ErrHTTPReqRes tells that there is problem with last HTTP(s) request/response

View Source
var ErrJson = fmt.Errorf("%w: something wrong with JSON", ErrGdutils)

ErrJson tells that there is problem with JSON

View Source
var ErrPreservedData = errors.New("preserved data error")

ErrPreservedData tells indices that there is some kind of error with scenario preserved data.

View Source
var ErrQJSON = fmt.Errorf("%w: could not obtain value from JSON", ErrJson)

ErrQJSON occurs when value could not be obtained from JSON

Functions

This section is empty.

Types

type BodyHeaders added in v0.7.4

type BodyHeaders struct {

	// Body should contain HTTP(s) request body
	Body interface{}

	// Headers should contain HTTP(s) request headers
	Headers map[string]string
}

BodyHeaders is entity that holds information about request body and request headers.

type State

type State struct {
	// IsDebug determine whether scenario is in debug mode
	Debugger debugger.Debugger

	// Cache is storage for scenario data
	Cache cache.Cache

	// HttpClient is entity that has ability to send HTTP(s) requests
	HttpContext httpctx.HttpContext

	// TemplateEngine is entity that has ability to retrieve templated values
	TemplateEngine template.TemplateEngine

	// JSONSchemaValidator is entity that has ability to validate JSON schemas
	JSONSchemaValidator validator.SchemaValidator
}

State struct represents data shared across one scenario.

func NewDefaultState

func NewDefaultState(isDebug bool, jsonSchemaDir string) *State

NewDefaultState returns *State with default http.Client, DefaultCache and default Debugger. jsonSchemaDir may be empty string or valid full path to directory with JSON schemas

func NewState

func NewState(httpClient *http.Client, cache cache.Cache, jsonSchemaValidator validator.SchemaValidator, isDebug bool) *State

NewState returns *State with provided HttpClient, cache and default Debugger

func (*State) IGenerateARandomFloatInTheRangeToAndSaveItAs

func (s *State) IGenerateARandomFloatInTheRangeToAndSaveItAs(from, to int, cacheKey string) error

IGenerateARandomFloatInTheRangeToAndSaveItAs generates random float from provided range and preserve it under given cacheKey key

func (*State) IGenerateARandomIntInTheRangeToAndSaveItAs

func (s *State) IGenerateARandomIntInTheRangeToAndSaveItAs(from, to int, cacheKey string) error

IGenerateARandomIntInTheRangeToAndSaveItAs generates random integer from provided range and preserve it under given cacheKey key

func (*State) IGenerateARandomStringInTheRangeToAndSaveItAs added in v0.8.0

func (s *State) IGenerateARandomStringInTheRangeToAndSaveItAs(charset string) func(from, to int, cacheKey string) error

IGenerateARandomStringInTheRangeToAndSaveItAs creates random string generator func using provided charset return func creates string from provided range and preserve it under given cacheKey

func (*State) IPrepareNewRequestToAndSaveItAs added in v0.7.0

func (s *State) IPrepareNewRequestToAndSaveItAs(method, urlTemplate, cacheKey string) error

IPrepareNewRequestToAndSaveItAs prepares new request and saves it in cache under cacheKey

func (*State) IPrintLastResponseBody

func (s *State) IPrintLastResponseBody() error

IPrintLastResponseBody prints last response from request

func (*State) ISaveFromTheLastResponseJSONNodeAs

func (s *State) ISaveFromTheLastResponseJSONNodeAs(expr, cacheKey string) error

ISaveFromTheLastResponseJSONNodeAs saves from last response body JSON node under given cacheKey key. expr should be valid according to qjson library

func (*State) ISendRequest added in v0.7.0

func (s *State) ISendRequest(cacheKey string) error

ISendRequest sends previously prepared HTTP(s) request

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) ISetFollowingBodyForPreparedRequest added in v0.7.0

func (s *State) ISetFollowingBodyForPreparedRequest(cacheKey string, bodyTemplate *godog.DocString) error

ISetFollowingBodyForPreparedRequest sets body for previously prepared request bodyTemplate may be in any format and accepts template values

func (*State) ISetFollowingHeadersForPreparedRequest added in v0.7.0

func (s *State) ISetFollowingHeadersForPreparedRequest(cacheKey string, headersTemplate *godog.DocString) error

ISetFollowingHeadersForPreparedRequest sets provided headers for previously prepared request

func (*State) IStartDebugMode

func (s *State) IStartDebugMode() error

IStartDebugMode starts debugging mode

func (*State) IStopDebugMode

func (s *State) IStopDebugMode() error

IStopDebugMode stops debugging mode

func (*State) IValidateLastResponseBodyWithSchema added in v0.7.3

func (s *State) IValidateLastResponseBodyWithSchema(schemaPath string) error

IValidateLastResponseBodyWithSchema validates last response body against JSON schema as provided in schemaPath

func (*State) IWait

func (s *State) IWait(timeInterval string) error

IWait waits for given timeInterval amount of time timeInterval should be string valid for time.ParseDuration func

func (*State) ResetState

func (s *State) ResetState(isDebug bool)

ResetState resets State struct instance to default values.

func (*State) TheJSONNodeShouldBe

func (s *State) TheJSONNodeShouldBe(expr string, goType string) error

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

func (s *State) TheJSONNodeShouldBeOfValue(expr, dataType, dataValue string) error

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

func (s *State) TheJSONNodeShouldBeSliceOfLength(expr string, length int) error

TheJSONNodeShouldBeSliceOfLength checks whether given key is slice and has given length expr should be valid according to qjson library

func (*State) TheJSONNodeShouldNotBe

func (s *State) TheJSONNodeShouldNotBe(expr string, goType string) error

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

func (s *State) TheJSONResponseShouldHaveNode(expr string) error

TheJSONResponseShouldHaveNode checks whether last response body contains given key

func (*State) TheJSONResponseShouldHaveNodes

func (s *State) TheJSONResponseShouldHaveNodes(nodeExprs string) error

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

func (s *State) TheResponseBodyShouldHaveType(dataType string) error

TheResponseBodyShouldHaveType checks whether last response body has given data type available data types are listed as package constants

func (*State) TheResponseShouldHaveHeader

func (s *State) TheResponseShouldHaveHeader(name string) error

TheResponseShouldHaveHeader checks whether last HTTP response has given header

func (*State) TheResponseShouldHaveHeaderOfValue

func (s *State) TheResponseShouldHaveHeaderOfValue(name, value string) error

TheResponseShouldHaveHeaderOfValue checks whether last HTTP response has given header with provided value

func (*State) TheResponseStatusCodeShouldBe

func (s *State) TheResponseStatusCodeShouldBe(code int) error

TheResponseStatusCodeShouldBe compare last response status code with given in argument.

Directories

Path Synopsis
pkg
Package pkg holds packages used for steps.
Package pkg holds packages used for steps.

Jump to

Keyboard shortcuts

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