UNPKG

4.67 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import { ServerResponse } from "http";
4import { Readable, Stream } from "stream";
5
6/**
7 * Injects a fake request into an HTTP server.
8 * @param dispatchFunc listener function. The same as you would pass to Http.createServer when making a node HTTP server. @see IListener
9 * @param options request options object @see RequestOptions
10 * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback}
11 */
12export function inject(dispatchFunc: Listener, options: RequestOptions): Promise<ResponseObject>;
13
14/**
15 * Checks if given object obj is a Shot Request object.
16 * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotisinjectionobj}
17 */
18export function isInjection(obj: any): boolean;
19
20/**
21 * listener function. The same as you would pass to Http.createServer when making a node HTTP server. Has the signature function (req, res) where:
22 * * req - a simulated request object. Inherits from Stream.Readable.
23 * * res - a simulated response object. Inherits from node's Http.ServerResponse.
24 * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback}
25 */
26export type Listener = (req: SimulatedRequestObject, res: SimulatedResponseObject) => void;
27
28/**
29 * a simulated request object. Inherits from Stream.Readable.
30 * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback}
31 */
32// eslint-disable-next-line @typescript-eslint/no-empty-interface
33export interface SimulatedRequestObject extends Readable {}
34
35/**
36 * a simulated response object. Inherits from node's Http.ServerResponse.
37 * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback}
38 */
39// eslint-disable-next-line @typescript-eslint/no-empty-interface
40export interface SimulatedResponseObject extends ServerResponse {}
41
42/**
43 * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback}
44 */
45export interface RequestOptions {
46 /** a string specifying the request URL. */
47 url: string;
48 /** a string specifying the HTTP request method, defaulting to 'GET'. */
49 method?: string | undefined;
50 /** a string specifying the HTTP HOST header value to be used if no header is provided, and the url does not include an authority component. Defaults to 'localhost'. */
51 authority?: string | undefined;
52 /** an optional object containing request headers. */
53 headers?: Headers | undefined;
54 /** an optional string specifying the client remote address. Defaults to '127.0.0.1'. */
55 remoteAddress?: string | undefined;
56 /** an optional request payload. Can be a string, Buffer, Stream or object. */
57 payload?: string | Buffer | Stream | object | undefined;
58 /** an object containing flags to simulate various conditions: */
59 simulate?: {
60 /** indicates whether the request will fire an end event. Defaults to undefined, meaning an end event will fire. */
61 end?: boolean | undefined;
62 /** indicates whether the request payload will be split into chunks. Defaults to `undefined`, meaning payload will not be chunked. */
63 split?: boolean | undefined;
64 /** whether the request will emit an error event. Defaults to undefined, meaning no error event will be emitted. If set to true, the emitted error will have a message of 'Simulated'. */
65 error?: boolean | undefined;
66 /** whether the request will emit a close event. Defaults to undefined, meaning no close event will be emitted. */
67 close?: boolean | undefined;
68 } | undefined;
69 /** Optional flag to validate this options object. Defaults to true. */
70 validate?: boolean | undefined;
71}
72
73export interface Headers {
74 [header: string]: string | string[];
75}
76
77/**
78 * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback}
79 */
80export interface ResponseObject {
81 /** an object containing the raw request and response objects where: */
82 raw: {
83 /** the simulated request object. */
84 req: SimulatedRequestObject;
85 /** the simulated response object. */
86 res: SimulatedResponseObject;
87 };
88 /** an object containing the response headers. */
89 headers: Headers;
90 /** the HTTP status code. */
91 statusCode: number;
92 /** the HTTP status message. */
93 statusMessage: string;
94 /** the payload as a UTF-8 encoded string. */
95 payload: string;
96 /** the raw payload as a Buffer. */
97 rawPayload: Buffer;
98 /** an object containing the response trailers. */
99 trailers: { [index: string]: any };
100}