cloudflaretest

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 9 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 cloudflaretest is an in-process fake of the slice of the Cloudflare v4 API the cloudflare hosting adapter calls, for tests only. No test in this repository reaches the real service: the adapter and the verb above it are exercised against this server, and every route it answers can be made to fail, so each error path is a test rather than a hope.

The fake answers the documented shapes (the v4 envelope: success, errors, result, result_info) for exactly these routes:

GET  /accounts
GET  /accounts/{account}/workers/workers
POST /accounts/{account}/workers/workers
GET  /accounts/{account}/workers/domains
PUT  /accounts/{account}/workers/domains
GET  /accounts/{account}/workers/subdomain

Anything else is a 404, so a request the adapter was not written to make fails the test that provoked it.

Index

Constants

View Source
const (
	ListAccounts = "GET accounts"
	ListWorkers  = "GET workers"
	CreateWorker = "POST workers"
	ListDomains  = "GET domains"
	AttachDomain = "PUT domains"
	GetSubdomain = "GET subdomain"
)

Route keys name a route for Fail and for the call log.

View Source
const Account = "0123456789abcdef0123456789abcdef"

Account is the one account the default fake holds.

View Source
const Subdomain = "example-sub"

Subdomain is the account's workers.dev subdomain.

View Source
const Token = "cf-fake-token-0000000000000000000000000000"

Token is the credential the fake accepts. Any other bearer is a 403.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	*httptest.Server
	Mu sync.Mutex
	// Accounts the token reaches.
	Accounts []string
	// Workers by name.
	Workers map[string]bool
	// Domains maps a hostname to the Worker it is routed to.
	Domains map[string]string
	// Fail answers a route with this HTTP status instead of serving it.
	Fail map[string]int
	// PageSize splits the Workers list into pages of this size (0 = one page).
	PageSize int
	// Calls is every request received, as its route key.
	Calls []string
	// Bodies holds each write's decoded JSON body, keyed by route.
	Bodies map[string][]map[string]any
	// Auth records every Authorization header value received, so a test can
	// assert the credential went where it should and nowhere else.
	Auth []string
}

Server is the fake. Its fields are the host's state; lock with Mu to read them while a request may be in flight.

func New

func New(t testing.TB) *Server

New starts a fake with one account, no Workers and no domains.

func (*Server) CallLog

func (s *Server) CallLog() []string

CallLog returns a copy of the call log.

func (*Server) Writes

func (s *Server) Writes() int

Writes is how many write requests the fake received.

Jump to

Keyboard shortcuts

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