httpcheck

package module
v1.5.1-0...-91c44b5 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2021 License: MIT Imports: 14 Imported by: 0

README

Build Status

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

GoDoc

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

Documentation

Index

Constants

View Source
const (
	// DefaultClientTimeout is the default timeout for requests made by checker.
	DefaultClientTimeout = 5 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

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

Checker represents the HTTP checker without testing.T.

func New

func New(h http.Handler, options ...Option) *Checker

New creates a HTTP Checker.

func (*Checker) GetURL

func (c *Checker) GetURL() string

GetURL returns the server URL.

func (*Checker) PersistCookie

func (c *Checker) PersistCookie(cookie string)

PersistCookie - enables a cookie to be preserved between requests

func (*Checker) Test

func (c *Checker) Test(t *testing.T, method, path string) *Tester

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

func (*Checker) TestRequest

func (c *Checker) TestRequest(t *testing.T, request *http.Request) *Tester

TestRequest - 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 instance will be used for making request

func (*Checker) UnpersistCookie

func (c *Checker) UnpersistCookie(cookie string)

UnpersistCookie - the specified cookie will not be preserved during requests anymore

type Option

type Option func(*Checker)

Option represents the option for the checker.

func CheckRedirect

func CheckRedirect(policy func(req *http.Request, via []*http.Request) error) Option

CheckRedirect sets the policy of redirection to the HTTP client.

func ClientTimeout

func ClientTimeout(d time.Duration) Option

ClientTimeout sets the client timeout.

func NoRedirect

func NoRedirect() Option

NoRedirect is the alias of the following:

CheckRedirect(func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
})

Client returns ErrUseLastResponse, the next request is not sent and the most recent response is returned with its body unclosed.

type Tester

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

Tester represents the HTTP tester having testing.T.

func (*Tester) Cb

func (tt *Tester) Cb(callback func(*http.Response))

Cb - Will call provided callback function with current response

func (*Tester) Check

func (tt *Tester) Check() *Tester

Check - Will make request 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 (*Tester) ContainsBody

func (tt *Tester) ContainsBody(segment []byte) *Tester

ContainsBody checks if the body contains provided [] byte data.

func (*Tester) ContainsString

func (tt *Tester) ContainsString(substr string) *Tester

ContainsString converts the response to a string type and then checks it containing the given string.

func (*Tester) HasBody

func (tt *Tester) HasBody(body []byte) *Tester

HasBody checks if the body is equal to provided []byte data.

func (*Tester) HasCookie

func (tt *Tester) HasCookie(key, expectedValue string) *Tester

HasCookie puts cookie on the request.

func (*Tester) HasHeader

func (tt *Tester) HasHeader(key, expectedValue string) *Tester

HasHeader checks if the response contains header on provided key with provided value.

func (*Tester) HasHeaders

func (tt *Tester) HasHeaders(headers map[string]string) *Tester

HasHeaders checks if the response contains a provided headers map

func (*Tester) HasJSON

func (tt *Tester) HasJSON(value interface{}) *Tester

HasJSON checks if the response body contains json with provided value.

func (*Tester) HasJson

func (tt *Tester) HasJson(value interface{}) *Tester

HasJson checks if the response body contains json with provided value. (deprecated)

func (*Tester) HasStatus

func (tt *Tester) HasStatus(status int) *Tester

HasStatus checks if the response status is equal to provided.

func (*Tester) HasString

func (tt *Tester) HasString(body string) *Tester

HasString converts the response to a string type and then compares it with the given string.

func (*Tester) HasXML

func (tt *Tester) HasXML(value interface{}) *Tester

HasXML checks if body contains xml with provided value.

func (*Tester) HasXml

func (tt *Tester) HasXml(value interface{}) *Tester

HasXml checks if body contains xml with provided value. (deprecated)

func (*Tester) NotContainsBody

func (tt *Tester) NotContainsBody(segment []byte) *Tester

NotContainsBody checks if the body does not contain provided [] byte data.

func (*Tester) NotContainsString

func (tt *Tester) NotContainsString(substr string) *Tester

NotContainsString converts the response to a string type and then checks if it does not contain the given string.

func (*Tester) WithBasicAuth

func (tt *Tester) WithBasicAuth(user, pass string) *Tester

WithBasicAuth is an alias to set basic auth in the request header.

func (*Tester) WithBearerAuth

func (tt *Tester) WithBearerAuth(token string) *Tester

WithBearerAuth is an alias to set bearer auth in the request header.

func (*Tester) WithBody

func (tt *Tester) WithBody(body []byte) *Tester

WithBody adds the []byte data to the body.

func (*Tester) WithCookie

func (tt *Tester) WithCookie(key, value string) *Tester

WithCookie checks if the response contains cookie with provided key and value.

func (*Tester) WithHeader

func (tt *Tester) WithHeader(key, value string) *Tester

WithHeader puts header on the request.

func (*Tester) WithHeaders

func (tt *Tester) WithHeaders(headers map[string]string) *Tester

WithHeaders puts a map of headers on the request.

func (*Tester) WithHostHeader

func (tt *Tester) WithHostHeader(value string) *Tester

WithHostHeader puts "Host" header on the request.

func (*Tester) WithJSON

func (tt *Tester) WithJSON(value interface{}) *Tester

WithJSON adds a json encoded struct to the body.

func (*Tester) WithJson

func (tt *Tester) WithJson(value interface{}) *Tester

WithJson adds a json encoded struct to the body. (deprecated) nolint:golint

func (*Tester) WithString

func (tt *Tester) WithString(body string) *Tester

WithString adds the string to the body.

func (*Tester) WithXML

func (tt *Tester) WithXML(value interface{}) *Tester

WithXML adds a xml encoded body to the request.

func (*Tester) WithXml

func (tt *Tester) WithXml(value interface{}) *Tester

WithXml adds a xml encoded body to the request. (deprecated)

Jump to

Keyboard shortcuts

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