sequence

package
v8.78.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package sequence scripts a mock's answers. A scenario names, per route, the sequence of responses the route gives: the Nth request to the route gets the Nth response and the last one repeats, so a poll loop sees state advance (pending, pending, completed) and a check that never reports stays where it is. The mocks of GitHub, CircleCI and the registry share this dispatcher and add their own headers and defaults on top.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(w http.ResponseWriter, r *http.Request, resp Response) error

Write sends the response: the fixture's headers, Content-Type application/json for a JSON body unless the fixture named one, the status, then the body (no body on a HEAD request, as the protocol demands).

Types

type Request

type Request struct {
	Method string
	Path   string
	Query  url.Values
	Header http.Header
}

Request is one request a mock received.

func (Request) String

func (r Request) String() string

String is "METHOD /path?query", the form of a route key.

type Response

type Response struct {
	// Status is the HTTP status; 0 reads as 200.
	Status int `yaml:"status"`
	// Headers are set on the response. A mock adds its own defaults for the
	// headers a fixture leaves out and never overrides one the fixture sets.
	Headers map[string]string `yaml:"headers"`
	// Body is sent as JSON when it is a mapping or a list, verbatim when it is
	// a string, and as an empty body when it is absent.
	Body any `yaml:"body"`
}

Response is one scripted answer of a route.

func (Response) Bytes

func (r Response) Bytes() ([]byte, error)

Bytes is the body on the wire.

func (Response) Header

func (r Response) Header() http.Header

Header is the fixture's headers as an http.Header, so lookups are case-insensitive.

func (Response) StatusCode

func (r Response) StatusCode() int

StatusCode is the status the response is sent with.

type Routes

type Routes map[string][]Response

Routes maps a route key, "METHOD /path" or "METHOD /path?key=value", to the route's responses. A key with a query matches a request whose query carries every listed parameter with the listed value, whatever else the request sends; a key without a query matches any query. Of several matching routes the one with the most query parameters wins.

type Script

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

Script dispatches requests to routes and records every request it sees.

func New

func New(routes Routes) (*Script, error)

New compiles the routes. A key that is not "METHOD /path[?query]" or a route without responses is an error, so a scenario's typo fails its run.

func (*Script) Next

func (s *Script) Next(r *http.Request) (resp Response, ok bool)

Next records the request and returns the matching route's next response. ok is false when no route matches; the request is recorded all the same.

func (*Script) Record

func (s *Script) Record(r *http.Request)

Record notes a request the mock answers without consulting the script.

func (*Script) Requests

func (s *Script) Requests() []Request

Requests are the requests received so far, in order.

Jump to

Keyboard shortcuts

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