apitest

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package apitest provides methods for testing client-server communication. It can be used to test http.Handler to build complex assertions on the HTTP responses.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CookieAssertion added in v0.8.0

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

CookieAssertion is used to build assertions for cookies.

func AssertCookie added in v0.8.0

func AssertCookie(t TestingT, cookie *http.Cookie) *CookieAssertion

AssertCookie asserts cookie.

func (*CookieAssertion) Expires added in v0.8.0

Expires asserts cookie expiration with fluent time assertions.

func (*CookieAssertion) IsHTTPOnly added in v0.8.0

func (a *CookieAssertion) IsHTTPOnly() *CookieAssertion

IsHTTPOnly asserts that cookie is HTTP only.

func (*CookieAssertion) IsNotHTTPOnly added in v0.8.0

func (a *CookieAssertion) IsNotHTTPOnly() *CookieAssertion

IsNotHTTPOnly asserts that cookie is not HTTP only.

func (*CookieAssertion) IsNotSecure added in v0.8.0

func (a *CookieAssertion) IsNotSecure() *CookieAssertion

IsNotSecure asserts that cookie is not secure.

func (*CookieAssertion) IsSecure added in v0.8.0

func (a *CookieAssertion) IsSecure() *CookieAssertion

IsSecure asserts that cookie is secure.

func (*CookieAssertion) WithDomain added in v0.8.0

func (a *CookieAssertion) WithDomain() *assertions.StringAssertion

WithDomain asserts cookie domain with fluent string assertions.

func (*CookieAssertion) WithPath added in v0.8.0

WithPath asserts cookie path with fluent string assertions.

func (*CookieAssertion) WithSameSite added in v0.8.0

func (a *CookieAssertion) WithSameSite(wantSameSite http.SameSite) *CookieAssertion

WithSameSite asserts that cookie has same site value.

func (*CookieAssertion) WithValue added in v0.8.0

func (a *CookieAssertion) WithValue() *assertions.StringAssertion

WithValue asserts cookie value with fluent string assertions.

type RequestOption

type RequestOption func(r *http.Request)

RequestOption can be used to tune up http.Request.

func WithContentType added in v0.8.0

func WithContentType(contentType string) RequestOption

WithContentType option overrides Content-Type header to the request.

func WithCookie added in v0.8.0

func WithCookie(cookie *http.Cookie) RequestOption

WithCookie option adds a cookie to the request.

func WithHeader

func WithHeader(key, value string) RequestOption

WithHeader option overrides specific header to the request.

func WithJSONContentType added in v0.8.0

func WithJSONContentType() RequestOption

WithJSONContentType option overrides Content-Type header to the request with "application/json" content type.

type ResponseAssertion added in v0.8.0

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

ResponseAssertion is used to build assertions around httptest.ResponseRecorder.

func HandleDELETE

func HandleDELETE(t TestingT, handler http.Handler, url string, options ...RequestOption) *ResponseAssertion

HandleDELETE is an alias for HandleRequest that builds the DELETE request from url and options.

func HandleGET

func HandleGET(t TestingT, handler http.Handler, url string, options ...RequestOption) *ResponseAssertion

HandleGET is an alias for HandleRequest that builds the GET request from url and options.

Example
package main

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/muonsoft/api-testing/apitest"
	"github.com/muonsoft/api-testing/assertjson"
)

func main() {
	handler := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		fmt.Println("request method:", request.Method)
		fmt.Println("request url:", request.URL.String())

		writer.WriteHeader(http.StatusOK)
		writer.Header().Set("Content-Type", "application/json")
		writer.Write([]byte(`{"ok":true}`))
	})

	// HandleGET builds and sends GET request to handler
	response := apitest.HandleGET(&testing.T{}, handler, "/example")

	response.IsOK()
	response.HasContentType("application/json")
	response.HasJSON(func(json *assertjson.AssertJSON) {
		json.Node("/ok").IsTrue()
	})
}
Output:

request method: GET
request url: /example

func HandlePATCH

func HandlePATCH(t TestingT, handler http.Handler, url string, body io.Reader, options ...RequestOption) *ResponseAssertion

HandlePATCH is an alias for HandleRequest that builds the PATCH request from url, body and options.

func HandlePOST

func HandlePOST(t TestingT, handler http.Handler, url string, body io.Reader, options ...RequestOption) *ResponseAssertion

HandlePOST is an alias for HandleRequest that builds the POST request from url, body and options.

func HandlePUT

func HandlePUT(t TestingT, handler http.Handler, url string, body io.Reader, options ...RequestOption) *ResponseAssertion

HandlePUT is an alias for HandleRequest that builds the PUT request from url, body and options.

func HandleRequest

func HandleRequest(t TestingT, handler http.Handler, request *http.Request) *ResponseAssertion

HandleRequest is used to test http.Handler by passing httptest.ResponseRecorder to it. This function returns ResponseAssertion struct as a helper to build assertions on the response.

func (*ResponseAssertion) Code added in v0.8.0

func (r *ResponseAssertion) Code() int

Code returns HTTP status code of the response.

func (*ResponseAssertion) Cookies added in v0.8.0

