UNPKG

5.03 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2017,2019. All Rights Reserved.
3// Node module: @loopback/testlab
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.stubExpressContext = exports.stubHandlerContext = exports.stubServerResponse = exports.stubServerRequest = exports.inject = void 0;
8const tslib_1 = require("tslib");
9/*
10 * HTTP Request/Response mocks
11 * https://github.com/hapijs/shot
12 */
13/* eslint-disable @typescript-eslint/no-explicit-any */
14const express_1 = tslib_1.__importDefault(require("express"));
15const util_1 = tslib_1.__importDefault(require("util"));
16const inject = require('@hapi/shot');
17exports.inject = inject;
18const ShotRequest = require('@hapi/shot/lib/request');
19function stubServerRequest(options) {
20 const stub = new ShotRequest(options);
21 // Hacky workaround for Express, see
22 // https://github.com/expressjs/express/blob/4.16.3/lib/middleware/init.js
23 // https://github.com/hapijs/shot/issues/82#issuecomment-247943773
24 // https://github.com/jfhbrook/pickleback
25 Object.assign(stub, ShotRequest.prototype);
26 return stub;
27}
28exports.stubServerRequest = stubServerRequest;
29const ShotResponse = require('@hapi/shot/lib/response');
30function stubServerResponse(request, onEnd) {
31 const stub = new ShotResponse(request, onEnd);
32 // Hacky workaround for Express, see
33 // https://github.com/expressjs/express/blob/4.16.3/lib/middleware/init.js
34 // https://github.com/hapijs/shot/issues/82#issuecomment-247943773
35 // https://github.com/jfhbrook/pickleback
36 Object.assign(stub, ShotResponse.prototype);
37 return stub;
38}
39exports.stubServerResponse = stubServerResponse;
40function 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}
50exports.stubHandlerContext = stubHandlerContext;
51function stubExpressContext(requestOptions = { url: '/' }) {
52 const app = (0, express_1.default)();
53 const request = new ShotRequest(requestOptions);
54 // mix in Express Request API
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 // mix in Express Response API
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}
79exports.stubExpressContext = stubExpressContext;
80/**
81 * Use `express.query` to parse the query string into `request.query` object
82 * @param request - Express http request object
83 */
84function parseQuery(request) {
85 // Use `express.query` to parse the query string
86 // See https://github.com/expressjs/express/blob/master/lib/express.js#L79
87 // See https://github.com/expressjs/express/blob/master/lib/middleware/query.js
88 express_1.default.query()(request, {}, () => { });
89}
90function defineCustomContextInspect(context, requestOptions) {
91 // Setup custom inspect functions to make test error messages easier to read
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// @types/node@v10.17.29 seems to miss the type definition of `util.inspect.custom`
101// error TS2339: Property 'custom' does not exist on type 'typeof inspect'.
102// Use a workaround for now to access the `custom` symbol for now.
103// https://nodejs.org/api/util.html#util_util_inspect_custom
104const custom = Symbol.for('nodejs.util.inspect.custom');
105function defineCustomInspect(obj, inspectFn) {
106 obj[custom] = inspectFn;
107}
108//# sourceMappingURL=shot.js.map
\No newline at end of file