httpcheck

package module
v1.12.1 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 17 Imported by: 7

README

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

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 TestingT.

func New

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

New creates an HTTP Checker for testing with the given handler.

func NewExternal added in v1.10.2

func NewExternal(url string, options ...Option) *Checker

NewExternal creates an HTTP Checker for testing an external server specified by url.

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 TestingT, 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 TestingT, 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 FieldPart added in v1.9.0

type FieldPart struct {
	FieldName string
	Value     string
}

FieldPart represents a multipart part of the field type part.

func (FieldPart) Part added in v1.9.0

func (p FieldPart) Part(mw *multipart.Writer) error

Part returns a multipart part.

type FilePart added in v1.9.0

type FilePart struct {
	FieldName string
	FileName  string
}

FilePart represents a multipart part of the file type part.

func (FilePart) Part added in v1.9.0

func (p FilePart) Part(mw *multipart.Writer) error

Part returns a multipart part.

type Option

type Option func(*Checker)

Option represents the option for the checker.

func CheckRedirect added in v1.4.0

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 Debug added in v1.12.1

func Debug() Option

func NoRedirect added in v1.4.0

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 Parter added in v1.9.0

type Parter interface {
	Part(mw *multipart.Writer) error
}

Parter is the interface that create a multipart part.

type Tester

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

Tester represents the HTTP tester having TestingT.

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(expected []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 checks if the response contains cookie with provided key and value.

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(expected any) *Tester

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

func (*Tester) HasJson deprecated

func (tt *Tester) HasJson(expected any) *Tester

Deprecated: HasJson checks if the response body contains json with provided value.

func (*Tester) HasStatus

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

HasStatus checks if the response status is equal to that provided.

func (*Tester) HasString

func (tt *Tester) HasString(expected 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(expected any) *Tester

HasXML checks if body contains xml with provided value.

func (*Tester) HasXml deprecated

func (tt *Tester) HasXml(expected any) *Tester

Deprecated: HasXml checks if body contains xml with provided value.

func (*Tester) MatchesJSONQuery added in v1.12.0

func (tt *Tester) MatchesJSONQuery(q string) *Tester

func (*Tester) MustContainsBody added in v1.7.0

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

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

func (*Tester) MustContainsString added in v1.7.0

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

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

func (*Tester) MustHasBody added in v1.7.0

func (tt *Tester) MustHasBody(expected []byte) *Tester

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

func (*Tester) MustHasCookie added in v1.7.0

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

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

func (*Tester) MustHasHeader added in v1.7.0

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

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

func (*Tester) MustHasHeaders added in v1.7.0

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

MustHasHeaders checks if the response contains a provided headers map.

func (*Tester) MustHasJSON added in v1.7.0

func (tt *Tester) MustHasJSON(expected any) *Tester

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

func (*Tester) MustHasStatus added in v1.7.0

func (tt *Tester) MustHasStatus(expected int) *Tester

MustHasStatus checks if the response status is equal to that provided.

func (*Tester) MustHasString added in v1.7.0

func (tt *Tester) MustHasString(expected string) *Tester

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

func (*Tester) MustHasXML added in v1.7.0

func (tt *Tester) MustHasXML(expected any) *Tester

MustHasXML checks if body contains xml with provided value.

func (*Tester) MustNotContainsBody added in v1.7.0

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

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

func (*Tester) MustNotContainsString added in v1.7.0

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

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

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) NotMatchesJSONQuery added in v1.12.0

func (tt *Tester) NotMatchesJSONQuery(q string) *Tester

func (*Tester) T added in v1.11.0

func (tt *Tester) T() TestingT

T returns the TestingT.

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 added in v1.5.0

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 puts cookie on the request.

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 added in v1.6.0

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

WithHostHeader puts "Host" header on the request.

func (*Tester) WithJSON

func (tt *Tester) WithJSON(expected any) *Tester

WithJSON adds a json encoded struct to the body.

func (*Tester) WithJson deprecated

func (tt *Tester) WithJson(expected any) *Tester

Deprecated: WithJson adds a json encoded struct to the body.

func (*Tester) WithMultipart added in v1.9.0

func (tt *Tester) WithMultipart(part Parter, parts ...Parter) *Tester

WithMultipart add a multipart data to the body.

func (*Tester) WithString

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

WithString adds the string to the body.

func (*Tester) WithXML

func (tt *Tester) WithXML(body any) *Tester

WithXML adds a xml encoded body to the request.

func (*Tester) WithXml deprecated

func (tt *Tester) WithXml(body any) *Tester

Deprecated: WithXml adds a xml encoded body to the request.

type TestingT added in v1.8.0

type TestingT interface {
	Errorf(format string, args ...any)
	FailNow()
}

TestingT is an interface wrapper around *testing.T.

Directories

Path Synopsis
plugin
goa Module

Jump to

Keyboard shortcuts

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