contract

package
v0.0.0-...-047b823 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 15 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 contract holds the spec-driven contract tests (docs/plan/PLAN-v2.md, P10.6). A small client reads the spec a fizz app serves and sends, for each parameter, a request serialized exactly as the document says. The server must bind what was sent. A request that the server rejects, or binds to a different value, is a drift between the document and the runtime.

This file is the serializer: OAS 3.2.1 "Style Values" and "Style Examples" (internal/conformance/oai/orig/oas/versions/3.2.1.md), whose table is the oracle of TestSerializeStyleTable.

Index

Constants

This section is empty.

Variables

View Source
var BadParams = []string{
	"map query", "struct query", "map header",
	"slice header", "array header", "slice header ints", "slice path",
	"option header array", "option path array",
}

BadParams lists the variants of NewBadParam.

View Source
var NoDefaultHeaders = []string{"X-Int", "X-Uint", "X-Num", "X-Bool", "X-Mode", "X-Ptr"}

NoDefaultHeaders lists the headers of NewNoDefaultHeaders that get the warning of P10.7i.

Functions

func New

func New() *fizz.Fizz

New returns the fixture: a fizz app with every parameter shape that fizz documents. It has no map or struct parameter outside deepObject, and no slice header or path parameter (see NewBadParam).

func NewBadParam

func NewBadParam(variant string) *fizz.Fizz

NewBadParam returns an app with one parameter that tonic cannot bind and that the document should not offer: a map or struct outside deepObject (P10.7b), or a slice or array in a header or in the path, which tonic binds from a single value (P10.7e, P10.7l, D29). The variants with an "option" prefix declare the parameter with fizz.Parameter.

func NewByteHeader

func NewByteHeader() *fizz.Fizz

NewByteHeader returns an app with a []byte header, which is a string parameter and not a slice: it is not a generation error.

func NewNoDefaultHeaders

func NewNoDefaultHeaders() *fizz.Fizz

NewNoDefaultHeaders returns an app with optional non-string headers that have no default (P10.7i).

func NewSliceDefault

func NewSliceDefault() *fizz.Fizz

NewSliceDefault returns an app with slice defaults (review finding 4, P10.7c): the default tag is split on commas by tonic, so the document should state [1, 2, 3], and fizz should not record an error.

func Run

func Run(t *testing.T, tg Target)

Run sends, for every operation that has parameters, the requests of the contract:

  • one per parameter, which carries that parameter (and the required ones), serialized as the document says;
  • one that carries the required parameters only;
  • for a parameter with a default, one that omits it (Echo only);
  • one that carries every parameter.

Types

type Doc

type Doc map[string]any

Doc is a served spec, decoded generically.

func (Doc) Coerce

func (d Doc) Coerce(s map[string]any, v any) (any, error)

Coerce converts v into the JSON data model value that a server bound to the Go type documented by s produces once it echoes it: integers and numbers become float64, objects and arrays are converted member by member. It fails when v cannot be a value of the documented type. For example a string where s says array, which is a documented default that a generated client could not use.

func (Doc) ParamSample

func (d Doc) ParamSample(param map[string]any) any

ParamSample returns the sample of a Parameter Object, in the order of the spec's fields: the parameter's example, its examples, then the schema's (see Sample).

func (Doc) Resolve

func (d Doc) Resolve(m map[string]any) map[string]any

Resolve follows the local $ref of m (any number of hops). A schema or parameter without $ref is returned as is.

func (Doc) Sample

func (d Doc) Sample(s map[string]any) any

Sample returns a value for schema s: its example, else examples, else default, else the first enum value, else a value of its type (string "s1", integer 7, number 1.5, boolean true, arrays of two elements, objects built from the properties). A candidate that does not fit the type of the schema is skipped, so a wrong example or default does not hide the type value.

type Object

type Object []Prop

Object is an object sample that keeps the order of its properties, which the spec's style table depends on (R, G, B). A map[string]any is an object too; its properties are serialized in key order.

type Param

type Param struct {
	Name          string
	In            string // query, path, header or cookie
	Style         string // "" means the default of In
	Explode       *bool  // nil means the default of Style
	AllowReserved bool
}

Param is what the serializer needs of an OAS 3.2 Parameter Object.

func ParamOf

func ParamOf(m map[string]any) Param

ParamOf reads a decoded Parameter Object.

type Placement

type Placement struct {
	// Query is a raw query string fragment ("a=1&a=2"), without leading ?.
	Query string
	// Path replaces the {name} template variable, with the leading
	// delimiter of the label and matrix styles.
	Path string
	// Header is the value of the header named after the parameter.
	Header string
	// Cookie is a fragment of the Cookie header ("a=1; a=2").
	Cookie string
}

Placement is where the serialized value goes. Only the field of the parameter's location is set.

func Serialize

func Serialize(p Param, v any) (Placement, error)

Serialize returns the placement of v, a scalar, a []any, a map[string]any or an Object (nil is the undefined value), according to the style, explode and allowReserved of p. It follows RFC 6570 as the spec's "Style Values" section describes: values are percent-encoded except the unreserved characters (and the reserved ones with allowReserved), delimiters are not. The tilde is left alone, which every decoder reads the same.

A combination that the spec table marks n/a is an error.

type Prop

type Prop struct {
	Key   string
	Value any
}

Prop is a property of an Object.

type Target

type Target struct {
	// Handler serves the app.
	Handler http.Handler
	// Spec is the document the app serves.
	Spec []byte
	// Echo tells that the handlers answer 200 with their input as JSON
	// (the bound values). The client then requires a 2xx and that the
	// echoed value of each parameter equals what it sent. Without Echo
	// it only requires that the answer is not a 400.
	Echo bool
	// Tolerate, without Echo, accepts a 400 that the handler's own logic
	// answers (such as "not found"), as opposed to a rejection of the
	// request itself. Optional.
	Tolerate func(status int, body []byte) bool
	// Fields maps "in:name" to the key of the echoed JSON object. The
	// default is the parameter name.
	Fields map[string]string
}

Target is an app under contract test.

Jump to

Keyboard shortcuts

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