testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package testutil provides testing utilities for the QuickBooks Online SDK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadFixture

func LoadFixture(t *testing.T, name string) []byte

LoadFixture loads a fixture file from the testdata directory. The path should be relative to the testdata directory.

func LoadFixtureFrom

func LoadFixtureFrom(t *testing.T, baseDir, name string) []byte

LoadFixtureFrom loads a fixture file from a specified base directory.

func LoadJSONFixture

func LoadJSONFixture(t *testing.T, name string, v any)

LoadJSONFixture loads a JSON fixture and unmarshals it into the provided value.

func MustMarshalJSON

func MustMarshalJSON(t *testing.T, v any) []byte

MustMarshalJSON marshals a value to JSON, failing the test on error.

func MustMarshalJSONIndent

func MustMarshalJSONIndent(t *testing.T, v any) []byte

MustMarshalJSONIndent marshals a value to indented JSON, failing the test on error.

func MustReadFile

func MustReadFile(t *testing.T, path string) []byte

MustReadFile reads a file, failing the test on error.

func TempDir

func TempDir(t *testing.T) string

TempDir creates a temporary directory for tests and returns its path. The directory is automatically cleaned up when the test completes.

func WriteFile

func WriteFile(t *testing.T, dir, name string, data []byte) string

WriteFile writes data to a file in the specified directory.

Types

type MockResponse

type MockResponse struct {
	// Status is the HTTP status code.
	Status int

	// Body is the response body.
	Body string

	// BodyJSON is the response body as a Go value (will be marshaled to JSON).
	// If set, Body is ignored.
	BodyJSON any

	// Headers are additional response headers.
	Headers map[string]string

	// ContentType is the Content-Type header (defaults to "application/json").
	ContentType string
}

MockResponse represents a mock HTTP response.

type MockServer

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

MockServer is a mock HTTP server for testing.

func NewMockServer

func NewMockServer(t *testing.T) *MockServer

NewMockServer creates a new mock server and registers cleanup with t.Cleanup.

func (*MockServer) ClearRequests

func (ms *MockServer) ClearRequests()

ClearRequests clears recorded requests but keeps registered responses.

func (*MockServer) LastRequest

func (ms *MockServer) LastRequest() *RecordedRequest

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

func (*MockServer) Register

func (ms *MockServer) Register(method, path string, resp MockResponse)

Register registers a mock response for a given method and path.

func (*MockServer) RegisterJSON

func (ms *MockServer) RegisterJSON(method, path string, status int, body any)

RegisterJSON registers a JSON response for a given method and path.

func (*MockServer) RequestCount

func (ms *MockServer) RequestCount() int

RequestCount returns the number of requests made to the server.

func (*MockServer) Requests

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

Requests returns all recorded requests.

func (*MockServer) Reset

func (ms *MockServer) Reset()

Reset clears all registered responses and recorded requests.

func (*MockServer) SetDefaultResponse

func (ms *MockServer) SetDefaultResponse(resp MockResponse)

SetDefaultResponse sets the default response for unregistered endpoints.

func (*MockServer) URL

func (ms *MockServer) URL() string

URL returns the mock server URL.

type RecordedRequest

type RecordedRequest struct {
	// Method is the HTTP method.
	Method string

	// Path is the URL path.
	Path string

	// Query is the raw query string.
	Query string

	// Headers are the request headers.
	Headers http.Header

	// Body is the request body.
	Body []byte
}

RecordedRequest represents a request that was made to the mock server.

Jump to

Keyboard shortcuts

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