Documentation ¶
Index ¶
- func CreateHeader() http.Header
- func NewMockGQLError(t *testing.T, expectedHeader http.Header) *httptest.Server
- func NewMockGQLOutput(t *testing.T, expectedHeader http.Header, output interface{}) *httptest.Server
- func NewMockGQLServer(t *testing.T, expectedHeader http.Header, responseStatus int, response []byte) *httptest.Server
- func NewMockServer(t *testing.T, expectedHeader http.Header, expectedMethod string, ...) *httptest.Server
- func ToGenericMap(i interface{}) map[string]interface{}
- type MockGraphQLHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateHeader ¶
func NewMockGQLError ¶
func NewMockGQLOutput ¶
func NewMockGQLServer ¶
func NewMockServer ¶
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.