testutil

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package testutil provides testing utilities for the Dash0 CLI.

Index

Constants

View Source
const (
	// Dashboards fixtures
	FixtureDashboardsListSuccess   = "dashboards/list_success.json"
	FixtureDashboardsListEmpty     = "dashboards/list_empty.json"
	FixtureDashboardsGetSuccess    = "dashboards/get_success.json"
	FixtureDashboardsImportSuccess = "dashboards/import_success.json"
	FixtureDashboardsNotFound      = "dashboards/error_not_found.json"
	FixtureDashboardsUnauthorized  = "dashboards/error_unauthorized.json"

	// Check rules fixtures
	FixtureCheckRulesListSuccess   = "checkrules/list_success.json"
	FixtureCheckRulesListEmpty     = "checkrules/list_empty.json"
	FixtureCheckRulesGetSuccess    = "checkrules/get_success.json"
	FixtureCheckRulesImportSuccess = "checkrules/import_success.json"
	FixtureCheckRulesNotFound      = "checkrules/error_not_found.json"

	// Views fixtures
	FixtureViewsListSuccess   = "views/list_success.json"
	FixtureViewsListEmpty     = "views/list_empty.json"
	FixtureViewsGetSuccess    = "views/get_success.json"
	FixtureViewsImportSuccess = "views/import_success.json"
	FixtureViewsNotFound      = "views/error_not_found.json"

	// Logs fixtures
	FixtureLogsQuerySuccess = "logs/query_success.json"
	FixtureLogsQueryEmpty   = "logs/query_empty.json"

	// Spans fixtures
	FixtureSpansQuerySuccess = "spans/query_success.json"
	FixtureSpansQueryEmpty   = "spans/query_empty.json"

	// Traces fixtures
	FixtureTracesGetSuccess   = "traces/get_success.json"
	FixtureTracesGetWithLinks = "traces/get_with_links.json"

	// Members fixtures
	FixtureMembersListSuccess  = "members/list_success.json"
	FixtureMembersListEmpty    = "members/list_empty.json"
	FixtureMembersUnauthorized = "members/error_unauthorized.json"

	// Synthetic checks fixtures
	FixtureSyntheticChecksListSuccess   = "syntheticchecks/list_success.json"
	FixtureSyntheticChecksListEmpty     = "syntheticchecks/list_empty.json"
	FixtureSyntheticChecksGetSuccess    = "syntheticchecks/get_success.json"
	FixtureSyntheticChecksImportSuccess = "syntheticchecks/import_success.json"
	FixtureSyntheticChecksNotFound      = "syntheticchecks/error_not_found.json"

	// Teams fixtures
	FixtureTeamsListSuccess   = "teams/list_success.json"
	FixtureTeamsListEmpty     = "teams/list_empty.json"
	FixtureTeamsGetSuccess    = "teams/get_success.json"
	FixtureTeamsCreateSuccess = "teams/create_success.json"
	FixtureTeamsNotFound      = "teams/error_not_found.json"
	FixtureTeamsUnauthorized  = "teams/error_unauthorized.json"
)

Fixture paths relative to the fixtures directory.

Variables

This section is empty.

Functions

func CaptureStdout

func CaptureStdout(t *testing.T, fn func()) string

CaptureStdout captures stdout during the execution of the given function. It returns the captured output as a string.

func FixturesDir

func FixturesDir() string

FixturesDir returns the absolute path to the fixtures directory.

func RequireAuthHeader

func RequireAuthHeader(r *http.Request) error

RequireAuthHeader is a validator that checks for the presence of an Authorization header.

func RequireHeaders added in v1.5.0

func RequireHeaders(r *http.Request) error

RequireHeaders is a validator that checks for both the Authorization and User-Agent headers.

func RequireUserAgentHeader added in v1.5.0

func RequireUserAgentHeader(r *http.Request) error

RequireUserAgentHeader is a validator that checks that the User-Agent header starts with "dash0-cli/".

func SetupTestEnv

func SetupTestEnv(t *testing.T)

SetupTestEnv sets environment variables for testing and returns a cleanup function.

Types

type MockResponse

type MockResponse struct {
	// StatusCode is the HTTP status code to return.
	StatusCode int
	// BodyFile is the path to a fixture file containing the response body.
	// If empty, Body is used instead.
	BodyFile string
	// Body is the response body to return if BodyFile is not set.
	Body interface{}
	// Validator is an optional function to validate the incoming request.
	// If it returns an error, the mock server returns a 400 Bad Request.
	Validator func(r *http.Request) error
}

MockResponse defines how the mock server should respond to a request.

type MockServer

type MockServer struct {
	*httptest.Server
	// contains filtered or unexported fields
}

MockServer is a test HTTP server that serves responses from fixtures.

func NewMockServer

func NewMockServer(t *testing.T, fixturesDir string) *MockServer

NewMockServer creates a new mock server for testing. The fixturesDir parameter specifies the base directory for fixture files.

func (*MockServer) LastRequest

func (m *MockServer) LastRequest() *RecordedRequest

LastRequest returns the most recent request, or nil if no requests were made.

func (*MockServer) On

func (m *MockServer) On(method, path string, resp MockResponse) *MockServer

