|
1 | 1 | const test = require('tap').test;
|
2 | 2 | const proxyquire = require('proxyquire');
|
3 | 3 | const nock = require('nock');
|
| 4 | +const needle = require('needle'); |
4 | 5 |
|
5 | 6 | const sinon = require('sinon');
|
6 | 7 | const spy = sinon.spy();
|
7 | 8 | const debugMock = (loggerType) => (msg) => {spy(msg);};
|
8 | 9 | const state = require('../lib/state');
|
| 10 | +const config = require('../lib/config'); |
| 11 | +config.initConfig({projectId: 'some-project-id'}); |
9 | 12 | const transmitter = proxyquire('../lib/transmitter', {'debug': debugMock});
|
10 | 13 |
|
11 | 14 | test('Transmitter transmits 0 events for no events', async function (t) {
|
12 | 15 | nock('http://host')
|
13 |
| - .post('/method') |
14 |
| - .reply(200, {}); |
| 16 | + .post('/method') |
| 17 | + .reply(200, {}); |
| 18 | + nock('http://host') |
| 19 | + .post('/method') |
| 20 | + .reply(200, {}); |
15 | 21 |
|
16 | 22 | spy.resetHistory();
|
| 23 | + const needleSpy = sinon.spy(needle, 'request'); |
17 | 24 |
|
18 | 25 | await transmitter.transmitEvents('http://host/method', 'some-project-id', 'some-agent-id');
|
19 |
| - t.ok(nock.isDone(), 'empty transmission sent'); |
| 26 | + t.equal(needleSpy.args[0][0], 'post', 'beacons are being posted'); |
| 27 | + t.equal(needleSpy.args[0][1], 'http://host/method', 'url is correct'); |
| 28 | + t.ok('agentId' in needleSpy.args[0][2], 'agent ID is transmitted'); |
| 29 | + t.equal(needleSpy.args[0][2]['agentId'], 'some-agent-id', 'agent ID is correct'); |
| 30 | + t.ok('projectId' in needleSpy.args[0][2], 'project ID is transmitted'); |
| 31 | + t.equal(needleSpy.args[0][2]['projectId'], 'some-project-id', 'project ID is correct'); |
| 32 | + t.deepEqual(needleSpy.args[0][3], {json: true, rejectUnauthorized: true}, 'request options are correct'); |
| 33 | + |
| 34 | + config['allowUnknownCA'] = true; |
| 35 | + await transmitter.transmitEvents('http://host/method', 'some-project-id', 'some-agent-id'); |
| 36 | + t.deepEqual(needleSpy.args[1][3], {json: true, rejectUnauthorized: false}, 'request options are correct'); |
| 37 | + |
| 38 | + t.ok(nock.isDone(), 'two transmissions sent'); |
| 39 | + |
20 | 40 | nock.cleanAll();
|
21 | 41 | });
|
22 | 42 |
|
|
0 commit comments