testutils

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package testutils provides testing utilities for the Glean CLI, including mock HTTP transports for injecting test responses into the SDK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupTestConfig

func SetupTestConfig(t *testing.T) func()

SetupTestConfig creates a test configuration and returns a cleanup function

Types

type Fixtures

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

Fixtures provides a way to load test fixtures from files

func NewFixtures

func NewFixtures(t *testing.T, paths ...string) *Fixtures

NewFixtures creates a new Fixtures instance with the given fixture files. The paths should be relative to the test file's location.

func (*Fixtures) Load

func (f *Fixtures) Load(name string) []byte

Load returns the contents of a fixture file by name (without extension)

func (*Fixtures) LoadAsStream

func (f *Fixtures) LoadAsStream(name string) []byte

LoadAsStream returns the contents of a fixture file as a stream of JSON objects. This is useful for API responses that are streamed as newline-delimited JSON. If the file contains a single JSON object, it will be converted to a stream format. If the file contains multiple JSON objects (one per line), they will be preserved.

func (*Fixtures) LoadAsText

func (f *Fixtures) LoadAsText(name string) string

LoadAsText returns the contents of a fixture file as plain text

func (*Fixtures) MustExist

func (f *Fixtures) MustExist(paths ...string) error

MustExist verifies that all specified fixtures exist

func (*Fixtures) WithBasePath

func (f *Fixtures) WithBasePath(path string) *Fixtures

WithBasePath sets a custom base path for fixture files

type MockTransport

type MockTransport struct {
	// Body is returned for every request
	Body []byte
	// Err is returned instead of a response when non-nil
	Err error
	// StatusCode defaults to 200 when zero
	StatusCode int
	// ContentType defaults to "application/json" when empty
	ContentType string
	// Requests records all requests received for inspection
	Requests []*http.Request
}

MockTransport implements http.RoundTripper (the Do method expected by glean.HTTPClient). It returns a predefined response body for every request, making it easy to test command output without making real network calls.

func SetupMockClient

func SetupMockClient(body []byte, err error) (*MockTransport, func())

SetupMockClient injects a MockTransport into the SDK client factory and returns the mock plus a cleanup function that restores the original factory.

func SetupTestWithResponse

func SetupTestWithResponse(t *testing.T, response []byte) (*MockTransport, func())

SetupTestWithResponse sets up both config and mock transport for testing.

func (*MockTransport) Do

func (m *MockTransport) Do(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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