1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import express from 'express';
|
14 | import {IncomingMessage, ServerResponse} from 'http';
|
15 | import {
|
16 | Listener as ShotListener,
|
17 | RequestOptions as ShotRequestOptions,
|
18 | ResponseObject,
|
19 | } from 'shot';
|
20 | import util from 'util';
|
21 |
|
22 | const inject: (
|
23 | dispatchFunc: ShotListener,
|
24 | options: ShotRequestOptions,
|
25 | ) => Promise<ResponseObject> = require('@hapi/shot');
|
26 |
|
27 |
|
28 | export {inject, ShotRequestOptions};
|
29 |
|
30 | const ShotRequest: ShotRequestCtor = require('@hapi/shot/lib/request');
|
31 | type ShotRequestCtor = new (options: ShotRequestOptions) => IncomingMessage;
|
32 |
|
33 | export function stubServerRequest(
|
34 | options: ShotRequestOptions,
|
35 | ): IncomingMessage {
|
36 | const stub = new ShotRequest(options);
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | Object.assign(stub, ShotRequest.prototype);
|
42 | return stub;
|
43 | }
|
44 |
|
45 | const ShotResponse: ShotResponseCtor = require('@hapi/shot/lib/response');
|
46 | export type ShotCallback = (response: ResponseObject) => void;
|
47 |
|
48 | export type ShotResponseCtor = new (
|
49 | request: IncomingMessage,
|
50 | onEnd: ShotCallback,
|
51 | ) => ServerResponse;
|
52 |
|
53 | export function stubServerResponse(
|
54 | request: IncomingMessage,
|
55 | onEnd: ShotCallback,
|
56 | ): ServerResponse {
|
57 | const stub = new ShotResponse(request, onEnd);
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | Object.assign(stub, ShotResponse.prototype);
|
63 | return stub;
|
64 | }
|
65 |
|
66 | export type ObservedResponse = ResponseObject;
|
67 |
|
68 | export interface HandlerContextStub {
|
69 | request: IncomingMessage;
|
70 | response: ServerResponse;
|
71 | result: Promise<ObservedResponse>;
|
72 | }
|
73 |
|
74 | export function stubHandlerContext(
|
75 | requestOptions: ShotRequestOptions = {url: '/'},
|
76 | ): HandlerContextStub {
|
77 | const request = stubServerRequest(requestOptions);
|
78 | let response: ServerResponse | undefined;
|
79 | const result = new Promise<ObservedResponse>(resolve => {
|
80 | response = new ShotResponse(request, resolve);
|
81 | });
|
82 |
|
83 | const context = {request, response: response!, result};
|
84 | defineCustomContextInspect(context, requestOptions);
|
85 | return context;
|
86 | }
|
87 |
|
88 | export interface ExpressContextStub extends HandlerContextStub {
|
89 | app: express.Application;
|
90 | request: express.Request;
|
91 | response: express.Response;
|
92 | result: Promise<ObservedResponse>;
|
93 | }
|
94 |
|
95 | export function stubExpressContext(
|
96 | requestOptions: ShotRequestOptions = {url: '/'},
|
97 | ): ExpressContextStub {
|
98 | const app = express();
|
99 |
|
100 | const request = new ShotRequest(requestOptions) as express.Request;
|
101 |
|
102 | const RequestApi = (express as any).request;
|
103 | for (const key of Object.getOwnPropertyNames(RequestApi)) {
|
104 | Object.defineProperty(
|
105 | request,
|
106 | key,
|
107 | Object.getOwnPropertyDescriptor(RequestApi, key)!,
|
108 | );
|
109 | }
|
110 | request.app = app;
|
111 | request.originalUrl = request.url;
|
112 | parseQuery(request);
|
113 |
|
114 | let response: express.Response | undefined;
|
115 | const result = new Promise<ObservedResponse>(resolve => {
|
116 | response = new ShotResponse(request, resolve) as express.Response;
|
117 |
|
118 | Object.assign(response, (express as any).response);
|
119 | const ResponseApi = (express as any).response;
|
120 | for (const key of Object.getOwnPropertyNames(ResponseApi)) {
|
121 | Object.defineProperty(
|
122 | response,
|
123 | key,
|
124 | Object.getOwnPropertyDescriptor(ResponseApi, key)!,
|
125 | );
|
126 | }
|
127 | response.app = app;
|
128 | (response as any).req = request;
|
129 | (request as any).res = response;
|
130 | });
|
131 |
|
132 | const context = {app, request, response: response!, result};
|
133 | defineCustomContextInspect(context, requestOptions);
|
134 | return context;
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function parseQuery(request: express.Request) {
|
142 |
|
143 |
|
144 |
|
145 | (express as any).query()(request, {}, () => {});
|
146 | }
|
147 |
|
148 | function defineCustomContextInspect(
|
149 | context: HandlerContextStub,
|
150 | requestOptions: ShotRequestOptions,
|
151 | ) {
|
152 |
|
153 | const inspectOpts = (depth: number, opts: any) =>
|
154 | util.inspect(requestOptions, opts);
|
155 |
|
156 | defineCustomInspect(
|
157 | context.request,
|
158 | (depth, opts) => `[RequestStub with options ${inspectOpts(depth, opts)}]`,
|
159 | );
|
160 |
|
161 | defineCustomInspect(
|
162 | context.response,
|
163 | (depth, opts) =>
|
164 | `[ResponseStub for request with options ${inspectOpts(depth, opts)}]`,
|
165 | );
|
166 |
|
167 | context.result = context.result.then(r => {
|
168 | defineCustomInspect(
|
169 | r,
|
170 | (depth, opts) =>
|
171 | `[ObservedResponse for request with options ${inspectOpts(
|
172 | depth,
|
173 | opts,
|
174 | )}]`,
|
175 | );
|
176 | return r;
|
177 | });
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | const custom = Symbol.for('nodejs.util.inspect.custom');
|
185 |
|
186 | function defineCustomInspect(
|
187 | obj: any,
|
188 | inspectFn: (depth: number, opts: any) => {},
|
189 | ) {
|
190 | obj[custom] = inspectFn;
|
191 | }
|