httpcheck

package module
v0.0.0-...-c49b174 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2017 License: MIT Imports: 12 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(*http.Response)

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

func New

func New(t *testing.T, handler http.Handler) *Checker

func (*Checker) Cb

func (c *Checker) Cb(cb Callback)

Will call provided callback function with current response

func (*Checker) Check

func (c *Checker) Check() *Checker

Will make reqeust to built request object. After request is made, it will save response object for future assertions Responsibility of this method is also to start and stop HTTP server

func (*Checker) GetUrl

func (c *Checker) GetUrl() string

func (*Checker) HasBody

func (c *Checker) HasBody(body []byte) *Checker

Will check if body contains provided []byte data

func (*Checker) HasCookie

func (c *Checker) HasCookie(key, expectedValue string) *Checker

Will put cookie on request

func (*Checker) HasHeader

func (c *Checker) HasHeader(key, expectedValue string) *Checker

Will check if response contains header on provided key with provided value

func (*Checker) HasHeaders

func (c *Checker) HasHeaders(headers map[string]string) *Checker

Will check if response contains a provided headers map

func (*Checker) HasJson

func (c *Checker) HasJson(value interface{}) *Checker

Will ckeck if body contains json with provided value

func (*Checker) HasStatus

func (c *Checker) HasStatus(status int) *Checker

Will ckeck if response status is equal to provided

func (*Checker) HasString

func (c *Checker) HasString(body string) *Checker

Convenience wrapper for HasBody Checks if body is equal to the given string

func (*Checker) HasXml

func (c *Checker) HasXml(value interface{}) *Checker

Will ckeck if body contains xml with provided value

func (*Checker) PersistCookie

func (c *Checker) PersistCookie(cookie string)

enables a cookie to be preserved between requests

func (*Checker) SetTesting

func (c *Checker) SetTesting(t *testing.T) *Checker

func (*Checker) Test

func (c *Checker) Test(method, path string) *Checker

Prepare for testing some part of code which lives on provided path and method.

func (*Checker) TestRequest

func (c *Checker) TestRequest(request *http.Request) *Checker

If you want to provide you custom http.Request instance, you can do it using this method In this case internal http.Request instance won't be created, and passed instane will be used for making request

func (*Checker) UnpersistCookie

func (c *Checker) UnpersistCookie(cookie string)

the specified cookie will not be preserved during requests anymore

func (*Checker) WithBody

func (c *Checker) WithBody(body []byte) *Checker

Adds the []byte data to the body

func (*Checker) WithCookie

func (c *Checker) WithCookie(key, value string) *Checker

Will ckeck if response contains cookie with provided key and value

func (*Checker) WithHeader

func (c *Checker) WithHeader(key, value string) *Checker

Will put header on request

func (*Checker) WithHeaders

func (c *Checker) WithHeaders(headers map[string]string) *Checker

Will put a map of headers on request

func (*Checker) WithJson

func (c *Checker) WithJson(value interface{}) *Checker

Will add the json-encoded struct to the body

func (*Checker) WithString

func (c *Checker) WithString(body string) *Checker

Adds the string to the body

func (*Checker) WithXml

func (c *Checker) WithXml(value interface{}) *Checker

Adds a XML encoded body to the request

Jump to

Keyboard shortcuts

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