httptester

package module
v0.0.0-...-4d9cf44 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2023 License: MIT Imports: 4 Imported by: 0

README

httptester

httptester provides a format for running related tests in a loop. This encourages negative testing and can help facilitate better coverage.

Add to your project

go get github.com/dfirebaugh/httptester

Example


func TestGetEntity(t *testing.T) {
	comicRepo := in_memory.Repo[entity.Comic]{}
	comicService := service.New[entity.Comic](&comicRepo)
	comicHandler := v1.EntityHandler[entity.Comic]{
		comicService,
		responder.Responder{},
	}

	tests := []httptester.HTTPTest{
		{
			Method: http.MethodGet,
			URL:    "/v1/user",
			Body:   nil,
			Tests: []httptester.TestCase{
				{
					Post: func(res *httptest.ResponseRecorder) {
						// should respond with latest
					},
				},
			},
		},
		{
			Method: http.MethodGet,
			URL:    "/v1/user?latest",
			Body:   nil,
			Tests: []httptester.TestCase{
				{
					Post: func(res *httptest.ResponseRecorder) {
						// should respond with latest
					},
				},
			},
		},
		{
			Method: http.MethodGet,
			URL:    "/v1/user?first",
			Body:   nil,
			Tests: []httptester.TestCase{
				{
					Post: func(res *httptest.ResponseRecorder) {
						// should respond with first
					},
				},
			},
		},
		{
			Method: http.MethodGet,
			URL:    "/v1/user?start=01-02-1988",
			Body:   nil,
			Tests: []httptester.TestCase{
				{
					Post: func(res *httptest.ResponseRecorder) {
						// should fail because end is require
					},
				},
			},
		},
		{
			Method: http.MethodGet,
			URL:    "/v1/user?end=01-02-1988",
			Body:   nil,
			Tests: []httptester.TestCase{
				{
					Post: func(res *httptest.ResponseRecorder) {
						// should fail because start is require
					},
				},
			},
		},
		{
			Method: http.MethodGet,
			URL:    "/v1/user?start=01-02-1988end=02-29-2022",
			Body:   nil,
			Tests: []httptester.TestCase{
				{
					Post: func(res *httptest.ResponseRecorder) {
						// should fail because it's an invalid date
					},
				},
			},
		},
		{
			Method: http.MethodGet,
			URL:    "/v1/user?start=01-02-1988end=02-29-2022",
			Body:   nil,
			Tests: []httptester.TestCase{
				{
					Post: func(res *httptest.ResponseRecorder) {
						// should respond with comics in the valid range
					},
				},
			},
		},
	}

	for _, el := range tests {
		el.Execute(t, comicHandler.Get)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPRecorder

func HTTPRecorder(next http.HandlerFunc, r *http.Request) *httptest.ResponseRecorder

Types

type HTTPTest

type HTTPTest struct {
	Method string
	URL    string
	Body   io.Reader
	Tests  []TestCase
}

func (HTTPTest) Execute

func (h HTTPTest) Execute(t *testing.T, handler http.HandlerFunc)

type TestCase

type TestCase struct {
	Pre  func()
	Post func(res *httptest.ResponseRecorder)
}

func (TestCase) Execute

func (tc TestCase) Execute(t *testing.T, res *httptest.ResponseRecorder)

Jump to

Keyboard shortcuts

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