intercomtest

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package intercomtest provides offline test helpers for code using intercom-go.

The package is built around httptest and does not call Intercom. Tests script expected routes, run SDK calls against the local server, and then inspect the captured requests.

Example:

srv := intercomtest.NewServer(t,
	intercomtest.Route("GET", "/me", intercomtest.JSON(200, `{
		"type": "admin",
		"id": "admin-1",
		"email": "admin@example.com"
	}`)),
)

client, err := srv.Client("token")
if err != nil {
	t.Fatal(err)
}

admin, err := client.Admins.Me(context.Background())
if err != nil {
	t.Fatal(err)
}
if admin.Email == nil || *admin.Email != "admin@example.com" {
	t.Fatalf("unexpected admin email: %v", admin.Email)
}

req := srv.Request(t, 0)
if req.Method != "GET" || req.Path != "/me" {
	t.Fatalf("unexpected request: %s %s", req.Method, req.Path)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler describes one expected Intercom request and the response to return.

func Route

func Route(method, path string, response Response) Handler

Route scripts one expected method/path pair and response.

type Request

type Request struct {
	Method   string
	Path     string
	RawQuery string
	Header   http.Header
	Body     []byte
}

Request describes one request captured by Server.

func (Request) JSONBody

func (r Request) JSONBody(t TestingT) map[string]any

JSONBody decodes a captured request body into a map.

type Response

type Response struct {
	StatusCode int
	Header     http.Header
	Body       []byte
}

Response describes an HTTP response returned by a scripted route.

func Bytes

func Bytes(statusCode int, contentType string, body []byte) Response

Bytes creates a binary response with the supplied content type.

func Error

func Error(statusCode int, requestID, code, message string) Response

Error creates an Intercom-style API error response.

func JSON

func JSON(statusCode int, body any) Response

JSON creates a JSON response. String and []byte bodies are written as-is; other values are JSON encoded.

func NoContent

func NoContent(statusCode int) Response

NoContent creates a response with no body.

type Server

type Server struct {
	// URL is the local base URL clients should use.
	URL string
	// contains filtered or unexported fields
}

Server is an offline Intercom API test server.

func NewServer

func NewServer(t TestingT, handlers ...Handler) *Server

NewServer starts an offline Intercom API test server with scripted routes.

func (*Server) Client

func (s *Server) Client(token string, opts ...intercom.Option) (*intercom.Client, error)

Client creates an intercom-go client configured to call this server.

func (*Server) Request

func (s *Server) Request(t TestingT, index int) Request

Request returns a captured request by index.

func (*Server) RequestCount

func (s *Server) RequestCount() int

RequestCount returns the number of captured requests.

func (*Server) Requests

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

Requests returns all captured requests.

type TestingT

type TestingT interface {
	Cleanup(func())
	Errorf(format string, args ...any)
	Fatalf(format string, args ...any)
	Helper()
}

TestingT is the subset of testing.TB used by Server.

Jump to

Keyboard shortcuts

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