testutils

package
v0.0.0-...-8eb6d81 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateHeader

func CreateHeader() http.Header

func NewMockGQLError

func NewMockGQLError(t *testing.T, expectedHeader http.Header) *httptest.Server

func NewMockGQLOutput

func NewMockGQLOutput(t *testing.T, expectedHeader http.Header, output interface{}) *httptest.Server

func NewMockGQLServer

func NewMockGQLServer(t *testing.T, expectedHeader http.Header, responseStatus int, response []byte) *httptest.Server

func NewMockServer

func NewMockServer(t *testing.T, expectedHeader http.Header, expectedMethod string, expectJSON bool, responseStatus int, response []byte) *httptest.Server

func ToGenericMap

func ToGenericMap(i interface{}) map[string]interface{}

ToGenericMap converts a struct to the generic `map[string]interface{}` used as the default for json.Unmarshal. Its main purpose is to convert GraphQL input values when writing unit tests for GraphQL API clients. It cannot be Object above because the type does not match for equality checks.

Types

type MockGraphQLHandler

type MockGraphQLHandler struct {
	ResponseStatus    int
	Response          interface{}
	Errors            []graphql.Error
	ExpectedHeaders   http.Header
	ExpectedVariables map[string]interface{}
	// contains filtered or unexported fields
}

MockGraphQLHandler provides a handler to mock a GraphQL server in combination with httptest.Server. It allows the response, headers, expected input variables and errors to be set up and changed between each test. It is designed to be run in a single test with sub-tests.

func NewMockGraphQLHandler

func NewMockGraphQLHandler(t *testing.T) *MockGraphQLHandler

NewMockGraphQLHandler sets up a MockGraphQLHandler to use the given testing.T for assertions with a default ResponseStatus of http.StatusOK.

func (*MockGraphQLHandler) ServeHTTP

func (g *MockGraphQLHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the standard net/http handler which validates the incoming GraphQL request and returns a mock response or error.

func (*MockGraphQLHandler) WithErrors

func (g *MockGraphQLHandler) WithErrors(errors []graphql.Error, cb func())

WithErrors will set up the provided errors to only be present during the provided callback, making it easy to set up errors for a single test.

Jump to

Keyboard shortcuts

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