cases

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cases derives test cases from the examples of an OpenAPI spec (FR-CASE) and orders them within their resource group.

Index

Constants

View Source
const (
	UnauthorizedExample = "unauthorized"
	InvalidTokenExample = "invalid-token"
	ForbiddenExample    = "forbidden"
)

Names of the authentication cases.

View Source
const (
	NotFoundExample = "not-found"
	ConflictExample = "conflict"
)

Names of the generated error cases.

View Source
const DefaultExample = "default"

DefaultExample is the name of the case built without named examples.

View Source
const RankAuth = 6

RankAuth is the rank of authentication cases of non-DELETE operations: after the 4xx examples, before the DELETEs.

View Source
const RankDelete = 7

RankDelete is the rank of DELETE cases, which run last in a group.

View Source
const Untagged = spec.Untagged

Untagged is the group of operations without tags.

Variables

This section is empty.

Functions

func ApplyMethodOrder

func ApplyMethodOrder(all []*Case, order []string)

ApplyMethodOrder re-ranks the regular cases by method (Config.MethodOrder). Listed methods come first in the given order, the others keep their default order after them; negative, authentication and DELETE cases keep their ranks. Within one method the default order stays, so GET by key still runs before the list.

func AuthFindings

func AuthFindings(s *spec.Spec) []spec.Finding

AuthFindings reports operations whose authentication cannot be tested because the spec documents neither 401 nor 403, or x-apitest-forbidden without 403 (FR-CASE-10, FR-CASE-11).

func CheckOptions

func CheckOptions(s *spec.Spec, opt Options) error

CheckOptions reports unknown tags or operations in opt (NFR-08).

func Name

func Name(tag, opID, example string) string

Name builds the stable case name "<Tag>/<operationId>/<example>".

func NameElem

func NameElem(s string) string

NameElem turns one element of a case name (tag, operation ID, example name) into the form that "go test" uses (FR-CASE-09). "/" is replaced by "_" because "go test -run" treats "/" as a level separator.

The rewrite matches testing.rewrite: spaces become "_" and non-printable runes are escaped.

func PickMedia

func PickMedia(content openapi3.Content) (string, *openapi3.MediaType)

PickMedia exposes the media type preference for responses.

func Selected

func Selected(op *spec.Operation, opt Options) bool

Selected reports whether op is selected by opt.

Types

type Case

type Case struct {
	Name    string // "<Tag>/<operationId>/<example>" (FR-CASE-09)
	Group   string // resource group (first tag)
	Example string // example name or "default"
	Op      *spec.Operation
	Kind    Kind
	Rank    int // position class within the group
	// Order is x-apitest-order of the operation; it orders cases of the same
	// rank (FR-ORDER-06).
	Order int
	// ParamExample is the example name used to resolve parameters (rank 3),
	// if it differs from Example (authentication cases reuse the example of
	// the regular case).
	ParamExample string

	MediaType string // request media type, "" without body
	Body      any
	HasBody   bool

	Expect Expectation
	// Compare is the comparison mode from x-apitest-compare, "" if not set.
	Compare string
	// Ignore lists fields from x-apitest-ignore (operation and response).
	Ignore []string
	// Unordered is x-apitest-compare-unordered of the expected response
	// (FR-CMP-07).
	Unordered bool

	// NotFoundParam is the path parameter a NotFound case sends
	// NotFoundValue in, a key no record has.
	NotFoundParam string
	NotFoundValue any

	// Skip is set by x-apitest-skip (FR-CASE-07).
	Skip string
	// NotBuildable explains why the case cannot be sent (FR-CASE-06).
	NotBuildable string
}

Case is one planned request with its expectation.

func Build

func Build(s *spec.Spec, opt Options) ([]*Case, error)

Build derives all cases for the selected operations, in execution order.

func VerifyCase

func VerifyCase(op *spec.Operation) *Case

VerifyCase returns the default case of op, used to read a resource after writing it (FR-VERIFY-01).

func (*Case) ErrorCase

func (c *Case) ErrorCase() bool

ErrorCase reports whether c tests an error answer Config.TolerateErrorCases covers: a generated 404 or 409, a named example that expects 404 or 409, or one that expects 5xx.

func (*Case) ParamSource

func (c *Case) ParamSource() string

ParamSource returns the example name used for named parameter examples.

type Expectation

type Expectation struct {
	Code     string // response key in the spec: "201", "2XX" or "default"
	Status   int    // exact status; 0 if Code is a range or "default"
	Class    int    // status class for ranges: 2 for "2XX"
	Response *openapi3.Response
	// Example is the expected body, if the spec provides one.
	Example    any
	HasExample bool
	// ExampleWhere locates the example in the spec, for the report.
	ExampleWhere string
}

Expectation is what a case expects from the API (FR-CASE-04).

func (Expectation) Matches

func (e Expectation) Matches(status int) bool

Matches reports whether status fulfils the expectation.

func (Expectation) String

func (e Expectation) String() string

String formats the expected status for messages.

type Kind

type Kind int

Kind distinguishes regular cases from negative and authentication tests.

const (
	Positive     Kind = iota
	Negative          // named request example matched to a 4xx response (FR-CASE-05)
	Unauthorized      // sent without token, expects 401 (FR-CASE-08)
	InvalidToken      // sent with a manipulated token, expects 401 or 403 (FR-CASE-10)
	Forbidden         // sent with Config.ForbiddenToken, expects 403 (FR-CASE-11)
	NotFound          // generated: an unknown key in the path, expects 404
	Conflict          // generated: a POST sent a second time, expects 409
	ServerError       // named request example matched to a 5xx response
)

Case kinds.

func (Kind) Generated

func (k Kind) Generated() bool

Generated reports whether apitest builds cases of kind k itself instead of taking them from named examples of the spec.

func (Kind) IsAuth

func (k Kind) IsAuth() bool

IsAuth reports whether k is one of the authentication kinds.

func (Kind) String

func (k Kind) String() string

String names the kind for reports.

type Options

type Options struct {
	Tags       []string // only these tags, in this order; empty = all
	IncludeOps []string // only these operations; empty = all
	ExcludeOps []string
	// ErrorCases adds the generated error cases: not-found (404) and
	// conflict (409), see errorCases.
	ErrorCases bool
}

Options select operations.

Jump to

Keyboard shortcuts

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