httptesting

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2023 License: MIT Imports: 7 Imported by: 0

README

HTTP Testing Library

Documentation

Overview

Package httptesting Go HTTP testing library. Simplifies chaining and asserting HTTP REST calls for unit and integration testing

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Httptester

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

Httptester struct for chaining REST calls together Uses the builder pattern for constructing and chaining requests

func New

func New(t util.TestingT, h http.Handler) *Httptester

New returns a new httptester. Create a new httptester for each test for concurrent use

func (*Httptester) AddCookie

func (ht *Httptester) AddCookie(cookie *http.Cookie)

AddCookie adds a cookie to the current request. This cookie will be chained through all subsuquent requests made.

func (*Httptester) AddCookieWithState

func (ht *Httptester) AddCookieWithState(f func(s State) *http.Cookie)

AddCookieWithState adds a cookie to the current request. This cookie will be chained through all subsuquent requests made. Able to use the values from previous requests to create the cookie

func (*Httptester) AddHeader

func (ht *Httptester) AddHeader(key, value string)

AddHeader adds a header to the current request

func (*Httptester) AddHeaderWithState

func (ht *Httptester) AddHeaderWithState(f func(s State) (key, value string))

AddHeaderWithState adds a header to the current request. Able to use the values from previous requests to create a new header

func (*Httptester) AssertBody

func (ht *Httptester) AssertBody(body []byte)

AssertBody asserts the body of the response to the previous request matches the []byte provided

func (*Httptester) AssertCookieDeepEquals

func (ht *Httptester) AssertCookieDeepEquals(expectedCookie *http.Cookie)

AssertCookieDeepEquals asserts that a cookie exists and it deep equals expectedCookie in the response to the previous request

func (*Httptester) AssertCookieExists

func (ht *Httptester) AssertCookieExists(cookieName string)

AssertCookieExists asserts that a cookie exists in the response to the previous request with the name of cookieName

func (*Httptester) AssertCookieValue

func (ht *Httptester) AssertCookieValue(cookieName, expectedValue string)

AssertCookieValue asserts that a cookie exists and its value is expectedValue in the response to the previous request

func (*Httptester) AssertHeader

func (ht *Httptester) AssertHeader(key, expectedValue string)

AssertHeader asserts the headers of the response to the previous request contains the expected key and value

func (*Httptester) AssertStatus

func (ht *Httptester) AssertStatus(expectedStatus string)

AssertStatus asserts the status of the response to the previous request

func (*Httptester) AssertStatusCode

func (ht *Httptester) AssertStatusCode(statusCode int)

AssertStatusCode asserts the status code of the response to the previous request

func (*Httptester) AssertStruct

func (ht *Httptester) AssertStruct(r interface{}, predicate func(responseBody interface{}) bool)

AssertStruct decodes the JSON response body into r and asserts the predicate passed in

func (*Httptester) AssertStructDeepEquals

func (ht *Httptester) AssertStructDeepEquals(r interface{}, expected interface{})

AssertStructDeepEquals decodes the JSON response body into r and asserts r is deeply equatable to expected

func (*Httptester) Delete

func (ht *Httptester) Delete(url string)

Delete creates a new Delete request

func (*Httptester) DeleteWithState

func (ht *Httptester) DeleteWithState(f func(s State) (url string))

DeleteWithState creates a new Delete request Takes a func of the current state and returns the parameters for a NewRequest

func (*Httptester) Execute

func (ht *Httptester) Execute()

Execute executes the current request that was build and resets the state of Response and ResponseResult. This method must be called before any assertions are made.

func (*Httptester) Get

func (ht *Httptester) Get(url string)

Get creates a new Get request

func (*Httptester) GetWithState

func (ht *Httptester) GetWithState(f func(s State) (url string))

GetWithState creates a new Get request. Takes a func of the current state and returns the parameters for a NewRequest

func (*Httptester) NewRequest

func (ht *Httptester) NewRequest(method string, url string, reader io.Reader)

NewRequest creates a new httptester Request the same as http.NewRequest

func (*Httptester) NewRequestWithState

func (ht *Httptester) NewRequestWithState(f func(s State) (method string, url string, reader io.Reader))

NewRequestWithState creates a new httptester Request the same as http.NewRequest. Takes a func of the current state and returns the parameters for a NewRequest

func (*Httptester) Patch

func (ht *Httptester) Patch(url string, reader io.Reader)

Patch creates a new Patch request with a url and request body

func (*Httptester) PatchWithState

func (ht *Httptester) PatchWithState(f func(s State) (url string, reader io.Reader))

PatchWithState creates a new Patch request with a url and request body Takes a func of the current state and returns the parameters for a NewRequest

func (*Httptester) Post

func (ht *Httptester) Post(url string, reader io.Reader)

Post creates a new Post request with a url and request body

func (*Httptester) PostWithState

func (ht *Httptester) PostWithState(f func(s State) (url string, reader io.Reader))

PostWithState creates a new Post request with a url and request body Takes a func of the current state and returns the parameters for a NewRequest

func (*Httptester) Put

func (ht *Httptester) Put(url string, reader io.Reader)

Put creates a new Put request with a url and request body

func (*Httptester) PutWithState

func (ht *Httptester) PutWithState(f func(s State) (url string, reader io.Reader))

PutWithState creates a new Put request with a url and request body Takes a func of the current state and returns the parameters for a NewRequest

func (*Httptester) SetBody

func (ht *Httptester) SetBody(reader io.Reader)

SetBody sets the body of the current request

func (*Httptester) SetBodyWithState

func (ht *Httptester) SetBodyWithState(f func(s State) (reader io.Reader))

SetBodyWithState encodes the struct passed in as JSON and sets the resulting []byte as the request body. Able to use the values from previous request to update the body

func (*Httptester) SetRequestBodyJSON

func (ht *Httptester) SetRequestBodyJSON(body interface{})

SetRequestBodyJSON encodes the struct passed in as JSON and sets the resulting []byte as the request body

func (*Httptester) SetValue

func (ht *Httptester) SetValue(key string, value any)

SetValue sets a value in State to be referenced later

func (*Httptester) SetValueWithState

func (ht *Httptester) SetValueWithState(f func(s State) (key string, value any))

SetValueWithState get access to the current state of the store to set a value to be referenced later

type State

type State struct {
	// Request previous http request made
	Request *http.Request

	// Response previous http response result
	Response *http.Response

	// ResponseResult stores the value of the decoded json body from the response result
	// ResponseResult will be nil until AssertStruct or AssertStructDeepEquals is called
	ResponseResult interface{}

	// Values key-value store to save values needed later in the test
	Values map[string]any
}

State stores the state of the previous request and is used for chaining headers and cookies for each request. Use *WithState functions to get access to the state. Helpful when the server generates an ID or uuid that needs to be chained with another request

Directories

Path Synopsis
internal
util
Package util Utility library for httptesting
Package util Utility library for httptesting

Jump to

Keyboard shortcuts

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