testutil

package
v0.7.11 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertSuccess

func AssertSuccess[T any](t *testing.T, result *api.BusinessError) (data T)

AssertSuccess asserts to unmarshal the business error and returns specific type of data.

Types

type Engine

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

Engine provides utilities to create HTTP request for unit tests.

func NewEngine

func NewEngine(engine *gin.Engine, baseURL ...string) *Engine

NewEngine creates a test engine with optional base URL.

func (*Engine) Delete

func (e *Engine) Delete(url string) *Request

Delete creates a DELETE request with given URL.

func (*Engine) Get

func (e *Engine) Get(url string) *Request

Get creates a GET request with given URL.

func (*Engine) Post

func (e *Engine) Post(url string) *Request

Post creates a POST request with given URL.

func (*Engine) Put

func (e *Engine) Put(url string) *Request

Put creates a PUT request with given URL.

func (*Engine) Request

func (e *Engine) Request(method, url string) *Request

Request creates a request with given HTTP method and URL.

type Request

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

Request is used to send HTTP request in unit tests.

func NewRequest

func NewRequest(engine *gin.Engine, method, url string) *Request

NewRequest creates a request with specified method and URL.

Note, query params are set by `WithQueryParams` instead of concatenated with URL.

func (*Request) AssertErrValidation

func (r *Request) AssertErrValidation(t *testing.T)

AssertErrValidation sends HTTP request and asserts api.ErrCodeValidation retrieved.

func (*Request) AssertError

func (r *Request) AssertError(t *testing.T, expectedError *api.BusinessError)

AssertError sends HTTP request and asserts an expected business error.

func (*Request) AssertSuccess

func (r *Request) AssertSuccess(t *testing.T, data ...any)

AssertSuccess sends HTTP request and asserts success with optional data.

func (*Request) AssertSuccessGet

func (r *Request) AssertSuccessGet(t *testing.T, dataPtr any)

AssertSuccessGet sends HTTP request and asserts to unmarshal data with specified `dataPtr`.

func (*Request) MustExec

func (r *Request) MustExec() *api.BusinessError

MustExec sends HTTP request and unmarshal response body to `api.BusinessError`.

func (*Request) MustExecRaw

func (r *Request) MustExecRaw() *httptest.ResponseRecorder

MustExecRaw sends HTTP request and requires 200 OK response.

It panics on any error, or response status code is not 200.

func (*Request) MustWithJSONBody

func (r *Request) MustWithJSONBody(body any) *Request

MustWithJSONBody builds JSON body with content type application/json.

func (*Request) MustWithMultipart

func (r *Request) MustWithMultipart(fields map[string]string, files ...map[string]string) *Request

MustWithMultipart builds form data in request body.

func (*Request) WithAuth

func (r *Request) WithAuth(accessToken string) *Request

WithAuth builds Authorization header with "Bearer xxx".

func (*Request) WithQueryParams

func (r *Request) WithQueryParams(params map[string]string) *Request

WithQueryParams builds query params to concatenate with URL.

This helper is intended for a single build step in test code. If you call it multiple times, the URL will be extended again, so prefer constructing all query params before the first call.

Jump to

Keyboard shortcuts

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