Version: v1.1.0 Latest Latest

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

Go to latest
Published: Mar 30, 2020 License: MIT Imports: 8 Imported by: 0



Package assert implements assertions to be run on HTTP response




This section is empty.


This section is empty.


func CompactJSON

func CompactJSON(jsonStr string, t *testing.T) string

CompactJSON returns JSON compacted to one line

func CompactJSONb

func CompactJSONb(jsonBytes []byte, t *testing.T) string

CompactJSONb returns JSON compacted to one line

func IndentJSON

func IndentJSON(jsonStr string, t *testing.T) string

IndentJSON pretty-prints JSON

func IndentJSONb

func IndentJSONb(jsonBytes []byte, t *testing.T) string

IndentJSONb pretty-prints JSON

func SetDiff

func SetDiff(d Differ)

TODO research for diffing


type Assert

type Assert struct {
	R *http.Response
	T *testing.T

Assert Specifies how the http.Response should look like by chaining assertions on it. Assertions are evaluated immediately.

func (*Assert) Body

func (a *Assert) Body(test func(t *testing.T, responseBody []byte)) *Assert

Body Assert that handler returned a specific body.

func (*Assert) ContentType

func (a *Assert) ContentType(contentType string) *Assert

ContentType Assert that response is of specific `Content-Type`

func (*Assert) Custom

func (a *Assert) Custom(customTest func(t *testing.T, response *http.Response)) *Assert

Custom deprecated in favor of Response

func (*Assert) Header

func (a *Assert) Header(key string, value string) *Assert

Header Assert that specific header is set with given value

func (*Assert) HeaderMissing

func (a *Assert) HeaderMissing(key string) *Assert

HeaderMissing Assert that specific header is not set.

func (*Assert) JSON

func (a *Assert) JSON(expectedContent string) *Assert

JSON Assert that the response equals to given JSON. Indentation here doesn't play a role.

To show a diff between expected and actual values set `HANDLERTEST_DIFF=true` env variable

func (*Assert) JSONMatches

func (a *Assert) JSONMatches(test interface{}) *Assert

JSONMatches Asserts that JSON response unmarshalls to object given as 2nd argument in the function further asserting conditions on the returned objects.

In case it's hard to predict your whole response, or you don't want to test the whole response, you might use this function to get an unmarshalled response body and test for specific values.

package main

import (


func main() {
	ProductListControllerThatLikesCategoryB := func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte(`[{"category":"b"}]`))

	type Product struct {
		Category string

	// TODO mock t in #12 to test for returned value
	t := new(testing.T)

	// create your request
		// then assert your expectations
		JSONMatches(func(t *testing.T, products []Product) {
			// unmarshalling of JSON objects is done for you
			if len(products) == 0 {
				t.Errorf("Expected to have some products returned")
			for _, p := range products {
				if p.Category != "a" {
					t.Errorf("Expected filter to return products only of category %s, but got %s",
						"a", p.Category)

					fmt.Printf("Expected filter to return products only of category %s, but got %s",
						"a", p.Category)


Expected filter to return products only of category a, but got b

func (*Assert) JSONUnmarshallsTo

func (a *Assert) JSONUnmarshallsTo(obj interface{}) *Assert

JSONUnmarshallsTo Assert that JSON response unmarshalls to given object / list

func (*Assert) Response added in v1.1.0

func (a *Assert) Response(customTest func(t *testing.T, response *http.Response)) *Assert

Response Use it to implement custom assertions you might need that is not covered by this lib use below function. Please also file an issue to support your case if it is general enough.

func (*Assert) Status

func (a *Assert) Status(statusCode int) *Assert

Status Assert that response has specific HTTP Status Code

Example (Multiple)
package main

import (


func main() {
	AHandler := func(w http.ResponseWriter, r *http.Request) {
		statusStr := r.Header.Get("X-Return")
		status, err := strconv.Atoi(statusStr)
		var response string

		if err != nil {
			response = `{"code": "WRONG_INPUT"}`
		} else {
			response = `{"id": 1}`

		_, err = w.Write([]byte(response))
		if err != nil {

	type successfulResponse struct {
		ID int `json:"id"`
	type errorResponse struct {
		Code string `json:"code"`

	t := new(testing.T)

	type expected struct {
		status      int
		jsonMatches interface{}

	tests := []struct {
		name     string
		status   string
		expected expected
		name:   "Status 200",
		status: "200",
		expected: expected{
			status: 200,
			jsonMatches: func(t *testing.T, ret successfulResponse) {
				if ret.ID != 1 {
					t.Error("Expected id=1")
	}, {
		name:   "Error",
		status: "awfa",
		expected: expected{
			status: 400,
			jsonMatches: func(t *testing.T, ret errorResponse) {
				if ret.Code != "WRONG_INPUT_DATA" {
					t.Errorf("Expected WRONG_INPUT_DATA, got %s", ret.Code)
					fmt.Printf("Expected WRONG_INPUT_DATA, got %s", ret.Code)

	for _, tt := range tests {
		// for somereason example don't like t.Run
		//t.Run(, func(t *testing.T) {
		handlertest.Call(AHandler).Header("X-Return", tt.status).



func (*Assert) TestRun

func (a *Assert) TestRun() func(*testing.T)

type Differ

type Differ func(minusPrefixed, plusPrefixed interface{}) string

Differ Custom implementation of diffing algorithm

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL