openapitest

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 9 Imported by: 0

README

openapitest

import "github.com/brpaz/lib-go/openapitest"

Package openapitest provides utilities for validating HTTP requests and responses against an OpenAPI spec in tests.

Typical usage with a package-level validator initialized in TestMain:

var v *openapitest.Validator

func TestMain(m *testing.M) {
	var err error
	v, err = openapitest.NewValidator("testdata/openapi.yaml")
	if err != nil {
		log.Fatal(err)
	}
	os.Exit(m.Run())
}

func TestSomething(t *testing.T) {
	// ... make request ...
	v.AssertResponse(t, req, resp)
}

Index

type Validator

Validator validates HTTP responses against an OpenAPI spec.

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

func NewValidator
func NewValidator(path string) (*Validator, error)

NewValidator reads the OpenAPI spec at path and returns a Validator.

func NewValidatorFromSpec
func NewValidatorFromSpec(spec []byte) (*Validator, error)

NewValidatorFromSpec parses spec bytes and returns a Validator.

func (*Validator) AssertResponse
func (v *Validator) AssertResponse(t testing.TB, req *http.Request, resp *http.Response)

AssertResponse validates resp against the OpenAPI spec for req. It calls t.Error for every validation failure, allowing the test to continue. The response body is restored after reading so callers can still decode it.

func (*Validator) RequireResponse
func (v *Validator) RequireResponse(t testing.TB, req *http.Request, resp *http.Response)

RequireResponse is like AssertResponse but calls t.Fatal on the first validation failure, stopping the test immediately.

Generated by gomarkdoc

Documentation

Overview

Package openapitest provides utilities for validating HTTP requests and responses against an OpenAPI spec in tests.

Typical usage with a package-level validator initialized in TestMain:

var v *openapitest.Validator

func TestMain(m *testing.M) {
	var err error
	v, err = openapitest.NewValidator("testdata/openapi.yaml")
	if err != nil {
		log.Fatal(err)
	}
	os.Exit(m.Run())
}

func TestSomething(t *testing.T) {
	// ... make request ...
	v.AssertResponse(t, req, resp)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator

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

Validator validates HTTP responses against an OpenAPI spec.

func NewValidator

func NewValidator(path string) (*Validator, error)

NewValidator reads the OpenAPI spec at path and returns a Validator.

func NewValidatorFromSpec

func NewValidatorFromSpec(spec []byte) (*Validator, error)

NewValidatorFromSpec parses spec bytes and returns a Validator.

func (*Validator) AssertResponse

func (v *Validator) AssertResponse(t testing.TB, req *http.Request, resp *http.Response)

AssertResponse validates resp against the OpenAPI spec for req. It calls t.Error for every validation failure, allowing the test to continue. The response body is restored after reading so callers can still decode it.

func (*Validator) RequireResponse

func (v *Validator) RequireResponse(t testing.TB, req *http.Request, resp *http.Response)

RequireResponse is like AssertResponse but calls t.Fatal on the first validation failure, stopping the test immediately.

Jump to

Keyboard shortcuts

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