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
- type CapturedRequest
- type MockResponse
- type RequestAssertion
- type Server
- func (s *Server) AssertionError() error
- func (s *Server) ClientConfig() *chargebee.ClientConfig
- func (s *Server) Close()
- func (s *Server) Enqueue(res MockResponse)
- func (s *Server) EnqueueJSON(statusCode int, payload any) error
- func (s *Server) LastRequest() (CapturedRequest, bool)
- func (s *Server) NewClient() *chargebee.Client
- func (s *Server) PendingResponses() int
- func (s *Server) Requests() []CapturedRequest
- func (s *Server) URL() string
Constants ¶
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 ¶
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 (*Server) AssertionError ¶
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) Enqueue ¶
func (s *Server) Enqueue(res MockResponse)
Enqueue adds a response to be returned for the next request.
func (*Server) EnqueueJSON ¶
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) PendingResponses ¶
PendingResponses returns how many queued responses are left.
func (*Server) Requests ¶
func (s *Server) Requests() []CapturedRequest
Requests returns a snapshot of all captured requests.