Skip to content

ikawaha/httpcheck

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Go Reference

httpcheck

supertest inspired library for testing HTTP servers.

A Fork from ivpusic/httpcheck with following changes:

  • Change to set testing.T when generating the request instead of the constructor,
  • Fix to prevent incorrect method chain,
  • Add to the timeout option of the client to the checker.

How to install?

go get github.com/ikawaha/httpcheck

API Documentation

Go Reference

How to use?

Basic example

package main

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

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

	checker.Test(t, "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(&testHandler{})

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

JSON

package main

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

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

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

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

XML

package main

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

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

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

	checker.Test(t, "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(&testHandler{})

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

Define callback

package main

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

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

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

License MIT