helpers

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2020 License: Apache-2.0 Imports: 22 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertAPIRequest

func AssertAPIRequest(
	t testing.TB,
	testServer ServerInitializer,
	APIPrefix string,
	request *APIRequest,
	expectedResponse *APIResponse,

)

AssertAPIRequest sends sends api request and checks api response (see docs for APIRequest and APIResponse) to the provided testServer using the provided APIPrefix

func AssertReportResponsesEqual

func AssertReportResponsesEqual(t testing.TB, expected, got []byte)

AssertReportResponsesEqual checks if reports in answer are the same

func AssertRuleResponsesEqual added in v1.4.1

func AssertRuleResponsesEqual(t testing.TB, expected, got []byte)

AssertRuleResponsesEqual checks if rules in answer are the same

func AssertStringsAreEqualJSON

func AssertStringsAreEqualJSON(t testing.TB, expected, got string)

AssertStringsAreEqualJSON checks whether strings represent the same JSON (whitespaces and order of elements doesn't matter) and asserts error otherwise

func CheckResponseBodyJSON

func CheckResponseBodyJSON(t testing.TB, expectedJSON string, body io.ReadCloser)

CheckResponseBodyJSON checks if body is the same json as in expected (ignores whitespaces, newlines, etc) also validates both expected and body to be a valid json

func CleanAfterGock added in v1.2.1

func CleanAfterGock(t testing.TB)

CleanAfterGock cleans after gock library and prints all unmatched requests

func ExecuteRequest

func ExecuteRequest(testServer ServerInitializer, req *http.Request) *httptest.ResponseRecorder

ExecuteRequest executes http request on a testServer

func FailOnError

func FailOnError(t testing.TB, err error)

FailOnError wraps result of function with one argument

func FrisbyExpectItemInArray

func FrisbyExpectItemInArray(fieldName string, expectedItem interface{}) frisby.ExpectFunc

FrisbyExpectItemInArray returns checker function for frisby to check if item is in the array Example:

frisby.Create("test creating organization"). Get(apiURL + "/organization"). Send(). ExpectStatus(200). Expect(helpers.FrisbyExpectItemInArray("organizations", 55))

will check if 55 is in organizations, like here `{"organizations": [1, 2, 3, 55], "status": "ok"}`

func GockExpectAPIRequest added in v1.2.1

func GockExpectAPIRequest(t *testing.T, baseURL string, request *APIRequest, response *APIResponse)

GockExpectAPIRequest makes gock expect the request with the baseURL and sends back the response

func JSONUnmarshalStrict

func JSONUnmarshalStrict(data []byte, outObj interface{}) error

JSONUnmarshalStrict unmarshales json and returns error if some field exist in data, but not in outObj

func MustGobSerialize added in v1.2.1

func MustGobSerialize(t testing.TB, obj interface{}) []byte

MustGobSerialize serializes an object using gob or panics

func NewGockAPIEndpointMatcher

func NewGockAPIEndpointMatcher(endpoint string) func(req *http.Request, _ *gock.Request) (bool, error)

NewGockAPIEndpointMatcher returns new matcher for github.com/h2non/gock to match endpoint with any args

func NewGockRequestMatcher added in v1.2.1

func NewGockRequestMatcher(
	t *testing.T, method string, url string, body interface{},
) func(*http.Request, *gock.Request) (bool, error)

NewGockRequestMatcher returns a new matcher for github.com/h2non/gock to match requests with provided method, url and body(the same types as body in APIRequest(see the docs))

func RunTestWithTimeout

func RunTestWithTimeout(t *testing.T, test TestFunctionPtr, timeToRun time.Duration)

RunTestWithTimeout runs test with timeToRun timeout and fails if it wasn't in time

func ToJSONPrettyString added in v1.2.1

func ToJSONPrettyString(obj interface{}) string

ToJSONPrettyString converts anything to indented JSON or panics if it's not possible

func ToJSONString

func ToJSONString(obj interface{}) string

ToJSONString converts anything to JSON or panics if it's not possible

Types

type APIRequest

type APIRequest struct {
	Method             string
	Endpoint           string
	EndpointArgs       []interface{}
	Body               interface{}
	UserID             types.UserID
	OrgID              types.OrgID
	XRHIdentity        string
	AuthorizationToken string
	ExtraHeaders       http.Header
}

APIRequest is a request to api to use in AssertAPIRequest

(required) Method is an http method (required) Endpoint is an endpoint without api prefix EndpointArgs are the arguments to pass to endpoint template (leave empty if endpoint is not a template) Body is a request body which can be a string or []byte (leave empty to not send) UserID is a user id for methods requiring user id (leave empty to not use it) OrgID is an org id for methods requiring it to be in token (leave empty to not use it) XRHIdentity is an authentication token (leave empty to not use it) AuthorizationToken is an authentication token (leave empty to not use it)

type APIResponse

type APIResponse struct {
	StatusCode  int
	Body        interface{}
	BodyChecker BodyChecker
	Headers     map[string]string
}

APIResponse is an expected api response to use in AssertAPIRequest

StatusCode is an expected http status code (leave empty to not check for status code) Body is an expected body which can be a string or []byte(leave empty to not check for body) BodyChecker is a custom body checker function (leave empty to use default one - CheckResponseBodyJSON)

type BodyChecker added in v1.2.1

type BodyChecker func(t testing.TB, expected, got []byte)

BodyChecker represents body checker type for api response

type MicroHTTPServer added in v1.3.0

type MicroHTTPServer struct {
	Serv      *http.Server
	Router    *mux.Router
	APIPrefix string
}

MicroHTTPServer in an implementation of ServerInitializer interface This small implementation could help implementing tests without using a real HTTP server implementation

func NewMicroHTTPServer added in v1.3.0

func NewMicroHTTPServer(address string, apiPrefix string) *MicroHTTPServer

NewMicroHTTPServer creates a MicroHTTPServer for the given address and prefix

func (*MicroHTTPServer) AddEndpoint added in v1.3.0

func (server *MicroHTTPServer) AddEndpoint(endpoint string, f func(http.ResponseWriter, *http.Request))

AddEndpoint adds a handler function to the router in order to response to the given endpoint

func (*MicroHTTPServer) Initialize added in v1.3.0

func (server *MicroHTTPServer) Initialize() http.Handler

Initialize returns the Handler instance in order to be modified

type ServerInitializer

type ServerInitializer interface {
	Initialize() http.Handler
}

ServerInitializer is interface which is implemented for any server having Initialize method

type TestFunctionPtr

type TestFunctionPtr = func(*testing.T)

TestFunctionPtr pointer to test function

Jump to

Keyboard shortcuts

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