httpcheck icon indicating copy to clipboard operation
httpcheck copied to clipboard

supertest inspired library for testing HTTP servers

httpcheck

Build Status

supertest inspired library for testing HTTP servers.

How to install?

go get github.com/ivpusic/httpcheck

API Documentation

godoc

How to use?

Basic example

package main

import (
	"github.com/ivpusic/httpcheck"
)

func TestExample(t *testing.T) {
	// testHandler should be instance of http.Handler
	checker := httpcheck.New(t, &testHandler{})

	checker.Test("GET", "/some/url").
		WithHeader("key", "value").
		WithCookie("key", "value").
		Check().
		HasStatus(200).
		HasCookie("key", "expectedValue").
		HasHeader("key", "expectedValue").
		HasJson(&someType{})
}

Include body

String

package main

import (
	"github.com/ivpusic/httpcheck"
)

func TestExample(t *testing.T) {
	checker := httpcheck.New(t, &testHandler{})

	checker.Test("GET", "/some/url").
		WithString("Hello!")
		Check().
		HasStatus(200)
}

JSON

package main

import (
	"github.com/ivpusic/httpcheck"
)

func TestExample(t *testing.T) {
	checker := httpcheck.New(t, &testHandler{})

	data := &someStruct{
		field1: "hi",
	}

	checker.Test("GET", "/some/url").
		WithJson(data)
		Check().
		HasStatus(200)
}

XML

package main

import (
	"github.com/ivpusic/httpcheck"
)

func TestExample(t *testing.T) {
	checker := httpcheck.New(t, &testHandler{})

	data := &someStruct{
		field1: "hi",
	}

	checker.Test("GET", "/some/url").
		WithXml(data)
		Check().
		HasStatus(200)
}

Provide *http.Request instance

package main

import (
	"net/http"
	"github.com/ivpusic/httpcheck"
)

func TestExample(t *testing.T) {
	checker := httpcheck.New(t, &testHandler{})

	checker.TestRequest(&http.Request{ /* fields */ }).
		Check().
		HasStatus(200)
}

Define callback

package main

import (
	"net/http"
	"github.com/ivpusic/httpcheck"
)

func TestExample(t *testing.T) {
	checker := httpcheck.New(t, &testHandler{})

	checker.Test("GET", "/some/url").
		Check().
		HasStatus(200).
		HasBody([]byte("some body")).
		Cb(func(response *http.Response) { /* do something */ })
}

Contribution Guidelines

  • Implement fix/feature
  • Write tests for fix/feature
  • Make sure all tests are passing
  • Send Pull Request

License

MIT