testrunner

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodyAsJson

func BodyAsJson(t *testing.T, res *http.Response, jsonBody any)

BodyAsJson unmarshal the body of a request to json

func BodyAsString

func BodyAsString(t *testing.T, res *http.Response) string

BodyAsString returns the body of a request as string

Types

type HttpTestRunner

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

HttpTestRunner is a runner that facilitates testing of HTTP requests.

It allows for configuring various aspects of the request, such as clearing the body, header, header function, and values.

func NewHttpTestRunner

func NewHttpTestRunner(t *testing.T) *HttpTestRunner

NewHttpTestRunner creates a new HttpTestRunner, equipped with empty headers and default HTTP method as GET.

The root path is also set to its default value '/'.

It's designed to streamline HTTP testing with ease and efficiency.

func (*HttpTestRunner) Clear

func (r *HttpTestRunner) Clear() *HttpTestRunner

Clear resets the HttpTestRunner by setting the body and handler func to nil while clearing the header.

It also resets the clearBody, clearHandlerFunc, and clearHeader flags to false, ensuring a clean slate for future tests.

func (*HttpTestRunner) ClearBodyAfter

func (r *HttpTestRunner) ClearBodyAfter() *HttpTestRunner

ClearBodyAfter ensures that body will be cleared after the HttpTestRunner.Run execution.

func (*HttpTestRunner) ClearHandlerFuncAfter

func (r *HttpTestRunner) ClearHandlerFuncAfter() *HttpTestRunner

ClearHandlerFuncAfter ensures that the handler function to be tested will be cleared after the HttpTestRunner.Run execution.

func (*HttpTestRunner) ClearHeaderAfter

func (r *HttpTestRunner) ClearHeaderAfter() *HttpTestRunner

ClearHeaderAfter ensures that headers will be cleared after the HttpTestRunner.Run execution.

func (*HttpTestRunner) Delete

func (r *HttpTestRunner) Delete() (res *http.Response, err error)

Delete executes an HTTP DELETE request using HttpTestRunner.Run and returns the response and an error if any occurred during the execution.

It will reset to the previous method in case if it wasn't http.MethodDelete.

func (*HttpTestRunner) Get

func (r *HttpTestRunner) Get() (res *http.Response, err error)

Get executes an HTTP GET request using HttpTestRunner.Run and returns the response and an error if any occurred during the execution.

It will reset to the previous method in case if it wasn't http.MethodGet.

func (*HttpTestRunner) Head

func (r *HttpTestRunner) Head() (res *http.Response, err error)

Head executes an HTTP HEAD request using HttpTestRunner.Run and returns the response and an error if any occurred during the execution.

It will reset to the previous method in case if it wasn't http.MethodHead.

func (*HttpTestRunner) Patch

func (r *HttpTestRunner) Patch() (res *http.Response, err error)

Patch executes an HTTP PATCH request using HttpTestRunner.Run and returns the response and an error if any occurred during the execution.

It will reset to the previous method in case if it wasn't http.MethodPatch.

func (*HttpTestRunner) Post

func (r *HttpTestRunner) Post() (res *http.Response, err error)

Post executes an HTTP POST request using HttpTestRunner.Run and returns the response and an error if any occurred during the execution.

It will reset to the previous method in case if it wasn't http.MethodPost.

func (*HttpTestRunner) Put

func (r *HttpTestRunner) Put() (res *http.Response, err error)

Put executes an HTTP PUT request using HttpTestRunner.Run and returns the response and an error if any occurred during the execution.

It will reset to the previous method in case if it wasn't http.MethodPut.

func (*HttpTestRunner) Run

func (r *HttpTestRunner) Run() (res *http.Response, err error)

Run executes the HTTP request method specified by the HttpTestRunner struct and returns the response and an error if any occurred during the execution.

func (*HttpTestRunner) WithBody

func (r *HttpTestRunner) WithBody(body io.Reader) *HttpTestRunner

WithBody set HttpTestRunner.body using an io.Reader

func (*HttpTestRunner) WithHandler

func (r *HttpTestRunner) WithHandler(handler http.Handler) *HttpTestRunner

WithHandler set a handler to be executed by the runner.

If a function is defined, it will bypass this handler.

Use ClearFuncAfter to run the function once and clear it for the next HttpTestRunner.Run execution.

func (*HttpTestRunner) WithHandlerFunc

func (r *HttpTestRunner) WithHandlerFunc(
	handlerFunc func(http.ResponseWriter, *http.Request)) *HttpTestRunner

WithHandlerFunc set a function to be exectued by the runner.

If a function is defined, it will bypass the handler.

Use ClearFuncAfter to run the function once and clear it for the next HttpTestRunner.Run execution.

func (*HttpTestRunner) WithHeader

func (r *HttpTestRunner) WithHeader(key string, value string) *HttpTestRunner

WithHeader add a key/value pair to be added to the header

func (*HttpTestRunner) WithJsonBody

func (r *HttpTestRunner) WithJsonBody(typedBody any) *HttpTestRunner

WithJsonBody set HttpTestRunner.body using an interface

func (*HttpTestRunner) WithMethod

func (r *HttpTestRunner) WithMethod(method string) *HttpTestRunner

WithMethod set the method to be used by the runner

func (*HttpTestRunner) WithPath

func (r *HttpTestRunner) WithPath(path string) *HttpTestRunner

WithPath set the path to be executed by the runner

func (*HttpTestRunner) WithStringBody

func (r *HttpTestRunner) WithStringBody(stringBody string) *HttpTestRunner

WithStringBody set HttpTestRunner.body using a string

func (*HttpTestRunner) WithValues

func (r *HttpTestRunner) WithValues(values url.Values) *HttpTestRunner

WithValues set the url values to be used by the runner

Jump to

Keyboard shortcuts

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