@villedemontreal/http-request 7.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +216 -0
- package/dist/src/config/configs.d.ts +38 -0
- package/dist/src/config/configs.js +63 -0
- package/dist/src/config/configs.js.map +1 -0
- package/dist/src/config/constants.d.ts +40 -0
- package/dist/src/config/constants.js +40 -0
- package/dist/src/config/constants.js.map +1 -0
- package/dist/src/config/init.d.ts +22 -0
- package/dist/src/config/init.js +50 -0
- package/dist/src/config/init.js.map +1 -0
- package/dist/src/httpUtils.d.ts +131 -0
- package/dist/src/httpUtils.js +363 -0
- package/dist/src/httpUtils.js.map +1 -0
- package/dist/src/httpUtils.test.d.ts +1 -0
- package/dist/src/httpUtils.test.js +628 -0
- package/dist/src/httpUtils.test.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +24 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/utils/logger.d.ts +11 -0
- package/dist/src/utils/logger.js +54 -0
- package/dist/src/utils/logger.js.map +1 -0
- package/dist/src/utils/testingConfigurations.d.ts +9 -0
- package/dist/src/utils/testingConfigurations.js +18 -0
- package/dist/src/utils/testingConfigurations.js.map +1 -0
- package/package.json +67 -0
- package/src/config/configs.ts +74 -0
- package/src/config/constants.ts +54 -0
- package/src/config/init.ts +55 -0
- package/src/httpUtils.test.ts +764 -0
- package/src/httpUtils.ts +405 -0
- package/src/index.ts +8 -0
- package/src/utils/logger.ts +53 -0
- package/src/utils/testingConfigurations.ts +14 -0
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Ok in test files :
|
|
3
|
+
// tslint:disable:no-string-literal
|
|
4
|
+
// tslint:disable: max-func-body-length
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const src_1 = require("@villedemontreal/general-utils/dist/src");
|
|
7
|
+
const chai_1 = require("chai");
|
|
8
|
+
const express = require("express");
|
|
9
|
+
const http_header_fields_typed_1 = require("http-header-fields-typed");
|
|
10
|
+
const HttpStatusCodes = require("http-status-codes");
|
|
11
|
+
const _ = require("lodash");
|
|
12
|
+
const superagent = require("superagent");
|
|
13
|
+
const configs_1 = require("./config/configs");
|
|
14
|
+
const constants_1 = require("./config/constants");
|
|
15
|
+
const httpUtils_1 = require("./httpUtils");
|
|
16
|
+
const testingConfigurations_1 = require("./utils/testingConfigurations");
|
|
17
|
+
const superagentMocker = require('superagent-mocker');
|
|
18
|
+
// ==========================================
|
|
19
|
+
// Set Testing configurations
|
|
20
|
+
// ==========================================
|
|
21
|
+
(0, testingConfigurations_1.setTestingConfigurations)();
|
|
22
|
+
describe('httpUtils', () => {
|
|
23
|
+
describe('urlJoin', () => {
|
|
24
|
+
it('single param with slash', async () => {
|
|
25
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com/'), 'http://google.com');
|
|
26
|
+
});
|
|
27
|
+
it('single param no slash', async () => {
|
|
28
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com'), 'http://google.com');
|
|
29
|
+
});
|
|
30
|
+
it('with slashes', async () => {
|
|
31
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com/', 'foo/', '/bar/'), 'http://google.com/foo/bar');
|
|
32
|
+
});
|
|
33
|
+
it('without slashes', async () => {
|
|
34
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com', 'foo', 'bar'), 'http://google.com/foo/bar');
|
|
35
|
+
});
|
|
36
|
+
it('without double slashes', async () => {
|
|
37
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com/', '//foo/', '//bar/'), 'http://google.com/foo/bar');
|
|
38
|
+
});
|
|
39
|
+
it('with slashes without text', async () => {
|
|
40
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com/', '///', '//bar/'), 'http://google.com/bar');
|
|
41
|
+
});
|
|
42
|
+
it('with slashes and empty text', async () => {
|
|
43
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com/', '', '//bar/'), 'http://google.com/bar');
|
|
44
|
+
});
|
|
45
|
+
it('with slashes and null text', async () => {
|
|
46
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com/', null, '//bar/'), 'http://google.com/bar');
|
|
47
|
+
});
|
|
48
|
+
it('with slashes and undefined text', async () => {
|
|
49
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://google.com/', undefined, '//bar/'), 'http://google.com/bar');
|
|
50
|
+
});
|
|
51
|
+
it('with http 2 slashes', async () => {
|
|
52
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://', 'google.com', 'foo', 'bar'), 'http://google.com/foo/bar');
|
|
53
|
+
});
|
|
54
|
+
it('with http 1 slash', async () => {
|
|
55
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http:/', 'google.com', 'foo', 'bar'), 'http://google.com/foo/bar');
|
|
56
|
+
});
|
|
57
|
+
it('with http no slash', async () => {
|
|
58
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http:', 'google.com', 'foo', 'bar'), 'http://google.com/foo/bar');
|
|
59
|
+
});
|
|
60
|
+
it('another example', async () => {
|
|
61
|
+
chai_1.assert.equal(httpUtils_1.httpUtils.urlJoin('http://api.montreal.ca/accounts/', '/inum', '@5441521452', 'tickets'), 'http://api.montreal.ca/accounts/inum/@5441521452/tickets');
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
describe('send', () => {
|
|
65
|
+
describe('mocked', () => {
|
|
66
|
+
let mock;
|
|
67
|
+
before(async () => {
|
|
68
|
+
mock = superagentMocker(superagent);
|
|
69
|
+
mock.get('http://localhost/test', (req) => {
|
|
70
|
+
return {
|
|
71
|
+
body: {
|
|
72
|
+
headers: req.headers
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
after(async () => {
|
|
78
|
+
mock.clearRoutes();
|
|
79
|
+
mock.unmock(superagent);
|
|
80
|
+
});
|
|
81
|
+
it('URL must have a hostname', async () => {
|
|
82
|
+
const request = superagent.get('/test');
|
|
83
|
+
try {
|
|
84
|
+
await httpUtils_1.httpUtils.send(request);
|
|
85
|
+
chai_1.assert.fail('expected send to throw an error');
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
chai_1.assert.strictEqual(err.message, 'The URL in your request MUST have a protocol and a hostname. Received: /test');
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
it('The Correlation Id is set automatically', async () => {
|
|
92
|
+
const currentCid = configs_1.configs.correlationId;
|
|
93
|
+
const request = superagent.get('http://localhost/test').set('titi', '123');
|
|
94
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
95
|
+
chai_1.assert.isOk(response);
|
|
96
|
+
chai_1.assert.isOk(response.status);
|
|
97
|
+
chai_1.assert.strictEqual(response.status, 200);
|
|
98
|
+
chai_1.assert.isObject(response.body);
|
|
99
|
+
chai_1.assert.isObject(response.body.headers);
|
|
100
|
+
const headers = response.body.headers;
|
|
101
|
+
chai_1.assert.strictEqual(headers.titi, '123');
|
|
102
|
+
chai_1.assert.strictEqual(headers[http_header_fields_typed_1.default.X_CORRELATION_ID.toLowerCase()], currentCid);
|
|
103
|
+
});
|
|
104
|
+
it('Regular response response', async () => {
|
|
105
|
+
for (const status of [200, 201, 301, 400, 404, 500, 501]) {
|
|
106
|
+
mock.get('http://localhost/test', (req) => {
|
|
107
|
+
return {
|
|
108
|
+
status,
|
|
109
|
+
body: {
|
|
110
|
+
msg: 'titi'
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
const request = superagent.get('http://localhost/test');
|
|
115
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
116
|
+
chai_1.assert.isOk(response);
|
|
117
|
+
chai_1.assert.isOk(response.status);
|
|
118
|
+
chai_1.assert.strictEqual(response.status, status);
|
|
119
|
+
chai_1.assert.isObject(response.body);
|
|
120
|
+
chai_1.assert.strictEqual(response.body.msg, 'titi');
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
it('Timeouts are added, if not already set', async () => {
|
|
124
|
+
const request = superagent.get('http://localhost/test').set('titi', '123');
|
|
125
|
+
chai_1.assert.isUndefined(request['_responseTimeout']);
|
|
126
|
+
chai_1.assert.isUndefined(request['_timeout']);
|
|
127
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
128
|
+
chai_1.assert.isOk(response);
|
|
129
|
+
chai_1.assert.strictEqual(request['_responseTimeout'], constants_1.constants.request.timeoutsDefault.response);
|
|
130
|
+
chai_1.assert.strictEqual(request['_timeout'], constants_1.constants.request.timeoutsDefault.deadline);
|
|
131
|
+
});
|
|
132
|
+
it('Response timeout already set', async () => {
|
|
133
|
+
const request = superagent.get('http://localhost/test').set('titi', '123');
|
|
134
|
+
request.timeout({
|
|
135
|
+
response: 55555
|
|
136
|
+
});
|
|
137
|
+
chai_1.assert.strictEqual(request['_responseTimeout'], 55555);
|
|
138
|
+
chai_1.assert.isUndefined(request['_timeout']);
|
|
139
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
140
|
+
chai_1.assert.isOk(response);
|
|
141
|
+
chai_1.assert.strictEqual(request['_responseTimeout'], 55555);
|
|
142
|
+
chai_1.assert.strictEqual(request['_timeout'], constants_1.constants.request.timeoutsDefault.deadline);
|
|
143
|
+
});
|
|
144
|
+
it('Deadline timeout already set', async () => {
|
|
145
|
+
const request = superagent.get('http://localhost/test').set('titi', '123');
|
|
146
|
+
request.timeout({
|
|
147
|
+
deadline: 55555
|
|
148
|
+
});
|
|
149
|
+
chai_1.assert.isUndefined(request['_responseTimeout']);
|
|
150
|
+
chai_1.assert.strictEqual(request['_timeout'], 55555);
|
|
151
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
152
|
+
chai_1.assert.isOk(response);
|
|
153
|
+
chai_1.assert.strictEqual(request['_responseTimeout'], constants_1.constants.request.timeoutsDefault.response);
|
|
154
|
+
chai_1.assert.strictEqual(request['_timeout'], 55555);
|
|
155
|
+
});
|
|
156
|
+
it('Both timeouts timeout already set', async () => {
|
|
157
|
+
const request = superagent.get('http://localhost/test').set('titi', '123');
|
|
158
|
+
request.timeout({
|
|
159
|
+
deadline: 55555,
|
|
160
|
+
response: 66666
|
|
161
|
+
});
|
|
162
|
+
chai_1.assert.strictEqual(request['_responseTimeout'], 66666);
|
|
163
|
+
chai_1.assert.strictEqual(request['_timeout'], 55555);
|
|
164
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
165
|
+
chai_1.assert.isOk(response);
|
|
166
|
+
chai_1.assert.strictEqual(request['_responseTimeout'], 66666);
|
|
167
|
+
chai_1.assert.strictEqual(request['_timeout'], 55555);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
describe('Network/Server error', () => {
|
|
171
|
+
it('Network/Server error', async () => {
|
|
172
|
+
const mock = superagentMocker(superagent);
|
|
173
|
+
mock.get('http://localhost/test', (req) => {
|
|
174
|
+
throw new Error('Network error');
|
|
175
|
+
});
|
|
176
|
+
try {
|
|
177
|
+
const request = superagent.get('http://localhost/test');
|
|
178
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
179
|
+
chai_1.assert.isNotOk(response);
|
|
180
|
+
chai_1.assert.fail();
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
/* ok */
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
describe('not mocked', () => {
|
|
188
|
+
it('Errors are handled properly', async () => {
|
|
189
|
+
try {
|
|
190
|
+
const request = superagent.get('httttp://nope').timeout(100);
|
|
191
|
+
const response = await httpUtils_1.httpUtils.send(request);
|
|
192
|
+
chai_1.assert.isNotOk(response);
|
|
193
|
+
chai_1.assert.fail();
|
|
194
|
+
}
|
|
195
|
+
catch (err) {
|
|
196
|
+
chai_1.assert.isObject(err);
|
|
197
|
+
chai_1.assert.isTrue('msg' in err);
|
|
198
|
+
chai_1.assert.isTrue('originalError' in err);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
describe(`Express request related tests`, () => {
|
|
204
|
+
let app;
|
|
205
|
+
let server;
|
|
206
|
+
let port;
|
|
207
|
+
let expressRequest;
|
|
208
|
+
async function startServer(caseSensitive) {
|
|
209
|
+
app = express();
|
|
210
|
+
app.set('case sensitive routing', caseSensitive);
|
|
211
|
+
app.get('/', async (req, res, next) => {
|
|
212
|
+
expressRequest = req;
|
|
213
|
+
res.sendStatus(HttpStatusCodes.OK);
|
|
214
|
+
});
|
|
215
|
+
port = await src_1.utils.findFreePort();
|
|
216
|
+
server = await app.listen(port);
|
|
217
|
+
}
|
|
218
|
+
async function send(pathAndQueryString) {
|
|
219
|
+
const superagentRequest = superagent.get(`http://localhost:${port}${pathAndQueryString}`);
|
|
220
|
+
const response = await httpUtils_1.httpUtils.send(superagentRequest);
|
|
221
|
+
chai_1.assert.strictEqual(response.status, HttpStatusCodes.OK);
|
|
222
|
+
}
|
|
223
|
+
describe(`Query params functions - Case sensitive`, () => {
|
|
224
|
+
before(async () => {
|
|
225
|
+
// ==========================================
|
|
226
|
+
// Set the configs for case sensitivity!
|
|
227
|
+
// ==========================================
|
|
228
|
+
(0, testingConfigurations_1.setTestingConfigurations)(true);
|
|
229
|
+
await startServer(true);
|
|
230
|
+
});
|
|
231
|
+
after(() => {
|
|
232
|
+
server.close();
|
|
233
|
+
});
|
|
234
|
+
it(`no query params`, async () => {
|
|
235
|
+
await send(`/`);
|
|
236
|
+
const values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k');
|
|
237
|
+
chai_1.assert.deepEqual(values, []);
|
|
238
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k');
|
|
239
|
+
chai_1.assert.deepEqual(value, undefined);
|
|
240
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k');
|
|
241
|
+
chai_1.assert.deepEqual(value, undefined);
|
|
242
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k');
|
|
243
|
+
chai_1.assert.deepEqual(value, undefined);
|
|
244
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
245
|
+
chai_1.assert.deepEqual(value, undefined);
|
|
246
|
+
});
|
|
247
|
+
it(`one query params - simple string`, async () => {
|
|
248
|
+
await send(`/?k=toto`);
|
|
249
|
+
let values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k');
|
|
250
|
+
chai_1.assert.deepEqual(values, ['toto']);
|
|
251
|
+
values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'K'); // case sensitive
|
|
252
|
+
chai_1.assert.deepEqual(values, []);
|
|
253
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k');
|
|
254
|
+
chai_1.assert.deepEqual(value, 'toto');
|
|
255
|
+
value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'K'); // case sensitive
|
|
256
|
+
chai_1.assert.deepEqual(value, undefined);
|
|
257
|
+
let error;
|
|
258
|
+
try {
|
|
259
|
+
httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k');
|
|
260
|
+
}
|
|
261
|
+
catch (err) {
|
|
262
|
+
error = err;
|
|
263
|
+
}
|
|
264
|
+
if (!error) {
|
|
265
|
+
chai_1.assert.fail();
|
|
266
|
+
}
|
|
267
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k', (errMsg, val) => {
|
|
268
|
+
chai_1.assert.isOk(errMsg);
|
|
269
|
+
chai_1.assert.deepEqual(val, 'toto');
|
|
270
|
+
return undefined;
|
|
271
|
+
});
|
|
272
|
+
chai_1.assert.deepEqual(value, undefined);
|
|
273
|
+
try {
|
|
274
|
+
httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k');
|
|
275
|
+
}
|
|
276
|
+
catch (err) {
|
|
277
|
+
error = err;
|
|
278
|
+
}
|
|
279
|
+
if (!error) {
|
|
280
|
+
chai_1.assert.fail();
|
|
281
|
+
}
|
|
282
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k', (errMsg, val) => {
|
|
283
|
+
chai_1.assert.isOk(errMsg);
|
|
284
|
+
chai_1.assert.deepEqual(val, 'toto');
|
|
285
|
+
return 123;
|
|
286
|
+
});
|
|
287
|
+
chai_1.assert.deepEqual(value, 123);
|
|
288
|
+
try {
|
|
289
|
+
httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
290
|
+
}
|
|
291
|
+
catch (err) {
|
|
292
|
+
error = err;
|
|
293
|
+
}
|
|
294
|
+
if (!error) {
|
|
295
|
+
chai_1.assert.fail();
|
|
296
|
+
}
|
|
297
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k', (errMsg, val) => {
|
|
298
|
+
chai_1.assert.isOk(errMsg);
|
|
299
|
+
chai_1.assert.deepEqual(val, 'toto');
|
|
300
|
+
return 123;
|
|
301
|
+
});
|
|
302
|
+
chai_1.assert.deepEqual(value, 123);
|
|
303
|
+
});
|
|
304
|
+
it('one query params - date string', async () => {
|
|
305
|
+
const dateStr = '2020-04-21T17:13:33.107Z';
|
|
306
|
+
await send(`/?k=${encodeURIComponent(dateStr)}`);
|
|
307
|
+
const values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k');
|
|
308
|
+
chai_1.assert.deepEqual(values, [dateStr]);
|
|
309
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k');
|
|
310
|
+
chai_1.assert.deepEqual(value, dateStr);
|
|
311
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k');
|
|
312
|
+
chai_1.assert.isTrue(_.isDate(value));
|
|
313
|
+
chai_1.assert.deepEqual(value, new Date(dateStr));
|
|
314
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k', (_errMsg) => {
|
|
315
|
+
chai_1.assert.fail();
|
|
316
|
+
});
|
|
317
|
+
chai_1.assert.deepEqual(value, new Date(dateStr));
|
|
318
|
+
let error;
|
|
319
|
+
try {
|
|
320
|
+
httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k');
|
|
321
|
+
}
|
|
322
|
+
catch (err) {
|
|
323
|
+
error = err;
|
|
324
|
+
}
|
|
325
|
+
if (!error) {
|
|
326
|
+
chai_1.assert.fail();
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k', (_errMsg) => {
|
|
330
|
+
throw new Error(`Custom Error`);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
catch (err) {
|
|
334
|
+
error = err;
|
|
335
|
+
}
|
|
336
|
+
if (!error) {
|
|
337
|
+
chai_1.assert.fail();
|
|
338
|
+
}
|
|
339
|
+
chai_1.assert.deepEqual(error.message, `Custom Error`);
|
|
340
|
+
try {
|
|
341
|
+
httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
342
|
+
}
|
|
343
|
+
catch (err) {
|
|
344
|
+
error = err;
|
|
345
|
+
}
|
|
346
|
+
if (!error) {
|
|
347
|
+
chai_1.assert.fail();
|
|
348
|
+
}
|
|
349
|
+
try {
|
|
350
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k', (_errMsg) => {
|
|
351
|
+
throw new Error(`Custom Error`);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
catch (err) {
|
|
355
|
+
error = err;
|
|
356
|
+
}
|
|
357
|
+
if (!error) {
|
|
358
|
+
chai_1.assert.fail();
|
|
359
|
+
}
|
|
360
|
+
chai_1.assert.deepEqual(error.message, `Custom Error`);
|
|
361
|
+
});
|
|
362
|
+
it('one query params - number string', async () => {
|
|
363
|
+
const testNumber = 123;
|
|
364
|
+
await send(`/?k=${testNumber}`);
|
|
365
|
+
const values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k');
|
|
366
|
+
chai_1.assert.deepEqual(values, [testNumber + '']);
|
|
367
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k');
|
|
368
|
+
chai_1.assert.deepEqual(value, testNumber + '');
|
|
369
|
+
// ==========================================
|
|
370
|
+
// Well, it seems '123' can actually be parsed
|
|
371
|
+
// to a valid date. What can you do?
|
|
372
|
+
// ==========================================
|
|
373
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k');
|
|
374
|
+
chai_1.assert.deepEqual(value, new Date(testNumber + ''));
|
|
375
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k');
|
|
376
|
+
chai_1.assert.deepEqual(value, testNumber);
|
|
377
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k', (_errMsg) => {
|
|
378
|
+
chai_1.assert.fail();
|
|
379
|
+
});
|
|
380
|
+
chai_1.assert.deepEqual(value, testNumber);
|
|
381
|
+
let error;
|
|
382
|
+
try {
|
|
383
|
+
httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
384
|
+
}
|
|
385
|
+
catch (err) {
|
|
386
|
+
error = err;
|
|
387
|
+
}
|
|
388
|
+
if (!error) {
|
|
389
|
+
chai_1.assert.fail();
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
it(`one query params - boolean`, async () => {
|
|
393
|
+
await send(`/?k=true`);
|
|
394
|
+
let value = httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
395
|
+
chai_1.assert.deepEqual(value, true);
|
|
396
|
+
await send(`/?k=TrUe`);
|
|
397
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
398
|
+
chai_1.assert.deepEqual(value, true);
|
|
399
|
+
await send(`/?k=false`);
|
|
400
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
401
|
+
chai_1.assert.deepEqual(value, false);
|
|
402
|
+
await send(`/?k=0`);
|
|
403
|
+
let error;
|
|
404
|
+
try {
|
|
405
|
+
httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
406
|
+
}
|
|
407
|
+
catch (err) {
|
|
408
|
+
error = err;
|
|
409
|
+
}
|
|
410
|
+
if (!error) {
|
|
411
|
+
chai_1.assert.fail();
|
|
412
|
+
}
|
|
413
|
+
await send(`/?k=1`);
|
|
414
|
+
try {
|
|
415
|
+
httpUtils_1.httpUtils.getQueryParamOneAsBoolean(expressRequest, 'k');
|
|
416
|
+
}
|
|
417
|
+
catch (err) {
|
|
418
|
+
error = err;
|
|
419
|
+
}
|
|
420
|
+
if (!error) {
|
|
421
|
+
chai_1.assert.fail();
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
it('two different query params', async () => {
|
|
425
|
+
await send(`/?k1=123&k2=titi`);
|
|
426
|
+
let values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k1');
|
|
427
|
+
chai_1.assert.deepEqual(values, ['123']);
|
|
428
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k1');
|
|
429
|
+
chai_1.assert.deepEqual(value, '123');
|
|
430
|
+
values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k2');
|
|
431
|
+
chai_1.assert.deepEqual(values, ['titi']);
|
|
432
|
+
value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k2');
|
|
433
|
+
chai_1.assert.deepEqual(value, 'titi');
|
|
434
|
+
});
|
|
435
|
+
it('two different query params, different only by casing!', async () => {
|
|
436
|
+
await send(`/?k=123&K=titi`);
|
|
437
|
+
let values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k');
|
|
438
|
+
chai_1.assert.deepEqual(values, ['123']);
|
|
439
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k');
|
|
440
|
+
chai_1.assert.deepEqual(value, '123');
|
|
441
|
+
values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'K');
|
|
442
|
+
chai_1.assert.deepEqual(values, ['titi']);
|
|
443
|
+
value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'K');
|
|
444
|
+
chai_1.assert.deepEqual(value, 'titi');
|
|
445
|
+
});
|
|
446
|
+
it('One query param with multiple values - first value is a number, second value is a date', async () => {
|
|
447
|
+
const dateStr = '2020-04-21T17:13:33.107Z';
|
|
448
|
+
const testNumber = 123;
|
|
449
|
+
await send(`/?k=${testNumber}&k=${encodeURIComponent(dateStr)}`);
|
|
450
|
+
const values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k');
|
|
451
|
+
chai_1.assert.deepEqual(values, [testNumber + '', dateStr]);
|
|
452
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k');
|
|
453
|
+
chai_1.assert.deepEqual(value, dateStr); // last value wins
|
|
454
|
+
// last value wins and is parsable to a Date
|
|
455
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k');
|
|
456
|
+
chai_1.assert.deepEqual(value, new Date(dateStr));
|
|
457
|
+
// last value wins and can't be parsed to a number
|
|
458
|
+
let error;
|
|
459
|
+
try {
|
|
460
|
+
httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k');
|
|
461
|
+
}
|
|
462
|
+
catch (err) {
|
|
463
|
+
error = err;
|
|
464
|
+
}
|
|
465
|
+
if (!error) {
|
|
466
|
+
chai_1.assert.fail();
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
it('One query param with multiple values - first value is a date, second value is a number', async () => {
|
|
470
|
+
const dateStr = '2020-04-21T17:13:33.107Z';
|
|
471
|
+
const testNumber = 123;
|
|
472
|
+
await send(`/?k=${encodeURIComponent(dateStr)}&k=${testNumber}`);
|
|
473
|
+
const values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k');
|
|
474
|
+
chai_1.assert.deepEqual(values, [dateStr, testNumber + '']);
|
|
475
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k');
|
|
476
|
+
chai_1.assert.deepEqual(value, testNumber + ''); // last value wins
|
|
477
|
+
// last value wins and CAN be parsed to a Date...
|
|
478
|
+
// Yep, '123' can be parsed as date.
|
|
479
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsDate(expressRequest, 'k');
|
|
480
|
+
chai_1.assert.deepEqual(value, new Date(testNumber + ''));
|
|
481
|
+
// last value wins and can be parsed to a number
|
|
482
|
+
value = httpUtils_1.httpUtils.getQueryParamOneAsNumber(expressRequest, 'k');
|
|
483
|
+
chai_1.assert.deepEqual(value, testNumber);
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
describe('Query params functions - Case insensitive', () => {
|
|
487
|
+
before(async () => {
|
|
488
|
+
// ==========================================
|
|
489
|
+
// Set the configs for case insensitivity!
|
|
490
|
+
// ==========================================
|
|
491
|
+
(0, testingConfigurations_1.setTestingConfigurations)(false);
|
|
492
|
+
await startServer(false);
|
|
493
|
+
});
|
|
494
|
+
after(() => {
|
|
495
|
+
server.close();
|
|
496
|
+
});
|
|
497
|
+
it('two different query params, different by casing!', async () => {
|
|
498
|
+
await send(`/?k=123&K=titi`);
|
|
499
|
+
let values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'k'); // lowercase
|
|
500
|
+
chai_1.assert.deepEqual(values, ['123', 'titi']);
|
|
501
|
+
let value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'k'); // lowercase
|
|
502
|
+
chai_1.assert.deepEqual(value, 'titi'); // last value wins
|
|
503
|
+
values = httpUtils_1.httpUtils.getQueryParamAll(expressRequest, 'K'); // uppercase
|
|
504
|
+
chai_1.assert.deepEqual(values, ['123', 'titi']);
|
|
505
|
+
value = httpUtils_1.httpUtils.getQueryParamOne(expressRequest, 'K'); // uppercase
|
|
506
|
+
chai_1.assert.deepEqual(value, 'titi'); // last value wins
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
describe('getOrderBys - Case insensitive', () => {
|
|
510
|
+
before(async () => {
|
|
511
|
+
(0, testingConfigurations_1.setTestingConfigurations)(false);
|
|
512
|
+
await startServer(false);
|
|
513
|
+
});
|
|
514
|
+
after(() => {
|
|
515
|
+
server.close();
|
|
516
|
+
});
|
|
517
|
+
it('Nil', async () => {
|
|
518
|
+
await send(`/`);
|
|
519
|
+
let orderBys = httpUtils_1.httpUtils.getOrderBys(null);
|
|
520
|
+
chai_1.assert.deepEqual(orderBys, []);
|
|
521
|
+
orderBys = httpUtils_1.httpUtils.getOrderBys(undefined);
|
|
522
|
+
chai_1.assert.deepEqual(orderBys, []);
|
|
523
|
+
});
|
|
524
|
+
it('No orderBys', async () => {
|
|
525
|
+
await send(`/`);
|
|
526
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
527
|
+
chai_1.assert.deepEqual(orderBys, []);
|
|
528
|
+
});
|
|
529
|
+
it('one orderBy, default is asc', async () => {
|
|
530
|
+
await send(`/?orderBy=name`);
|
|
531
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
532
|
+
chai_1.assert.deepEqual(orderBys, [
|
|
533
|
+
{
|
|
534
|
+
key: 'name',
|
|
535
|
+
direction: src_1.OrderByDirection.ASC
|
|
536
|
+
}
|
|
537
|
+
]);
|
|
538
|
+
});
|
|
539
|
+
it('one orderBy, explicit asc', async () => {
|
|
540
|
+
await send(`/?orderBy=+name`);
|
|
541
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
542
|
+
chai_1.assert.deepEqual(orderBys, [
|
|
543
|
+
{
|
|
544
|
+
key: 'name',
|
|
545
|
+
direction: src_1.OrderByDirection.ASC
|
|
546
|
+
}
|
|
547
|
+
]);
|
|
548
|
+
});
|
|
549
|
+
it('one orderBy, desc', async () => {
|
|
550
|
+
await send(`/?orderBy=-name`);
|
|
551
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
552
|
+
chai_1.assert.deepEqual(orderBys, [
|
|
553
|
+
{
|
|
554
|
+
key: 'name',
|
|
555
|
+
direction: src_1.OrderByDirection.DESC
|
|
556
|
+
}
|
|
557
|
+
]);
|
|
558
|
+
});
|
|
559
|
+
it('multiple orderBys', async () => {
|
|
560
|
+
await send(`/?orderBy=-name,age,+nick,-color`);
|
|
561
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
562
|
+
chai_1.assert.deepEqual(orderBys, [
|
|
563
|
+
{
|
|
564
|
+
key: 'name',
|
|
565
|
+
direction: src_1.OrderByDirection.DESC
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
key: 'age',
|
|
569
|
+
direction: src_1.OrderByDirection.ASC
|
|
570
|
+
},
|
|
571
|
+
{
|
|
572
|
+
key: 'nick',
|
|
573
|
+
direction: src_1.OrderByDirection.ASC
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
key: 'color',
|
|
577
|
+
direction: src_1.OrderByDirection.DESC
|
|
578
|
+
}
|
|
579
|
+
]);
|
|
580
|
+
});
|
|
581
|
+
it('The case sensitivity of the "orderBy" key is not important', async () => {
|
|
582
|
+
await send(`/?ORDERBY=-name`);
|
|
583
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
584
|
+
chai_1.assert.deepEqual(orderBys, [
|
|
585
|
+
{
|
|
586
|
+
key: 'name',
|
|
587
|
+
direction: src_1.OrderByDirection.DESC
|
|
588
|
+
}
|
|
589
|
+
]);
|
|
590
|
+
});
|
|
591
|
+
it('The case sensitivity of the orderBy *value* is kept', async () => {
|
|
592
|
+
await send(`/?orderBy=-NAME`);
|
|
593
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
594
|
+
chai_1.assert.deepEqual(orderBys, [
|
|
595
|
+
{
|
|
596
|
+
key: 'NAME',
|
|
597
|
+
direction: src_1.OrderByDirection.DESC
|
|
598
|
+
}
|
|
599
|
+
]);
|
|
600
|
+
});
|
|
601
|
+
});
|
|
602
|
+
describe('getOrderBys - Case sensitive', () => {
|
|
603
|
+
before(async () => {
|
|
604
|
+
(0, testingConfigurations_1.setTestingConfigurations)(true);
|
|
605
|
+
await startServer(true);
|
|
606
|
+
});
|
|
607
|
+
after(() => {
|
|
608
|
+
server.close();
|
|
609
|
+
});
|
|
610
|
+
it('The case sensitivity of the "orderBy" key is important', async () => {
|
|
611
|
+
await send(`/?ORDERBY=-name`);
|
|
612
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
613
|
+
chai_1.assert.deepEqual(orderBys, []);
|
|
614
|
+
});
|
|
615
|
+
it('The case sensitivity of the orderBy *value* is kept', async () => {
|
|
616
|
+
await send(`/?orderBy=-NAME`);
|
|
617
|
+
const orderBys = httpUtils_1.httpUtils.getOrderBys(expressRequest);
|
|
618
|
+
chai_1.assert.deepEqual(orderBys, [
|
|
619
|
+
{
|
|
620
|
+
key: 'NAME',
|
|
621
|
+
direction: src_1.OrderByDirection.DESC
|
|
622
|
+
}
|
|
623
|
+
]);
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
});
|
|
627
|
+
});
|
|
628
|
+
//# sourceMappingURL=httpUtils.test.js.map
|