testutil

package
v4.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package testutil contains helpers for mocking Chargebee API requests in tests.

Quick usage:

mock := testutil.NewServer()
defer mock.Close()

mock.Enqueue(testutil.MockResponse{
	StatusCode: 200,
	Body:       []byte(`{"customer":{"id":"cust_123"}}`),
})

client := client := mock.NewClient()
// Use client as usual. Requests are routed to the mock server.

You can inspect incoming requests using Requests() / LastRequest(), and you can validate requests per response via MockResponse.Assert.

Index

Constants

View Source
const (
	DefaultSiteName = "test-site"
	DefaultAPIKey   = "test-api-key"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CapturedRequest

type CapturedRequest struct {
	Method string
	Path   string
	Query  url.Values
	Header http.Header
	Body   []byte
}

CapturedRequest stores data from each incoming request.

type MockResponse

type MockResponse struct {
	StatusCode int
	Body       []byte
	Headers    http.Header
	Assert     RequestAssertion
}

MockResponse represents one queued response.

type RequestAssertion

type RequestAssertion func(req *http.Request, body []byte) error

RequestAssertion lets tests validate an incoming request.

type Server

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

Server is an HTTP test server that queues responses and records requests.

func NewServer

func NewServer() *Server

NewServer starts a new mock server instance.

func (*Server) AssertionError

func (s *Server) AssertionError() error

AssertionError returns the first assertion error that occurred.

func (*Server) ClientConfig

func (s *Server) ClientConfig() *chargebee.ClientConfig

ClientConfig returns a Chargebee config wired to use this mock server.

func (*Server) Close

func (s *Server) Close()

Close shuts down the underlying test server.

func (*Server) Enqueue

func (s *Server) Enqueue(res MockResponse)

Enqueue adds a response to be returned for the next request.

func (*Server) EnqueueJSON

func (s *Server) EnqueueJSON(statusCode int, payload any) error

EnqueueJSON marshals payload as JSON and queues it as the next response.

func (*Server) LastRequest

func (s *Server) LastRequest() (CapturedRequest, bool)

LastRequest returns the most recent captured request.

func (*Server) NewClient

func (s *Server) NewClient() *chargebee.Client

NewClient creates a Chargebee client pre-configured to hit this server.

func (*Server) PendingResponses

func (s *Server) PendingResponses() int

PendingResponses returns how many queued responses are left.

func (*Server) Requests

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

Requests returns a snapshot of all captured requests.

func (*Server) URL

func (s *Server) URL() string

URL returns the underlying test server URL.

Jump to

Keyboard shortcuts

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