requesttest

package
v3.1.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package requesttest provides reusable OpenAPI request-contract helpers for handler tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertCases

func AssertCases(t *testing.T, validator *Validator, cases []Case)

AssertCases validates a table of valid and invalid request samples.

func AssertRecorderResponse

func AssertRecorderResponse(
	t testing.TB,
	validator *Validator,
	req *http.Request,
	recorder *httptest.ResponseRecorder,
	validation ResponseValidation,
)

AssertRecorderResponse validates recorder against the OpenAPI operation that matches req.

func NewFormRequest

func NewFormRequest(method string, target string, body string) *http.Request

NewFormRequest builds an httptest request with a form-urlencoded body.

func NewJSONRequest

func NewJSONRequest(method string, target string, body string) *http.Request

NewJSONRequest builds an httptest request with an application/json body.

func NewRequest

func NewRequest(method string, target string, contentType string, body string) *http.Request

NewRequest builds an httptest request with an optional Content-Type header.

func RequireStringPointer

func RequireStringPointer(t testing.TB, name string, got *string, want string)

RequireStringPointer verifies an optional generated string field.

func RequireStringSlicePointer

func RequireStringSlicePointer(t testing.TB, name string, got *[]string, want []string)

RequireStringSlicePointer verifies an optional generated string slice field.

func RoundTripJSON

func RoundTripJSON(t testing.TB, source any, target any)

RoundTripJSON marshals source and unmarshals it into target for adapter tests that verify two DTOs share the same JSON boundary.

func WithHeaders

func WithHeaders(req *http.Request, values map[string]string) *http.Request

WithHeaders adds headers to req and returns the same request for table setup.

Types

type Case

type Case struct {
	Request                  *http.Request
	Name                     string
	WantErrorContains        string
	ForbiddenErrorSubstrings []string
	WantValid                bool
}

Case describes one request-contract validation sample.

type ClientSmokeDoer

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

ClientSmokeDoer handles generated-client requests without opening a socket.

func NewClientSmokeDoer

func NewClientSmokeDoer(t testing.TB, route ClientSmokeRoute) *ClientSmokeDoer

NewClientSmokeDoer creates a focused in-memory doer for generated-client tests that should verify request construction and response parsing only.

func (*ClientSmokeDoer) Do

func (doer *ClientSmokeDoer) Do(request *http.Request) (*http.Response, error)

Do verifies the generated request and returns the configured response.

type ClientSmokeRoute

type ClientSmokeRoute struct {
	Request      any
	Response     any
	ResponseBody []byte
	Headers      map[string]string
	ContentType  string
	Method       string
	Path         string
	Status       int
}

ClientSmokeRoute describes the single generated-client request accepted by a smoke server.

type ResponseValidation

type ResponseValidation = requestvalidation.ResponseValidation

ResponseValidation configures response-contract checks around a real httptest response.

type Validator

type Validator struct {
	*requestvalidation.Validator
}

Validator wraps the runtime-safe OpenAPI request validator with testing constructors.

func NewIDPValidator

func NewIDPValidator(t testing.TB) *Validator

NewIDPValidator loads the IDP OpenAPI document.

func NewManagementValidator

func NewManagementValidator(t testing.TB) *Validator

NewManagementValidator loads the management OpenAPI document.

func (*Validator) ValidateRecorderResponse

func (validator *Validator) ValidateRecorderResponse(
	req *http.Request,
	recorder *httptest.ResponseRecorder,
	validation ResponseValidation,
) error

ValidateRecorderResponse validates a httptest recorder response against the matching OpenAPI operation.

Jump to

Keyboard shortcuts

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