/** * @module Mockttp */ import MockRuleBuilder from "./rules/mock-rule-builder"; import { ProxyConfig, MockedEndpoint, OngoingRequest } from "./types"; import { MockRuleData, MockRuleCtx } from "./rules/mock-rule-types"; import { CAOptions } from './util/tls'; /** * A mockttp instance allow you to start and stop mock servers and control their behaviour. * * Call `.start()` to set up a server on a random port, use methods like `.get(url)`, * `.post(url)` and `.anyRequest()` to get a {@link MockRuleBuilder} and start defining * mock rules. Call `.stop()` when your test is complete. */ export interface Mockttp { /** * Start a mock server. * * Specify a fixed port if you need one. If you don't, a random port will be chosen, which * you can get later with `.port`, or by using `.url` and `.urlFor(path)` to generate * your URLs automatically. */ start(port?: number): Promise; /** * Stop the mock server and reset the rules. */ stop(): Promise; /** * Enable extra debug output so you can understand exactly what the server is doing. */ enableDebug(): void; /** * Reset the stored rules. Most of the time it's better to start & stop the server instead, * but this can be useful in some special cases. */ reset(): void; /** * The root URL of the server. * * This will throw an error if read before the server is started. */ url: string; /** * The URL for a given path on the server. * * This will throw an error if read before the server is started. */ urlFor(path: string): string; /** * The port the server is running on. * * This will throw an error if read before the server is started. */ port: number; /** * The environment variables typically needed to use this server as a proxy, in a format you * can add to your environment straight away. * * This will throw an error if read before the server is started. * * ``` * process.env = Object.assign(process.env, mockServer.proxyEnv) * ``` */ proxyEnv: ProxyConfig; /** * Get a builder for a mock rule that will match any requests on any path. */ anyRequest(): MockRuleBuilder; /** * Get a builder for a mock rule that will match GET requests for the given path. */ get(url: string, ctx?: MockRuleCtx): MockRuleBuilder; /** * Get a builder for a mock rule that will match POST requests for the given path. */ post(url: string, ctx?: MockRuleCtx): MockRuleBuilder; /** * Get a builder for a mock rule that will match PUT requests for the given path. */ put(url: string, ctx?: MockRuleCtx): MockRuleBuilder; /** * Get a builder for a mock rule that will match DELETE requests for the given path. */ delete(url: string, ctx?: MockRuleCtx): MockRuleBuilder; /** * Get a builder for a mock rule that will match PATCH requests for the given path. */ patch(url: string, ctx?: MockRuleCtx): MockRuleBuilder; /** * Get a builder for a mock rule that will match OPTIONS requests for the given path. * * This can only be used if the `cors` option has been set to false. * * If cors is true (the default when using a remote client, e.g. in the browser), * then the mock server automatically handles OPTIONS requests to ensure requests * to the server are allowed by clients observing CORS rules. * * You can pass `{cors: false}` to `getLocal`/`getRemote` to disable this behaviour, * but if you're testing in a browser you will need to ensure you mock all OPTIONS * requests appropriately so that the browser allows your other requests to be sent. */ options(url: string, ctx?: MockRuleCtx): MockRuleBuilder; /** * Subscribe to hear about request details as they're received. * * This is only useful in some niche use cases, such as logging all requests seen * by the server independently of the rules defined. * * The callback will be called asynchronously from request handling. This function * returns a promise, and the callback is not guaranteed to be registered until * the promise is resolved. */ on(event: 'request', callback: (req: OngoingRequest) => void): Promise; } export interface MockttpOptions { cors?: boolean; debug?: boolean; https?: CAOptions; } /** * @hidden */ export declare abstract class AbstractMockttp { protected cors: boolean; protected debug: boolean; abstract readonly url: string; abstract addRule: (ruleData: MockRuleData) => Promise; abstract on(event: 'request', callback: (req: OngoingRequest) => void): Promise; constructor(options: MockttpOptions); readonly proxyEnv: ProxyConfig; urlFor(path: string): string; anyRequest(): MockRuleBuilder; get(url: string, ctx?: MockRuleCtx): MockRuleBuilder; post(url: string, ctx?: MockRuleCtx): MockRuleBuilder; put(url: string, ctx?: MockRuleCtx): MockRuleBuilder; delete(url: string, ctx?: MockRuleCtx): MockRuleBuilder; patch(url: string, ctx?: MockRuleCtx): MockRuleBuilder; options(url: string, ctx?: MockRuleCtx): MockRuleBuilder; }