func (r *ResponseAssertion) Cookies() []*http.Cookie

Cookies returns HTTP cookies of the response.

func (*ResponseAssertion) HasCode added in v0.8.0

func (r *ResponseAssertion) HasCode(code int)

HasCode asserts that the response has specific HTTP status code.

func (*ResponseAssertion) HasContentType added in v0.8.0

func (r *ResponseAssertion) HasContentType(contentType string)

HasContentType asserts that the response contains Content-Type header with specific value.

func (*ResponseAssertion) HasCookie added in v0.8.0

func (r *ResponseAssertion) HasCookie(name string) *CookieAssertion

HasCookie asserts that the response contains specific cookie in Set-Cookie header.

func (*ResponseAssertion) HasHeader added in v0.8.0

func (r *ResponseAssertion) HasHeader(key, value string)

HasHeader asserts that the response contains specific header with key and value.

func (*ResponseAssertion) HasJSON added in v0.8.0

func (r *ResponseAssertion) HasJSON(jsonAssert assertjson.JSONAssertFunc)

HasJSON asserts that the response body contains JSON and runs JSON assertions by callback function.

func (*ResponseAssertion) HasNoContent added in v0.8.0

func (r *ResponseAssertion) HasNoContent()

HasNoContent asserts that the response has an 204 No Content HTTP status code and also checks that body is empty.

func (*ResponseAssertion) HasXML added in v0.8.0

func (r *ResponseAssertion) HasXML(xmlAssert assertxml.XMLAssertFunc)

HasXML asserts that the response body contains XML and runs XML assertions by callback function.

func (*ResponseAssertion) Header added in v0.8.0

func (r *ResponseAssertion) Header() http.Header

Header returns HTTP headers of the response.

func (*ResponseAssertion) IsAccepted added in v0.8.0

func (r *ResponseAssertion) IsAccepted()

IsAccepted asserts that the response has an 202 Accepted HTTP status code.

func (*ResponseAssertion) IsBadGateway added in v0.8.0

func (r *ResponseAssertion) IsBadGateway()

IsBadGateway asserts that the response has an 502 Bad Gateway HTTP status code.

func (*ResponseAssertion) IsBadRequest added in v0.8.0

func (r *ResponseAssertion) IsBadRequest()

IsBadRequest asserts that the response has an 400 Bad Request HTTP status code.

func (*ResponseAssertion) IsConflict added in v0.8.0

func (r *ResponseAssertion) IsConflict()

IsConflict asserts that the response has an 409 Conflict HTTP status code.

func (*ResponseAssertion) IsCreated added in v0.8.0

func (r *ResponseAssertion) IsCreated()

IsCreated asserts that the response has an 201 Created HTTP status code.

func (*ResponseAssertion) IsForbidden added in v0.8.0

func (r *ResponseAssertion) IsForbidden()

IsForbidden asserts that the response has an 403 Forbidden HTTP status code.

func (*ResponseAssertion) IsInternalServerError added in v0.8.0

func (r *ResponseAssertion) IsInternalServerError()

IsInternalServerError asserts that the response has an 500 Internal Server Error HTTP status code.

func (*ResponseAssertion) IsMethodNotAllowed added in v0.8.0

func (r *ResponseAssertion) IsMethodNotAllowed()

IsMethodNotAllowed asserts that the response has an 405 Method Not Allowed HTTP status code.

func (*ResponseAssertion) IsNotFound added in v0.8.0

func (r *ResponseAssertion) IsNotFound()

IsNotFound asserts that the response has an 404 Not Found HTTP status code.

func (*ResponseAssertion) IsOK added in v0.8.0

func (r *ResponseAssertion) IsOK()

IsOK asserts that the response has an 200 Ok HTTP status code.

func (*ResponseAssertion) IsUnauthorized added in v0.8.0

func (r *ResponseAssertion) IsUnauthorized()

IsUnauthorized asserts that the response has an 401 Unauthorized HTTP status code.

func (*ResponseAssertion) IsUnprocessableEntity added in v0.8.0

func (r *ResponseAssertion) IsUnprocessableEntity()

IsUnprocessableEntity asserts that the response has an 422 Unprocessable Entity HTTP status code.

func (*ResponseAssertion) IsUnsupportedMediaType added in v0.8.0

func (r *ResponseAssertion) IsUnsupportedMediaType()

IsUnsupportedMediaType asserts that the response has an 415 Unsupported Media Type HTTP status code.

func (*ResponseAssertion) Print added in v0.8.0

func (r *ResponseAssertion) Print()

Print prints response headers and body to console. Use it for debug purposes.

func (*ResponseAssertion) PrintJSON added in v0.8.0

func (r *ResponseAssertion) PrintJSON()

PrintJSON prints response headers and indented JSON body to console. Use it for debug purposes.

func (*ResponseAssertion) Recorder added in v0.8.0

Recorder returns underlying httptest.ResponseRecorder.

type TestingT added in v0.8.0

type TestingT interface {
	Helper()
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Log(args ...interface{})
}

TestingT is an interface wrapper around *testing.T.

Jump to

Keyboard shortcuts

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