Help us shape the future of Go! Take the 2026 Go Developer Survey

apitest

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package apitest is a fake Tailscale API for tests. It serves the OAuth token endpoint, requires the bearer token it issued, records every request, and answers registered routes. It contains no credential-shaped strings.

Index

Constants

View Source
const (
	ClientID     = "test-client-id"
	ClientSecret = "test-client-secret"
	AccessToken  = "test-access-token"
	Tailnet      = "T1234CNTRL"
	// FedClientID is the federated identity the token-exchange route accepts.
	FedClientID = "test-fed-client-id"
)

Fixed test identities. They are deliberately not shaped like real keys.

View Source
const ValidatePath = "/api/v2/tailnet/" + Tailnet + "/acl/validate"

ValidatePath is the only path tailkeeper may send a request other than GET to: policy validation, which never changes the policy.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fake

type Fake struct {
	Server *httptest.Server
	// Scope is returned in the token response's "scope" field.
	Scope string
	// ExpiresIn is the token lifetime in seconds the OAuth token route grants; 0
	// grants an hour.
	ExpiresIn int
	// NumberTokens makes the OAuth token route issue AccessToken-1, AccessToken-2,
	// ... in turn; API routes then accept only the newest.
	NumberTokens bool
	// contains filtered or unexported fields
}

Fake is a running fake API server.

func New

func New(t testing.TB) *Fake

New starts a fake API and stops it when the test ends.

func (*Fake) AcceptIDToken

func (f *Fake) AcceptIDToken(tok string)

AcceptIDToken makes the token-exchange route accept tok for FedClientID.

func (*Fake) Client

func (f *Fake) Client(t testing.TB) *api.Client

Client returns an api.Client for the fake's tailnet using the fake OAuth client, with retries sleeping zero time.

func (*Fake) ExchangeRequests

func (f *Fake) ExchangeRequests() int

ExchangeRequests returns how many times the token-exchange endpoint was called.

func (*Fake) FailExchanges

func (f *Fake) FailExchanges(statuses ...int)

FailExchanges makes the next exchanges return these statuses, in order.

func (*Fake) Fallback

func (f *Fake) Fallback(h http.HandlerFunc)

Fallback registers a handler for every path that has no route of its own. The request is recorded and its bearer token checked first, as for any other.

func (*Fake) Handle

func (f *Fake) Handle(method, path string, h http.HandlerFunc)

Handle registers a handler for method and escaped path.

func (*Fake) JSON

func (f *Fake) JSON(method, path string, status int, body string)

JSON registers a fixed JSON response.

func (*Fake) Raw

func (f *Fake) Raw(method, path string, status int, body string, headers map[string]string)

Raw registers a fixed response with any body (also empty) and extra headers, such as an ETag or a content type other than JSON.

func (*Fake) Requests

func (f *Fake) Requests() []Request

Requests returns the API requests recorded so far (token requests excluded).

func (*Fake) TokenRequests

func (f *Fake) TokenRequests() int

TokenRequests returns how many times the token endpoint was called.

func (*Fake) URL

func (f *Fake) URL() string

URL is the base URL to pass as api.Options.BaseURL.

type Request

type Request struct {
	Method      string
	Path        string // escaped path, e.g. /api/v2/tailnet/T1234CNTRL/devices
	Query       url.Values
	Auth        string
	Accept      string
	ContentType string
	Body        []byte
}

Request is one recorded request.

Jump to

Keyboard shortcuts

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