httptestutil

package module
v0.0.0-...-e6e658f Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: MIT Imports: 6 Imported by: 0

README

HTTPTestUtil

Contains shared utilities for running http tests.

You craft a httptestutil.TestSet, or a set of assertions about the behavior of a handler. This consists of multiple httptestutil.TestConfig, which are created using the httptestutil.Test function - this takes a name for the test, and then a series of httptestutil.TestOption which either modify the request or create assertions about the response.

This is most useful for blackbox testing of a handler, where you don't understand the internals.

Simple Example

package main

import (
	"testing"
	"net/http"
	"github.com/dougrich/httptestutil"
)

func TestHandler(t *testing.T) {
	httptestutil.TestSet{
		httptestutil.Test(
			"RejectGET405",
			httptestutil.RequestMethod(http.MethodGet),
			httptestutil.ResponseStatus(http.StatusMethodNotAllowed),
		),
	}.Run(t, &Handler{})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check func(*testing.T)

type RequestModifier

type RequestModifier func(req *http.Request)

type ResponseAssertion

type ResponseAssertion func(*testing.T, *httptest.ResponseRecorder)

type TestConfig

type TestConfig struct {
	// contains filtered or unexported fields
}

func Test

func Test(name string, options ...TestOption) TestConfig

Test creates a single test object

type TestOption

type TestOption func(*TestConfig)

func After

func After(check Check) TestOption

func Before

func Before(check Check) TestOption

func RequestBody

func RequestBody(body string) TestOption

RequestBody sets the body for a request

func RequestHeader

func RequestHeader(header string, value string) TestOption

RequestHeader sets a specific header on the request

func RequestJSON

func RequestJSON(d interface{}) TestOption

RequestJSON sets the body and the correct content type for a request

func RequestMethod

func RequestMethod(method string) TestOption

RequestMethod sets the method for the request

func RequestRel

func RequestRel(rel string) TestOption

RequestRel sets the relative url for the request (i.e. "/abc")

func ResponseBody

func ResponseBody(expectedBody string) TestOption

ResponseBody asserts that the response received has the expected body

func ResponseCookie

func ResponseCookie(name string, value string) TestOption

ResponseCookie asserts that the response received has a specific cookie

func ResponseHeader

func ResponseHeader(header string, expected string) TestOption

ResponseHeader asserts that the response received has the expected header

func ResponseJsonField

func ResponseJsonField(field string, expected interface{}) TestOption

ResponseJsonField asserts that a specific JSON field has a specific value

func ResponseJsonFieldMatcher

func ResponseJsonFieldMatcher(field string, matcher func(t *testing.T, actual interface{})) TestOption

ResponseJsonFieldMatcher is a looser version which provides a more primitive assertion

func ResponseJsonFieldPattern

func ResponseJsonFieldPattern(field string, pattern string) TestOption

ResponseJsonFieldPattern asserts that a specific JSON field matches a regular expression string

func ResponseStatus

func ResponseStatus(expectedStatus int) TestOption

ResponseStatus asserts that the response received has the expected status

type TestSet

type TestSet []TestConfig

func (TestSet) Run

func (tests TestSet) Run(t *testing.T, handler http.Handler)

TestSet.Run runs the tests against a given handler inside the greater testing context. It runs each test as a sub test.

Jump to

Keyboard shortcuts

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