or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

error-simulation.mdhttp-methods.mdindex.mdmock-adapter.mdresponses.md
tile.json

http-methods.mddocs/

HTTP Method Mocking

Comprehensive HTTP method mocking with support for all standard HTTP verbs and flexible parameter matching. Each HTTP method has a corresponding on* method that returns a RequestHandler for configuring responses.

Capabilities

GET Method Mocking

Mock GET requests with optional URL and parameter matching.

/**
 * Mock GET requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onGet(matcher?, config?): RequestHandler;

Usage Examples:

// Mock all GET requests
mock.onGet().reply(200);

// Mock specific URL
mock.onGet("/users").reply(200, []);

// Mock with regex
mock.onGet(/\/users\/\d+/).reply(200, { id: 1 });

// Mock with query parameters
mock.onGet("/search", { params: { q: "test" } }).reply(200, []);

// Mock with headers
mock.onGet("/api", { headers: { Authorization: "Bearer token" } }).reply(200);

POST Method Mocking

Mock POST requests with optional URL, body, and parameter matching.

/**
 * Mock POST requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param body - Request body to match (any type)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onPost(matcher?, body?, config?): RequestHandler;

Usage Examples:

// Mock all POST requests
mock.onPost().reply(201);

// Mock specific URL
mock.onPost("/users").reply(201, { id: 1 });

// Mock with specific body
mock.onPost("/users", { name: "John" }).reply(201);

// Mock with body and headers
mock.onPost("/api/users", { name: "John" }, { 
  headers: { "Content-Type": "application/json" } 
}).reply(201);

PUT Method Mocking

Mock PUT requests with optional URL, body, and parameter matching.

/**
 * Mock PUT requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param body - Request body to match (any type)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onPut(matcher?, body?, config?): RequestHandler;

DELETE Method Mocking

Mock DELETE requests with optional URL and parameter matching.

/**
 * Mock DELETE requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onDelete(matcher?, config?): RequestHandler;

PATCH Method Mocking

Mock PATCH requests with optional URL, body, and parameter matching.

/**
 * Mock PATCH requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param body - Request body to match (any type)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onPatch(matcher?, body?, config?): RequestHandler;

HEAD Method Mocking

Mock HEAD requests with optional URL and parameter matching.

/**
 * Mock HEAD requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onHead(matcher?, config?): RequestHandler;

OPTIONS Method Mocking

Mock OPTIONS requests with optional URL and parameter matching.

/**
 * Mock OPTIONS requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onOptions(matcher?, config?): RequestHandler;

LIST Method Mocking

Mock LIST requests with optional URL, body, and parameter matching.

/**
 * Mock LIST requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param body - Request body to match (any type)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onList(matcher?, body?, config?): RequestHandler;

LINK Method Mocking

Mock LINK requests with optional URL, body, and parameter matching.

/**
 * Mock LINK requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param body - Request body to match (any type)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onLink(matcher?, body?, config?): RequestHandler;

UNLINK Method Mocking

Mock UNLINK requests with optional URL, body, and parameter matching.

/**
 * Mock UNLINK requests
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param body - Request body to match (any type)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onUnlink(matcher?, body?, config?): RequestHandler;

Any Method Mocking

Mock requests of any HTTP method with optional URL and parameter matching.

/**
 * Mock requests of any HTTP method
 * @param matcher - URL matcher (string, RegExp, or undefined for any URL)
 * @param config - Optional configuration for params, headers matching
 * @returns RequestHandler for chaining response configuration
 */
onAny(matcher?, config?): RequestHandler;

Usage Examples:

// Mock any method to any URL
mock.onAny().reply(200);

// Mock any method to specific URL
mock.onAny("/api").reply(200);

// Useful for testing request order
const responses = [
  ["GET", "/foo", 200, { foo: "bar" }],
  ["POST", "/bar", 201],
  ["PUT", "/baz", 204],
];

mock.onAny().reply((config) => {
  const [method, url, ...response] = responses.shift();
  if (config.url === url && config.method.toUpperCase() === method) {
    return response;
  }
  return [500, {}];
});

URL Matching

String Matching

Exact string matching with leading slash normalization.

// These are equivalent
mock.onGet("/users").reply(200);
mock.onGet("users").reply(200);

Regex Matching

Use regular expressions for flexible URL matching.

// Match user IDs
mock.onGet(/\/users\/\d+/).reply(200);

// Match with variables
const usersUri = "/users";
const url = new RegExp(`${usersUri}/*`);
mock.onGet(url).reply(200);

No URL Matching

Omit the matcher to match any URL for the specified method.

// Match all GET requests regardless of URL
mock.onGet().reply(200);

Parameter Matching

Query Parameters

Match requests based on query parameters using the params config option.

interface ConfigMatcher {
  params?: object | AsymmetricMatcher;
  headers?: object | AsymmetricMatcher;
  data?: any | AsymmetricMatcher;
}

Usage Examples:

// Exact parameter matching
mock.onGet("/search", { params: { q: "test", limit: 10 } }).reply(200);

// With asymmetric matcher
mock.onGet("/search", { 
  params: { 
    asymmetricMatch: (actual) => actual.q === "test" 
  } 
}).reply(200);

Headers Matching

Match requests based on HTTP headers.

// Exact header matching
mock.onGet("/api", { 
  headers: { "Authorization": "Bearer token" } 
}).reply(200);

// With asymmetric matcher for partial header matching
mock.onPost("/api", {}, { 
  headers: { 
    asymmetricMatch: (actual) => actual.Authorization?.startsWith("Bearer ")
  } 
}).reply(201);

Request Body Matching

For methods that support request bodies (POST, PUT, PATCH, LIST, LINK, UNLINK).

// Exact body matching
mock.onPost("/users", { name: "John", age: 30 }).reply(201);

// JSON string matching
mock.onPost("/users", '{"name":"John"}').reply(201);

// With asymmetric matcher
mock.onPost("/users", {
  asymmetricMatch: (actual) => actual.name === "John"
}).reply(201);

Asymmetric Matchers

Support for Jest-style matchers and custom matching functions.

// Custom asymmetric matcher
mock.onPost("/product", {
  asymmetricMatch: function (actual) {
    return ["computer", "phone"].includes(actual.type);
  }
}).reply(204);

// With Jest matchers (if available)
mock.onPost("/product", { id: 1 }, {
  headers: expect.objectContaining({
    Authorization: expect.stringMatching(/^Basic /)
  })
}).reply(204);

Method Chaining

All on* methods return the AxiosMockAdapter instance, enabling method chaining.

mock
  .onGet("/users").reply(200, users)
  .onPost("/users").reply(201)
  .onGet("/posts").reply(200, posts);