/
index.ts
438 lines (392 loc) Β· 13.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as assert from 'assert';
import {describe, it, afterEach, beforeEach} from 'mocha';
import * as nock from 'nock';
import {Readable, PassThrough} from 'stream';
import * as sinon from 'sinon';
import {teenyRequest} from '../src';
import {TeenyStatistics, TeenyStatisticsWarning} from '../src/TeenyStatistics';
import {pool} from '../src/agents';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const HttpProxyAgent = require('http-proxy-agent');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const HttpsProxyAgent = require('https-proxy-agent');
nock.disableNetConnect();
const uri = 'https://example.com';
function mockJson() {
return nock(uri).get('/').reply(200, {hello: 'π'});
}
function mockError() {
return nock(uri).get('/').replyWithError('mock err');
}
describe('teeny', () => {
const sandbox = sinon.createSandbox();
let emitWarnStub: sinon.SinonStub;
let statsStub: sinon.SinonStubbedInstance<TeenyStatistics>;
beforeEach(() => {
emitWarnStub = sandbox.stub(process, 'emitWarning');
// don't mask other process warns
emitWarnStub
.callThrough()
.withArgs(sinon.match.instanceOf(TeenyStatisticsWarning))
.callsFake(() => {});
// note: this stubs the already instantiated TeenyStatistics
statsStub = sandbox.stub(teenyRequest.stats);
});
afterEach(() => {
pool.clear();
sandbox.restore();
teenyRequest.resetStats();
nock.cleanAll();
});
it('should get JSON', done => {
const scope = mockJson();
teenyRequest({uri}, (error, response, body) => {
assert.ifError(error);
assert.strictEqual(response.statusCode, 200);
assert.ok(body.hello);
scope.done();
done();
});
});
it('should set defaults', done => {
const scope = mockJson();
const defaultRequest = teenyRequest.defaults({timeout: 60000});
defaultRequest({uri}, (error, response, body) => {
assert.ifError(error);
assert.strictEqual(response.statusCode, 200);
assert.ok(body.hello);
scope.done();
done();
});
});
it('response event emits object compatible with request module', done => {
const reqHeaders = {fruit: 'banana'};
const resHeaders = {veggies: 'carrots'};
const scope = nock(uri).get('/').reply(202, 'ok', resHeaders);
const reqStream = teenyRequest({uri, headers: reqHeaders});
reqStream
.on('response', res => {
assert.strictEqual(res.statusCode, 202);
assert.strictEqual(res.headers.veggies, 'carrots');
assert.deepStrictEqual(res.request.headers, reqHeaders);
assert.deepStrictEqual(res.toJSON(), {
headers: resHeaders,
});
assert(res instanceof Readable);
scope.done();
done();
})
.on('error', done);
});
it('should include the request in the response', done => {
const path = '/?dessert=pie';
const scope = nock(uri).get(path).reply(202);
const headers = {dinner: 'tacos'};
const url = `${uri}${path}`;
teenyRequest({url, headers}, (error, response) => {
assert.ifError(error);
const req = response.request;
assert.deepStrictEqual(req.headers, headers);
assert.strictEqual(req.href, url);
scope.done();
done();
});
});
it('should not wrap the error', done => {
const scope = nock(uri)
.get('/')
.reply(200, 'π¨', {'content-type': 'application/json'});
teenyRequest({uri}, err => {
assert.ok(err);
assert.ok(err!.message.match(/^invalid json response body/));
scope.done();
done();
});
});
it('should include headers in the response', done => {
const headers = {dinner: 'tacos'};
const body = {hello: 'π'};
const scope = nock(uri).get('/').reply(200, body, headers);
teenyRequest({uri}, (err, res) => {
assert.ifError(err);
assert.strictEqual(headers['dinner'], res.headers['dinner']);
scope.done();
done();
});
});
it('should accept the forever option', done => {
const scope = nock(uri).get('/').reply(200);
teenyRequest({uri, forever: true}, (err, res) => {
assert.ifError(err);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.strictEqual((res.request.agent as any).keepAlive, true);
scope.done();
done();
});
});
it('should allow setting compress/gzip to true', done => {
const reqheaders = {
'Accept-Encoding': 'gzip,deflate',
};
const scope = nock(uri, {reqheaders}).get('/').reply(200);
teenyRequest({uri, gzip: true}, err => {
assert.ifError(err);
scope.done();
done();
});
});
it('should allow setting compress/gzip to false', done => {
const badheaders = ['Accept-Encoding'];
const scope = nock(uri, {badheaders}).get('/').reply(200);
teenyRequest({uri, gzip: false}, err => {
assert.ifError(err);
scope.done();
done();
});
});
const envVars = ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY'];
for (const v of envVars) {
it(`should respect ${v} environment variable for proxy config`, done => {
sandbox.stub(process, 'env').value({[v]: 'https://fake.proxy'});
const expectedBody = {hello: 'π'};
const scope = nock(uri).get('/').reply(200, expectedBody);
teenyRequest({uri}, (err, res, body) => {
scope.done();
assert.ifError(err);
assert.deepStrictEqual(expectedBody, body);
assert.ok(res.request.agent instanceof HttpsProxyAgent);
return done();
});
});
}
it('should create http proxy if upstream scheme is http', done => {
sandbox.stub(process, 'env').value({http_proxy: 'https://fake.proxy'});
const expectedBody = {hello: 'π'};
const scope = nock('http://example.com').get('/').reply(200, expectedBody);
teenyRequest({uri: 'http://example.com'}, (err, res, body) => {
scope.done();
assert.ifError(err);
assert.deepStrictEqual(expectedBody, body);
assert.ok(res.request.agent instanceof HttpProxyAgent);
return done();
});
});
it('should use proxy if set in request options', done => {
const expectedBody = {hello: 'π'};
const scope = nock(uri).get('/').reply(200, expectedBody);
teenyRequest({uri, proxy: 'https://fake.proxy'}, (err, res, body) => {
scope.done();
assert.ifError(err);
assert.deepStrictEqual(expectedBody, body);
assert.ok(res.request.agent instanceof HttpsProxyAgent);
return done();
});
});
// see: https://github.com/googleapis/nodejs-storage/issues/798
it('should not throw exception when piped through pumpify', async () => {
const scope = mockJson();
const stream = teenyRequest({uri}).pipe(new PassThrough());
let content = '';
for await (const data of stream) {
content += data;
}
assert.deepStrictEqual(JSON.parse(content), {hello: 'π'});
scope.done();
});
it('should emit response event when called without callback', done => {
const scope = mockJson();
teenyRequest({uri}).on('response', res => {
assert.ok(res);
scope.done();
return done();
});
});
it('should pipe response stream to user', done => {
const scope = mockJson();
teenyRequest({uri})
.on('error', done)
.on('data', () => {
scope.done();
done();
});
});
it('should not pipe response stream to user unless they ask for it', done => {
const scope = mockJson();
const stream = teenyRequest({uri}).on('error', done);
stream.on('response', responseStream => {
// We are using an internal property of Readable to get the number of
// active readers. The property changed from `pipesCount: number` in
// Node.js 12.x and below to `pipes: Array` in Node.js 13.x.
let numPipes =
responseStream.body._readableState.pipesCount ??
responseStream.body._readableState.pipes?.length;
assert.strictEqual(numPipes, 0);
stream.on('data', () => {
numPipes =
responseStream.body._readableState.pipesCount ??
responseStream.body._readableState.pipes?.length;
assert.strictEqual(numPipes, 1);
scope.done();
done();
});
});
});
it('should expose TeenyStatistics instance', () => {
assert.ok(teenyRequest.stats instanceof TeenyStatistics);
});
it('should allow resetting statistics', () => {
const oldStats = teenyRequest.stats;
teenyRequest.resetStats();
assert.notStrictEqual(teenyRequest.stats, oldStats);
assert.ok(teenyRequest.stats instanceof TeenyStatistics);
});
it('should keep the original stats options when resetting', () => {
statsStub.getOptions.restore();
statsStub.setOptions.restore();
teenyRequest.stats.setOptions({concurrentRequests: 42});
teenyRequest.resetStats();
const newOptions = teenyRequest.stats.getOptions();
assert.deepStrictEqual(newOptions, {concurrentRequests: 42});
});
it('should emit warning on too many concurrent requests', done => {
statsStub.setOptions.restore();
statsStub.requestStarting.restore();
teenyRequest.stats.setOptions({concurrentRequests: 1});
const scope = mockJson();
teenyRequest({uri}, () => {
assert.ok(emitWarnStub.calledOnce);
scope.done();
done();
});
});
it('should track stats, callback mode, success', done => {
const scope = mockJson();
teenyRequest({uri}, () => {
assert.ok(statsStub.requestStarting.calledOnceWithExactly());
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
done();
});
});
it('should track stats, callback mode, failure', done => {
const scope = mockError();
teenyRequest({uri}, err => {
assert.ok(err);
assert.ok(statsStub.requestStarting.calledOnceWithExactly());
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
done();
});
});
it('should track stats, stream mode, success', done => {
const scope = mockJson();
const readable = teenyRequest({uri});
assert.ok(statsStub.requestStarting.calledOnceWithExactly());
readable.once('response', () => {
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
done();
});
});
it('should track stats, stream mode, failure', done => {
const scope = mockError();
const readable = teenyRequest({uri});
assert.ok(statsStub.requestStarting.calledOnceWithExactly());
readable.once('error', err => {
assert.ok(err);
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
done();
});
});
it('should accept a Buffer as the body of a request', done => {
const scope = nock(uri).post('/', 'hello').reply(200, 'π');
teenyRequest(
{uri, method: 'POST', body: Buffer.from('hello')},
(error, response, body) => {
assert.ifError(error);
assert.strictEqual(response.statusCode, 200);
assert.strictEqual(body, 'π');
scope.done();
done();
}
);
});
it('should accept a plain string as the body of a request', done => {
const scope = nock(uri).post('/', 'hello').reply(200, 'π');
teenyRequest(
{uri, method: 'POST', body: 'hello'},
(error, response, body) => {
assert.ifError(error);
assert.strictEqual(response.statusCode, 200);
assert.strictEqual(body, 'π');
scope.done();
done();
}
);
});
it('should accept json as the body of a request', done => {
const body = {hello: 'π'};
const scope = nock(uri).post('/', JSON.stringify(body)).reply(200, 'π');
teenyRequest({uri, method: 'POST', json: body}, (error, response, body) => {
assert.ifError(error);
assert.strictEqual(response.statusCode, 200);
assert.strictEqual(body, 'π');
scope.done();
done();
});
});
// TODO multipart is broken with 2 strings
// see: https://github.com/googleapis/teeny-request/issues/168
it.skip('should track stats, multipart mode, success', done => {
const scope = mockJson();
teenyRequest(
{
method: 'POST',
headers: {},
multipart: [{body: 'foo'}, {body: 'bar'}],
uri,
},
() => {
assert.ok(statsStub.requestStarting.calledOnceWithExactly());
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
done();
}
);
});
it.skip('should track stats, multipart mode, failure', done => {
const scope = mockError();
teenyRequest(
{
method: 'POST',
headers: {},
multipart: [{body: 'foo'}, {body: 'bar'}],
uri,
},
err => {
assert.ok(err);
assert.ok(statsStub.requestStarting.calledOnceWithExactly());
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
done();
}
);
});
});