Skip to content

ivpusic/httpcheck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

supertest inspired library for testing HTTP servers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages