UNPKG

1.68 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2017,2020. 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.createRestAppClient = exports.createClientForHandler = exports.supertest = void 0;
8const tslib_1 = require("tslib");
9/*
10 * HTTP client utilities
11 */
12const http_1 = tslib_1.__importDefault(require("http"));
13const supertest_1 = tslib_1.__importDefault(require("supertest"));
14exports.supertest = supertest_1.default;
15/**
16 * Create a SuperTest client connected to an HTTP server listening
17 * on an ephemeral port and calling `handler` to handle incoming requests.
18 * @param handler
19 */
20function createClientForHandler(handler) {
21 const server = http_1.default.createServer(handler);
22 return (0, supertest_1.default)(server);
23}
24exports.createClientForHandler = createClientForHandler;
25/**
26 * Create a SuperTest client for a running RestApplication instance.
27 * It is the responsibility of the caller to ensure that the app
28 * is running and to stop the application after all tests are done.
29 * @param app - A running (listening) instance of a RestApplication.
30 */
31function createRestAppClient(app) {
32 var _a;
33 const url = (_a = app.restServer.rootUrl) !== null && _a !== void 0 ? _a : app.restServer.url;
34 if (!url) {
35 throw new Error(`Cannot create client for ${app.constructor.name}, it is not listening.`);
36 }
37 return (0, supertest_1.default)(url);
38}
39exports.createRestAppClient = createRestAppClient;
40//# sourceMappingURL=client.js.map
\No newline at end of file