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