API mocks

On this page

Segment

A mock compatible with Segment's HTTP Tracking API. Your app sends identify, track, page, screen, group and alias calls as it does to Segment; the mock validates them like Segment, keeps every event and lets your tests ask which events arrived. Nothing is forwarded to destinations.

Testlie is not affiliated with, endorsed by or sponsored by Segment or Twilio. These names are trademarks of their owners, used here only to say which API this mock is compatible with.

Connect

Create a Segment mock under API mocks → Business tools. Use its base URL in place of https://api.segment.io and its write key as your source's write key.

js
import { Analytics } from "@segment/analytics-node";

// The SDK resolves `path` against `host` (new URL(path, host)), so split the base URL.
// <slug> is the 14-character hex part of the mock's base URL (not its id).
const analytics = new Analytics({ writeKey: process.env.SEGMENT_WRITE_KEY, host: "https://testlie.com", path: "/mock/<slug>/v1/batch", flushAt: 1 });
analytics.track({ userId: "user_1", event: "Order Completed", properties: { total: 42 } });

The write key goes in Authorization: Basic base64(writeKey:) or in the body's writeKey, as Segment accepts.

Endpoints

POST /v1/identify, /v1/track, /v1/page, /v1/screen, /v1/group, /v1/alias, /v1/batch and /v1/import, plus the short forms (/v1/i, /v1/t, /v1/p, /v1/s, /v1/g, /v1/a, /v1/b).

Like Segment, the API answers 200 {"success": true} for anything it can parse, even events it then drops, and 400 only for invalid JSON or oversized requests (32 KB per message, 500 KB and 2,500 events per batch). Events the mock drops are kept with the reason, so tests can see why:

  • unknown write key,
  • no userId or anonymousId,
  • track without event, group without groupId, alias without previousId,
  • a messageId already received.

Asserting on events

GET /v1/testlie/events (authenticated with the write key, as above) lists captured events, newest first. Filter with type, event, userId, anonymousId, messageId, status (accepted or dropped) and limit:

js
const res = await fetch(`${BASE}/v1/testlie/events?event=Order%20Completed&userId=user_1`, {
  headers: { Authorization: `Basic ${Buffer.from(`${writeKey}:`).toString("base64")}` },
});
const { events } = await res.json(); // [{ type, event, userId, properties, receivedAt, status, ... }]

The mock's Objects tab shows the same events.

Webhooks

With a webhook URL set, every accepted event is posted there as JSON, like a Segment webhook destination, signed with X-Signature: the hex HMAC-SHA1 of the body with the mock's shared secret.