CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nock

HTTP server mocking and expectations library for Node.js testing environments

67

0.98x
Overview
Eval results
Files

task.mdevals/scenario-4/

Header Mock Harness

A helper that configures mocked profile-service HTTP interactions with strict request header expectations and consistent reply headers for tests.

Capabilities

Profile reads require auth

  • GET /profile without an Authorization bearer token yields a 401 JSON body and still returns the default response headers. @test
  • GET /profile with an Authorization bearer token and Accept: application/json returns the stub profile JSON and default response headers. @test

Default response headers

  • Any mocked response includes X-Env: test, Cache-Control: no-store, a Date header, and a Content-Length matching the body emitted for that mock. @test

Blocks spoofing header

  • Requests containing header X-Spoof return a 403 stub immediately and do not satisfy other mocks. @test

Profile update requires basic auth and echoes trace

  • POST /profile with valid basic auth and JSON body returns 204, echoes X-Trace-Id from the request into the response headers, and applies default headers; missing auth yields 401. @test

Implementation

@generates

API

export interface HeaderMocks {
  mockProfileRead(token: string, profile: Record<string, unknown>): void;
  mockProfileUpdate(credentials: { username: string; password: string }, body: Record<string, unknown>): void;
  blockSpoofing(): void;
  verifyAll(): void;
  reset(): void;
}

export function createHeaderMocks(baseUrl: string): HeaderMocks;

Dependencies { .dependencies }

nock { .dependency }

Mock HTTP requests and responses for Node tests, including header matching and reply header helpers.

Install with Tessl CLI

npx tessl i tessl/npm-nock

tile.json