From d2b4b1627ca548ea22c464d7bb0710aac45f8aa2 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Tue, 2 Oct 2018 06:31:48 +0200 Subject: [PATCH] chore(shot): update to v4 (#29275) --- types/shot/index.d.ts | 12 ++-- types/shot/shot-tests.ts | 20 +++---- types/shot/v3/index.d.ts | 111 ++++++++++++++++++++++++++++++++++++ types/shot/v3/shot-tests.ts | 26 +++++++++ types/shot/v3/tsconfig.json | 28 +++++++++ types/shot/v3/tslint.json | 3 + 6 files changed, 179 insertions(+), 21 deletions(-) create mode 100644 types/shot/v3/index.d.ts create mode 100644 types/shot/v3/shot-tests.ts create mode 100644 types/shot/v3/tsconfig.json create mode 100644 types/shot/v3/tslint.json diff --git a/types/shot/index.d.ts b/types/shot/index.d.ts index 3bf550d094..d0feff29fd 100644 --- a/types/shot/index.d.ts +++ b/types/shot/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for shot 3.4 +// Type definitions for shot 4.0 // Project: https://github.com/hapijs/shot // Definitions by: AJP // Simon Schick @@ -14,10 +14,9 @@ import { Readable, Stream } from "stream"; * Injects a fake request into an HTTP server. * @param dispatchFunc listener function. The same as you would pass to Http.createServer when making a node HTTP server. @see IListener * @param options request options object @see RequestOptions - * @param callback the callback function @see Callback * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} */ -export function inject(dispatchFunc: Listener, options: RequestOptions, callback: (res: ResponseObject) => void): void; +export function inject(dispatchFunc: Listener, options: RequestOptions): Promise; /** * Checks if given object obj is a Shot Request object. @@ -33,23 +32,20 @@ export function isInjection(obj: any): boolean; */ export type Listener = (req: SimulatedRequestObject, res: SimulatedResponseObject) => void; -// disabled for backwards compat -// tslint:disable:no-empty-interface - /** * a simulated request object. Inherits from Stream.Readable. * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} */ +// tslint:disable-next-line:no-empty-interface export interface SimulatedRequestObject extends Readable {} /** * a simulated response object. Inherits from node's Http.ServerResponse. * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} */ +// tslint:disable-next-line:no-empty-interface export interface SimulatedResponseObject extends ServerResponse {} -// tslint:enable:no-empty-interface - /** * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} */ diff --git a/types/shot/shot-tests.ts b/types/shot/shot-tests.ts index 3c5b2f24dd..7d809a8716 100644 --- a/types/shot/shot-tests.ts +++ b/types/shot/shot-tests.ts @@ -2,25 +2,19 @@ // Load modules -import Http = require('http'); -import Shot = require('shot'); +import { IncomingMessage, ServerResponse, createServer } from 'http'; +import { inject } from 'shot'; // Declare internals -const internals: any = {}; - -internals.main = () => { - const dispatch = (req: Http.IncomingMessage, res: Http.ServerResponse) => { +async function main() { + const dispatch = (req: IncomingMessage, res: ServerResponse) => { const reply = 'Hello World'; res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }); res.end(reply); }; - const server = Http.createServer(dispatch); + const server = createServer(dispatch); - Shot.inject(dispatch, { method: 'get', url: '/', headers: { test: 'asd', test2: ['a', 'b'] } }, (res) => { - console.log(res.payload); - }); -}; - -internals.main(); + console.log((await inject(dispatch, { method: 'get', url: '/', headers: { test: 'asd', test2: ['a', 'b'] } })).payload); +} diff --git a/types/shot/v3/index.d.ts b/types/shot/v3/index.d.ts new file mode 100644 index 0000000000..3bf550d094 --- /dev/null +++ b/types/shot/v3/index.d.ts @@ -0,0 +1,111 @@ +// Type definitions for shot 3.4 +// Project: https://github.com/hapijs/shot +// Definitions by: AJP +// Simon Schick +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +import { ServerResponse } from "http"; +import { Readable, Stream } from "stream"; + +/** + * Injects a fake request into an HTTP server. + * @param dispatchFunc listener function. The same as you would pass to Http.createServer when making a node HTTP server. @see IListener + * @param options request options object @see RequestOptions + * @param callback the callback function @see Callback + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ +export function inject(dispatchFunc: Listener, options: RequestOptions, callback: (res: ResponseObject) => void): void; + +/** + * Checks if given object obj is a Shot Request object. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotisinjectionobj} + */ +export function isInjection(obj: any): boolean; + +/** + * listener function. The same as you would pass to Http.createServer when making a node HTTP server. Has the signature function (req, res) where: + * * req - a simulated request object. Inherits from Stream.Readable. + * * res - a simulated response object. Inherits from node's Http.ServerResponse. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ +export type Listener = (req: SimulatedRequestObject, res: SimulatedResponseObject) => void; + +// disabled for backwards compat +// tslint:disable:no-empty-interface + +/** + * a simulated request object. Inherits from Stream.Readable. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ +export interface SimulatedRequestObject extends Readable {} + +/** + * a simulated response object. Inherits from node's Http.ServerResponse. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ +export interface SimulatedResponseObject extends ServerResponse {} + +// tslint:enable:no-empty-interface + +/** + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ +export interface RequestOptions { + /** a string specifying the request URL. */ + url: string; + /** a string specifying the HTTP request method, defaulting to 'GET'. */ + method?: string; + /** 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'. */ + authority?: string; + /** an optional object containing request headers. */ + headers?: Headers; + /** an optional string specifying the client remote address. Defaults to '127.0.0.1'. */ + remoteAddress?: string; + /** an optional request payload. Can be a string, Buffer, Stream or object. */ + payload?: string | Buffer | Stream | object; + /** an object containing flags to simulate various conditions: */ + simulate?: { + /** indicates whether the request will fire an end event. Defaults to undefined, meaning an end event will fire. */ + end?: boolean; + /** indicates whether the request payload will be split into chunks. Defaults to `undefined`, meaning payload will not be chunked. */ + split?: boolean; + /** 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'. */ + error?: boolean; + /** whether the request will emit a close event. Defaults to undefined, meaning no close event will be emitted. */ + close?: boolean; + }; + /** Optional flag to validate this options object. Defaults to true. */ + validate?: boolean; +} + +export interface Headers { + [header: string]: string | string[]; +} + +/** + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ +export interface ResponseObject { + /** an object containing the raw request and response objects where: */ + raw: { + /** the simulated request object. */ + req: SimulatedRequestObject; + /** the simulated response object. */ + res: SimulatedResponseObject; + }; + /** an object containing the response headers. */ + headers: Headers; + /** the HTTP status code. */ + statusCode: number; + /** the HTTP status message. */ + statusMessage: string; + /** the payload as a UTF-8 encoded string. */ + payload: string; + /** the raw payload as a Buffer. */ + rawPayload: Buffer; + /** an object containing the response trailers. */ + trailers: {[index: string]: any}; +} diff --git a/types/shot/v3/shot-tests.ts b/types/shot/v3/shot-tests.ts new file mode 100644 index 0000000000..3c5b2f24dd --- /dev/null +++ b/types/shot/v3/shot-tests.ts @@ -0,0 +1,26 @@ +// From: https://github.com/hapijs/shot#example + +// Load modules + +import Http = require('http'); +import Shot = require('shot'); + +// Declare internals + +const internals: any = {}; + +internals.main = () => { + const dispatch = (req: Http.IncomingMessage, res: Http.ServerResponse) => { + const reply = 'Hello World'; + res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }); + res.end(reply); + }; + + const server = Http.createServer(dispatch); + + Shot.inject(dispatch, { method: 'get', url: '/', headers: { test: 'asd', test2: ['a', 'b'] } }, (res) => { + console.log(res.payload); + }); +}; + +internals.main(); diff --git a/types/shot/v3/tsconfig.json b/types/shot/v3/tsconfig.json new file mode 100644 index 0000000000..1e859cfe17 --- /dev/null +++ b/types/shot/v3/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": false, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "shot": [ + "shot/v3" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "shot-tests.ts" + ] +} diff --git a/types/shot/v3/tslint.json b/types/shot/v3/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/shot/v3/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}