or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-9/

Body-Matched Mocking Helper

Create a helper that sets up HTTP mocks keyed off the shape and content of request bodies for a single base URL. The helper should make it easy to prime expectations for different body formats and to verify that every expectation was satisfied after client calls complete. Each mock is primed via the API, exercised by client calls, and then verified through the helper.

Capabilities

Webhook JSON matching

  • After calling mockWebhook("signup", uuidPattern), a POST to /webhook with JSON body {"event":"signup","payload":{"id":"123e4567-e89b-12d3-a456-426614174000","metadata":{"source":"web"}}} returns a 202 response with {"status":"accepted"}; a subsequent POST to the same path with payload.id: "invalid" leaves one expectation pending so verification fails. @test

Metrics NDJSON matching

  • After calling mockMetrics(["http.latency", "db.connections"]), posting an NDJSON string that includes lines for metrics named http.latency and db.connections to /metrics returns 200 with body ok; omitting one of those metric names leaves the expectation unmet during verification. @test

Binary upload matching

  • After calling mockUpload(), posting a binary buffer that begins with the PNG signature (89 50 4E 47 in hex) to /upload returns 201 with body stored; sending plain text to the same path keeps the expectation pending when verified. @test

Verification lifecycle

  • After exercising the client calls, assertComplete() reports any pending expectations and cleanup() removes all mocks so new tests start cleanly. @test

Implementation

@generates

API

/**
 * Creates request body-aware mocks tied to a base URL.
 * @param {string} baseUrl base URL of the external service being mocked.
 * @returns {{
 *   mockWebhook(eventName: string, idPattern: RegExp): void,
 *   mockMetrics(expectedNames: string[]): void,
 *   mockUpload(): void,
 *   assertComplete(): void,
 *   cleanup(): void
 * }}
 */
function createBodyMocker(baseUrl) {}

module.exports = { createBodyMocker };

Dependencies { .dependencies }

nock { .dependency }

Mocks outbound HTTP requests with request body matching support.