Options
All
  • Public
  • Public/Protected
  • All
Menu

A wrapper for Node's HTTP server API that provides convenient semantics for test code.

Do not use this for actual server applications, since its request matching logic is inefficient and its request queue can grow without limit.

    const server = await TestHttpServer.start();

// simulate an endpoint
server.forMethodAndPath('get', '/thing',
TestHttpHandlers.respond(200, {}, 'hello'));

// return 500 error for non-matching requests
server.byDefault(TestHttpHandlers.respond(500));

// ... do some HTTP requests to server.url

const request = await server.nextRequest(); // retrieve the first request
server.close();

Hierarchy

  • TestHttpServer

Index

Constructors

  • new TestHttpServer(secure: boolean, proxy: boolean, options: ServerOptions | ServerOptions, port?: number): TestHttpServer
  • Parameters

    • secure: boolean
    • proxy: boolean
    • options: ServerOptions | ServerOptions
    • Optional port: number

    Returns TestHttpServer

Properties

certificate?: string

The server's self-signed certificate, if it is a secure server.

count: number
defaultHandler: TestHttpHandler
hostname: string

The server's hostname (always "localhost" in this implementation).

matchers: ((req: TestHttpRequest, res: ServerResponse) => boolean)[]
port: number

The server's port.

realServer: Server

An AsyncQueue of all requests handled so far. Call await server.requests.take() to block untiil the server has handled a request.

responses: ServerResponse[]
secure: boolean
url: string

The server's base URL ("http://localhost:8000").

nextPort: number = 8000

Methods

  • Specifies a TestHttpHandler to use for all requests that are not otherwise matched.

    Parameters

    Returns TestHttpServer

    The same server.

  • close(): void
  • Stops the server.

    Returns void

  • closeAndWait(): Promise<void>
  • Stops the server and provides a Promise to indicate when it is completely stopped.

    Returns Promise<void>

  • Specifies a TestHttpHandler to use for requests with the specified method and path. This overrides any previous handler for the same method and path.

    handler

    The request handler. This is normally created with a function like respond.

    Parameters

    • method: string

      The HTTP method.

    • path: string

      The request path.

    • handler: TestHttpHandler

    Returns TestHttpServer

    The same server.

  • Consumes the next received request, waiting until one is available.

    Returns Promise<TestHttpRequest>

    A Promise that will be resolved with a TestHttpRequest.

  • Parameters

    • req: IncomingMessage

    Returns Promise<TestHttpRequest>

  • requestCount(): number
  • Returns the total number of requests that have been received.

    Returns number

    The number of requests so far.

  • startInstance(port?: number): Promise<void>
  • Parameters

    • Optional port: number

    Returns Promise<void>

  • start(options?: ServerOptions, port?: number): Promise<TestHttpServer>
  • Creates and starts a TestHttpServer instance.

    Note: in a non-TypeScript project that uses a transpiler, you may not be able to access this static method; if so, use the same method in TestHttpServers instead.

    Parameters

    • Optional options: ServerOptions

      Any desired [[http.ServerOptions]].

    • Optional port: number

      A specific port to listen on; if omitted, it picks an available port.

    Returns Promise<TestHttpServer>

  • startProxy(options?: ServerOptions, port?: number): Promise<TestHttpServer>
  • Creates and starts a TestHttpServer instance that acts as an HTTP proxy.

    The server will only act as a proxy and will ignore any request handlers that you specify, but it still has the same properties as a regular TestHttpServer, behaves the same in terms of dynamically choosing a port, and allows you to inspect received requests. The received requests will have a path property equal to either the full request URL or, if using a tunneling agent, the request URL minus the path.

    Note that the current implementation does not support proxying a request to an HTTPS URL.

    Parameters

    • Optional options: ServerOptions

      Any desired [[http.ServerOptions]].

    • Optional port: number

      A specific port to listen on; if omitted, it picks an available port.

    Returns Promise<TestHttpServer>

  • startSecure(options?: ServerOptions, port?: number): Promise<TestHttpServer>
  • Creates and starts a TestHttpServer instance that uses HTTPS, with a self-signed certificate.

    Note: in a non-TypeScript project that uses a transpiler, you may not be able to access this static method; if so, use the same method in TestHttpServers instead.

    Parameters

    • Optional options: ServerOptions

      Any desired [[https.ServerOptions]] other than the certificate.

    • Optional port: number

      A specific port to listen on; if omitted, it picks an available port.

    Returns Promise<TestHttpServer>

  • startSecureInternal(options: ServerOptions, proxy: boolean, port?: number): Promise<TestHttpServer>
  • Parameters

    • options: ServerOptions
    • proxy: boolean
    • Optional port: number

    Returns Promise<TestHttpServer>

  • startSecureProxy(options?: ServerOptions, port?: number): Promise<TestHttpServer>
  • Creates and starts a TestHttpServer instance that acts as a secure HTTP proxy with a self-signed certificate.

    This is the same as TestHttpServer.startProxy, but the proxy server itself is secure. Note that the current implementation does not support proxying a request to an HTTPS URL (that is, when the target server is itself secure).

    Parameters

    • Optional options: ServerOptions

      Any desired [[https.ServerOptions]] other than the certificate.

    • Optional port: number

      A specific port to listen on; if omitted, it picks an available port.

    Returns Promise<TestHttpServer>

Generated using TypeDoc