httpmock

package
v0.1.2 Latest Latest
Warning

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

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

Documentation

Overview

Package httpmock provides HTTP test fakes for the ghapi client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BranchListResponse

func BranchListResponse(pairs ...string) any

BranchListResponse builds a REST list-branches response body. pairs are (name, sha) alternating, e.g. BranchListResponse("main", "abc", "dev", "def"). All entries are marked protected:true so tests model trusted-upstream branches by default; use BranchListResponseProtected to mix protected and unprotected entries.

func BranchListResponseProtected

func BranchListResponseProtected(triples ...any) any

BranchListResponseProtected builds a REST list-branches response body where each entry is a (name, sha, protected) triple.

func CompareAncestorResponse

func CompareAncestorResponse(mergeBaseSHA string) any

CompareAncestorResponse builds a REST compare response indicating the requested sha is an ancestor of the head, with the given merge-base sha.

func TagListResponse

func TagListResponse(pairs ...string) any

TagListResponse builds a REST list-tags response body. pairs are (name, sha) alternating.

Types

type Matcher

type Matcher func(req *http.Request) bool

Matcher determines whether a stub matches an outbound request.

func GraphQL

func GraphQL(q string) Matcher

GraphQL matches a POST GraphQL request whose query matches q.

func GraphQLForRepo

func GraphQLForRepo(owner, repo string) Matcher

GraphQLForRepo matches any GraphQL request whose query or variables reference the given repository owner/name. This is the preferred matcher for queries that pass owner/name through GraphQL `$variables` rather than interpolating them as string literals.

func REST

func REST(method, pathPattern string) Matcher

REST matches a request by method and URL path pattern (regex).

func RESTWithQuery

func RESTWithQuery(method, pathPattern, querySubstring string) Matcher

RESTWithQuery is like REST but also requires querySubstring to appear in the request's raw query string. Use this to distinguish calls that share a URL path but differ by query parameter (e.g. branches?protected=true vs branches?per_page=100).

type Registry

type Registry struct {
	Requests []*http.Request
	// contains filtered or unexported fields
}

Registry records requests and serves stubbed responses. It mirrors the cli/cli pkg/httpmock usage pattern for deterministic API tests.

func (*Registry) Register

func (r *Registry) Register(m Matcher, resp Responder)

Register adds a matcher/response pair to the registry.

func (*Registry) RoundTrip

func (r *Registry) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip satisfies http.RoundTripper.

func (*Registry) Verify

func (r *Registry) Verify(t *testing.T)

Verify fails the test if any registered stubs were never exercised.

type Responder

type Responder func(req *http.Request) (*http.Response, error)

Responder builds an HTTP response for a matched request.

func GraphQLQuery

func GraphQLQuery(body string, cb func(query string, variables map[string]any)) Responder

GraphQLQuery inspects the outbound GraphQL request before returning a body.

func JSONResponse

func JSONResponse(body any) Responder

JSONResponse turns a value into a 200 JSON response.

func StatusResponse

func StatusResponse(code int) Responder

StatusResponse returns a response with the given status code and empty body.

func StatusResponseWithHeaders

func StatusResponseWithHeaders(code int, headers map[string]string) Responder

StatusResponseWithHeaders is StatusResponse with response headers — used by tests that exercise rate-limit fallbacks keyed off Retry-After or X-RateLimit-Reset / X-RateLimit-Remaining.

Jump to

Keyboard shortcuts

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