fixture

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package fixture serves committed site trees over httptest.

Tests never talk to the live network. Each fixture lives under testdata/fixtures/<name>/ with a manifest.json that maps paths to files and optional request conditions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dir

func Dir(name string) (string, error)

Dir returns testdata/fixtures/<name> under the module root.

func Get

func Get(t testing.TB, s *Server, path, ua, accept string, extra http.Header) (int, http.Header, []byte)

Get issues a request against s and returns status, headers, body.

func ModuleRoot

func ModuleRoot() (string, error)

ModuleRoot walks up from the working directory to go.mod.

func ReadFile

func ReadFile(t testing.TB, name, rel string) []byte

ReadFile is a test helper for committed fixture bytes.

Types

type Manifest

type Manifest struct {
	Name  string `json:"name"`
	Pages []Page `json:"pages"`
}

Manifest describes one committed fixture site.

type Page

type Page struct {
	Path        string            `json:"path"`
	File        string            `json:"file"`
	ContentType string            `json:"content_type"`
	Headers     map[string]string `json:"headers,omitempty"`
	When        *When             `json:"when,omitempty"`
	Status      int               `json:"status,omitempty"`
	Redirect    string            `json:"redirect,omitempty"`
}

Page is one path-to-file mapping. Pages are tried in order; the first whose When matches the request wins.

type RequestLog

type RequestLog struct {
	Method    string
	Path      string
	UserAgent string
	Accept    string
	Signature string
	SigInput  string
	SigAgent  string
	Header    http.Header
}

RequestLog is one inbound request, for assertions.

type Server

type Server struct {
	URL    string
	Origin string
	Dir    string
	Name   string
	// contains filtered or unexported fields
}

Server is a local origin serving one fixture tree.

func Serve

func Serve(t testing.TB, name string) *Server

Serve starts fixture <name>. The server is closed via t.Cleanup.

func ServeHook

func ServeHook(t testing.TB, name string, hook func(mux *http.ServeMux)) *Server

ServeHook starts fixture <name> after hook registers extra paths (key directory, for example). Register those before returning; the fixture catch-all is attached after hook.

func (*Server) Close

func (s *Server) Close()

Close shuts the server down. Serve already registers Cleanup.

func (*Server) Requests

func (s *Server) Requests() []RequestLog

Requests returns a copy of the inbound log.

func (*Server) ResetLogs

func (s *Server) ResetLogs()

ResetLogs clears the inbound log.

type When

type When struct {
	UserAgentContains string `json:"user_agent_contains,omitempty"`
	AcceptPrefers     string `json:"accept_prefers,omitempty"`
	HasSignature      *bool  `json:"has_signature,omitempty"`
	Hit               int    `json:"hit,omitempty"`
}

When restricts a page to a class of request. All set fields must match. An empty When matches everything.

Jump to

Keyboard shortcuts

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