1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const test = require("tape");
|
4 | const request = require("supertest");
|
5 | const tools_1 = require("./tools");
|
6 | class TestApi {
|
7 | constructor(app) {
|
8 | this.app = app;
|
9 | process.setMaxListeners(Infinity);
|
10 | }
|
11 | test(params) {
|
12 | for (let i = 0; i < params.length; i++) {
|
13 | test(`${i + 1}) ${params[i].description}`, t => {
|
14 | this.__test(t, params[i]);
|
15 | });
|
16 | }
|
17 | }
|
18 | __test(t, params) {
|
19 | const app = this.app;
|
20 | return new Promise((_res, _rej) => {
|
21 | return new Promise((__res, __rej) => {
|
22 | try {
|
23 | if (params.execute && params.execute.then) {
|
24 | return params.execute
|
25 | .then(success => {
|
26 | if (params.showResults && success.success !== false) {
|
27 | console.log('[showResults: ON]:', success);
|
28 | }
|
29 | else if (params.showResults && success.success === false) {
|
30 | __rej(success);
|
31 | t.error(true, success);
|
32 | t.end();
|
33 | return;
|
34 | }
|
35 | __res(true);
|
36 | _res(true);
|
37 | t.error(false, 'No error');
|
38 | t.end();
|
39 | })
|
40 | .catch(err => {
|
41 | __rej(err);
|
42 | t.error(true, err);
|
43 | t.end();
|
44 | });
|
45 | }
|
46 | const expectedCode = params.expectedCode || 200, send = params.send || null, contentType = params.contentType || /json/, showResults = (params.showResults === undefined) ? true : params.showResults;
|
47 | let route = params.route.toString().trim(), method;
|
48 | if (route.indexOf(' ') > -1) {
|
49 | [method, route] = params.route.split(' ');
|
50 | }
|
51 | else {
|
52 | route = params.route;
|
53 | method = (params.method === undefined) ? 'get' : params.method.toString().toLowerCase();
|
54 | }
|
55 | const r = request(app)[method](route);
|
56 | if (send) {
|
57 | r.send(send);
|
58 | }
|
59 | r.expect('Content-Type', contentType);
|
60 | r.expect(expectedCode);
|
61 | r.end((err, res) => {
|
62 | if (err) {
|
63 | t.error(true, res.body.error || err);
|
64 | __rej(err.message);
|
65 | }
|
66 | else {
|
67 | if (showResults) {
|
68 | console.log('[showResults: ON]:', res.body);
|
69 | }
|
70 | if (params.expected && !params.compared) {
|
71 | console.log('comparing with body.data');
|
72 | params.compared = res.body.data;
|
73 | }
|
74 | if (params.compared && params.expected && params.expected.length) {
|
75 | const expected = {};
|
76 | for (let i = 0; i < params.expected.length; i++) {
|
77 | expected[params.expected[i]] = res.body.data[params.expected[i]];
|
78 | }
|
79 | t.same(params.compared, expected, `'${params.context}' info matches as expected`);
|
80 | }
|
81 | else {
|
82 | if ((!res.body.data && !res.body.swagger) ||
|
83 | (Array.isArray(res.body.data) && !res.body.data.length)) {
|
84 | if (!res.body.error && (params.emptyResultExpected || params.failureExpected || send)) {
|
85 | t.error(false, 'No error');
|
86 | }
|
87 | else {
|
88 | if (!res.body.success && params.emptyResultExpected) {
|
89 | t.error(false, 'No error');
|
90 | }
|
91 | else {
|
92 | t.error(true, '[114] Empty array returned');
|
93 | }
|
94 | }
|
95 | }
|
96 | else {
|
97 | if (res.body.swagger || res.body.success || params.failureExpected) {
|
98 | if ((res.body.data && Array.isArray(res.body.data) && !res.body.data.length) || !res.body.data
|
99 | && !params.emptyResultExpected) {
|
100 | t.error(true, '[120] Empty array/value returned');
|
101 | }
|
102 | else {
|
103 | t.error(false, 'No error');
|
104 | }
|
105 | }
|
106 | else {
|
107 | t.error(true, res.body.error);
|
108 | }
|
109 | }
|
110 | }
|
111 | __res(true);
|
112 | _res(res.body);
|
113 | }
|
114 | t.end();
|
115 | });
|
116 | }
|
117 | catch (error) {
|
118 | console.log('138 tools.test.__test()', error);
|
119 | __rej(tools_1.response(false, error.message));
|
120 | }
|
121 | })
|
122 | .catch(errorResponse => {
|
123 | console.log('143 tools.test.__test()', errorResponse);
|
124 | _res(errorResponse);
|
125 | });
|
126 | });
|
127 | }
|
128 | }
|
129 | exports.TestApi = TestApi;
|
130 |
|
\ | No newline at end of file |