UNPKG

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