1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.stubExpressContext = exports.stubHandlerContext = exports.stubServerResponse = exports.stubServerRequest = exports.inject = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const express_1 = tslib_1.__importDefault(require("express"));
|
15 | const util_1 = tslib_1.__importDefault(require("util"));
|
16 | const inject = require('@hapi/shot');
|
17 | exports.inject = inject;
|
18 | const ShotRequest = require('@hapi/shot/lib/request');
|
19 | function stubServerRequest(options) {
|
20 | const stub = new ShotRequest(options);
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | Object.assign(stub, ShotRequest.prototype);
|
26 | return stub;
|
27 | }
|
28 | exports.stubServerRequest = stubServerRequest;
|
29 | const ShotResponse = require('@hapi/shot/lib/response');
|
30 | function stubServerResponse(request, onEnd) {
|
31 | const stub = new ShotResponse(request, onEnd);
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | Object.assign(stub, ShotResponse.prototype);
|
37 | return stub;
|
38 | }
|
39 | exports.stubServerResponse = stubServerResponse;
|
40 | function stubHandlerContext(requestOptions = { url: '/' }) {
|
41 | const request = stubServerRequest(requestOptions);
|
42 | let response;
|
43 | const result = new Promise(resolve => {
|
44 | response = new ShotResponse(request, resolve);
|
45 | });
|
46 | const context = { request, response: response, result };
|
47 | defineCustomContextInspect(context, requestOptions);
|
48 | return context;
|
49 | }
|
50 | exports.stubHandlerContext = stubHandlerContext;
|
51 | function stubExpressContext(requestOptions = { url: '/' }) {
|
52 | const app = (0, express_1.default)();
|
53 | const request = new ShotRequest(requestOptions);
|
54 |
|
55 | const RequestApi = express_1.default.request;
|
56 | for (const key of Object.getOwnPropertyNames(RequestApi)) {
|
57 | Object.defineProperty(request, key, Object.getOwnPropertyDescriptor(RequestApi, key));
|
58 | }
|
59 | request.app = app;
|
60 | request.originalUrl = request.url;
|
61 | parseQuery(request);
|
62 | let response;
|
63 | const result = new Promise(resolve => {
|
64 | response = new ShotResponse(request, resolve);
|
65 |
|
66 | Object.assign(response, express_1.default.response);
|
67 | const ResponseApi = express_1.default.response;
|
68 | for (const key of Object.getOwnPropertyNames(ResponseApi)) {
|
69 | Object.defineProperty(response, key, Object.getOwnPropertyDescriptor(ResponseApi, key));
|
70 | }
|
71 | response.app = app;
|
72 | response.req = request;
|
73 | request.res = response;
|
74 | });
|
75 | const context = { app, request, response: response, result };
|
76 | defineCustomContextInspect(context, requestOptions);
|
77 | return context;
|
78 | }
|
79 | exports.stubExpressContext = stubExpressContext;
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function parseQuery(request) {
|
85 |
|
86 |
|
87 |
|
88 | express_1.default.query()(request, {}, () => { });
|
89 | }
|
90 | function defineCustomContextInspect(context, requestOptions) {
|
91 |
|
92 | const inspectOpts = (depth, opts) => util_1.default.inspect(requestOptions, opts);
|
93 | defineCustomInspect(context.request, (depth, opts) => `[RequestStub with options ${inspectOpts(depth, opts)}]`);
|
94 | defineCustomInspect(context.response, (depth, opts) => `[ResponseStub for request with options ${inspectOpts(depth, opts)}]`);
|
95 | context.result = context.result.then(r => {
|
96 | defineCustomInspect(r, (depth, opts) => `[ObservedResponse for request with options ${inspectOpts(depth, opts)}]`);
|
97 | return r;
|
98 | });
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | const custom = Symbol.for('nodejs.util.inspect.custom');
|
105 | function defineCustomInspect(obj, inspectFn) {
|
106 | obj[custom] = inspectFn;
|
107 | }
|
108 |
|
\ | No newline at end of file |