golden

package module
v0.0.0-...-645f897 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

Simple golden file test helper for HTTP responses.

Check text response is the same as the one in golden file.

req := httptest.NewRequest(http.MethodGet, "/api/endpoint", nil)
rec := httptest.NewRecorder()
handler(rec, req)

golden.Resp(suite.T(), "testdata/test.golden", rec).Check()

Check JSON response is the same as the one in golden file using testify assertion for body contents.

req := httptest.NewRequest(http.MethodGet, "/api/endpoint", nil)
rec := httptest.NewRecorder()
handler(rec, req)

golden.Resp(suite.T(), "testdata/test.golden", rec).BodyTester(suite.JSONEq).Check()

Save golden file:

req := httptest.NewRequest(http.MethodGet, "/api/endpoint", nil)
rec := httptest.NewRecorder()
handler(rec, req)

golden.Resp(suite.T(), "testdata/test.golden", rec).BodyTester(suite.JSONEq).Save()

The suite.T() in above examples comes from https://github.com/stretchr/testify

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyTester

type BodyTester func(exp string, act string, msgAndArgs ...interface{}) bool

BodyTester is a function used to compare HTTP body contents. It is compatible with testify JSONEq assertion.

type RespTester

type RespTester interface {
	// BodyTester sets function used to compare HTTP bodies.
	BodyTester(tester BodyTester) RespTester
	// Check checks if golden data matches ResponseRecorder.
	// Check panics if golden resp cannot be read or it cannot be unmarshaled.
	Check() RespTester
	// Save saves golden resp.
	// It panics if file cannot be marshaled or written.
	Save() RespTester
}

RespTester represents golden resp tester.

func Resp

func Resp(t *testing.T, path string, rec *httptest.ResponseRecorder) RespTester

Resp returns HTTP response golden resp tester.

Jump to

Keyboard shortcuts

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