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
- func ApplyMethodOrder(all []*Case, order []string)
- func AuthFindings(s *spec.Spec) []spec.Finding
- func CheckOptions(s *spec.Spec, opt Options) error
- func Name(tag, opID, example string) string
- func NameElem(s string) string
- func PickMedia(content openapi3.Content) (string, *openapi3.MediaType)
- func Selected(op *spec.Operation, opt Options) bool
- type Case
- type Expectation
- type Kind
- type Options
Constants ¶
const ( InvalidTokenExample = "invalid-token" ForbiddenExample = "forbidden" )
Names of the authentication cases.
const ( NotFoundExample = "not-found" ConflictExample = "conflict" )
Names of the generated error cases.
const DefaultExample = "default"
DefaultExample is the name of the case built without named examples.
const RankAuth = 6
RankAuth is the rank of authentication cases of non-DELETE operations: after the 4xx examples, before the DELETEs.
const RankDelete = 7
RankDelete is the rank of DELETE cases, which run last in a group.
const Untagged = spec.Untagged
Untagged is the group of operations without tags.
Variables ¶
This section is empty.
Functions ¶
func ApplyMethodOrder ¶
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 ¶
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 ¶
CheckOptions reports unknown tags or operations in opt (NFR-08).
func NameElem ¶
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.
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 VerifyCase ¶
VerifyCase returns the default case of op, used to read a resource after writing it (FR-VERIFY-01).
func (*Case) ErrorCase ¶
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 ¶
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) 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 ¶
Generated reports whether apitest builds cases of kind k itself instead of taking them from named examples of the spec.
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.