On registers a mock response for a specific method and path.

func (*MockServer) OnDefault

func (m *MockServer) OnDefault(handler http.HandlerFunc) *MockServer

OnDefault sets a default handler for unmatched requests.

func (*MockServer) OnPattern

func (m *MockServer) OnPattern(method string, pattern *regexp.Regexp, resp MockResponse) *MockServer

OnPattern registers a mock response for requests matching a regex pattern.

func (*MockServer) Requests

func (m *MockServer) Requests() []RecordedRequest

Requests returns all recorded requests.

func (*MockServer) Reset

func (m *MockServer) Reset()

Reset clears all recorded requests.

func (*MockServer) WithCheckRuleImport

func (m *MockServer) WithCheckRuleImport(fixture string) *MockServer

WithCheckRuleImport sets up the mock server to accept check rule imports.

func (*MockServer) WithCheckRulesCreate

func (m *MockServer) WithCheckRulesCreate(fixture string) *MockServer

WithCheckRulesCreate sets up the mock server to accept check rule creation.

func (*MockServer) WithCheckRulesDelete

func (m *MockServer) WithCheckRulesDelete() *MockServer

WithCheckRulesDelete sets up the mock server to accept check rule deletion.

func (*MockServer) WithCheckRulesGet

func (m *MockServer) WithCheckRulesGet(fixture string) *MockServer

WithCheckRulesGet sets up the mock server to return a check rule by ID.

func (*MockServer) WithCheckRulesList

func (m *MockServer) WithCheckRulesList(fixture string) *MockServer

WithCheckRulesList sets up the mock server to return a list of check rules.

func (*MockServer) WithDashboardImport

func (m *MockServer) WithDashboardImport(fixture string) *MockServer

WithDashboardImport sets up the mock server to accept dashboard imports.

func (*MockServer) WithDashboardsCreate

func (m *MockServer) WithDashboardsCreate(fixture string) *MockServer

WithDashboardsCreate sets up the mock server to accept dashboard creation.

func (*MockServer) WithDashboardsDelete

func (m *MockServer) WithDashboardsDelete() *MockServer

WithDashboardsDelete sets up the mock server to accept dashboard deletion.

func (*MockServer) WithDashboardsGet

func (m *MockServer) WithDashboardsGet(fixture string) *MockServer

WithDashboardsGet sets up the mock server to return a dashboard by ID.

func (*MockServer) WithDashboardsList

func (m *MockServer) WithDashboardsList(fixture string) *MockServer

WithDashboardsList sets up the mock server to return a list of dashboards.

func (*MockServer) WithDashboardsUpdate

func (m *MockServer) WithDashboardsUpdate(fixture string) *MockServer

WithDashboardsUpdate sets up the mock server to accept dashboard updates.

func (*MockServer) WithNotFound

func (m *MockServer) WithNotFound(fixture string) *MockServer

WithNotFound sets up any unmatched route to return 404.

func (*MockServer) WithSyntheticCheckImport

func (m *MockServer) WithSyntheticCheckImport(fixture string) *MockServer

WithSyntheticCheckImport sets up the mock server to accept synthetic check imports.

func (*MockServer) WithSyntheticChecksCreate

func (m *MockServer) WithSyntheticChecksCreate(fixture string) *MockServer

WithSyntheticChecksCreate sets up the mock server to accept synthetic check creation.

func (*MockServer) WithSyntheticChecksDelete

func (m *MockServer) WithSyntheticChecksDelete() *MockServer

WithSyntheticChecksDelete sets up the mock server to accept synthetic check deletion.

func (*MockServer) WithSyntheticChecksGet

func (m *MockServer) WithSyntheticChecksGet(fixture string) *MockServer

WithSyntheticChecksGet sets up the mock server to return a synthetic check by ID.

func (*MockServer) WithSyntheticChecksList

func (m *MockServer) WithSyntheticChecksList(fixture string) *MockServer

WithSyntheticChecksList sets up the mock server to return a list of synthetic checks.

func (*MockServer) WithUnauthorized

func (m *MockServer) WithUnauthorized(fixture string) *MockServer

WithUnauthorized sets up the mock server to return 401 Unauthorized.

func (*MockServer) WithViewImport

func (m *MockServer) WithViewImport(fixture string) *MockServer

WithViewImport sets up the mock server to accept view imports.

func (*MockServer) WithViewsCreate

func (m *MockServer) WithViewsCreate(fixture string) *MockServer

WithViewsCreate sets up the mock server to accept view creation.

func (*MockServer) WithViewsDelete

func (m *MockServer) WithViewsDelete() *MockServer

WithViewsDelete sets up the mock server to accept view deletion.

func (*MockServer) WithViewsGet

func (m *MockServer) WithViewsGet(fixture string) *MockServer

WithViewsGet sets up the mock server to return a view by ID.

func (*MockServer) WithViewsList

func (m *MockServer) WithViewsList(fixture string) *MockServer

WithViewsList sets up the mock server to return a list of views.

type RecordedRequest

type RecordedRequest struct {
	Method string
	Path   string
	Query  string
	Body   []byte
	Header http.Header
}

RecordedRequest stores information about a received request.

Jump to

Keyboard shortcuts

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