-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsender.js
More file actions
80 lines (67 loc) · 1.9 KB
/
Copy pathsender.js
File metadata and controls
80 lines (67 loc) · 1.9 KB
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
'use strict'
process.env['STACKIFY_TEST'] = true
var expect = require('chai').expect;
var rewire = require('rewire');
var root_path = '../';
var debug = require(root_path + 'lib/debug');
var sender = rewire(root_path + 'lib/sender');
var event = require(root_path + 'lib/event');
var config = require(root_path + 'config/config');
var sinon = require('sinon');
var logs = [];
describe('Sender', function() {
let stub;
let body = {
'message': 'test'
};
let response = {
statusCode: 200,
body: body
};
let errorResponse = {
statusCode: 401,
body: body
};
before(function(){
stub = sinon.stub(sender, 'request')
event.on(config.EVENT_ERROR, function () {
logs.push(arguments)
})
sinon.stub(debug, 'writeResponse')
sinon.stub(debug, 'writeRequest')
});
after(function(){
sinon.restore();
});
it('Error should be logged', function(done) {
let error = new Error('some error');
stub.callsArgWith(1, error, {}, {})
sender.send({}, function (){}, function (){});
setTimeout(function () {
expect(logs.length).to.be.equal(1)
expect(logs[0][0]).to.be.equal('error')
expect(logs[0][1]).to.be.equal('some error')
expect(logs[0][2]).to.eql([{'error':error}])
done();
}, 1);
})
it('Success response', function(done) {
let error = new Error('some error');
stub.callsArgWith(1, null, response, response.body)
sender.send({}, function callback(cbResponse) {
expect(cbResponse.success).to.be.equal(true)
expect(cbResponse.appData).to.be.eql(response.body)
}, function fail() {
});
done();
})
it('Error response', function(done) {
let error = new Error('some error');
stub.callsArgWith(1, null, errorResponse, errorResponse.body)
sender.send({}, function callback(cbResponse) {
}, function fail(code) {
expect(code).to.be.equal(401)
});
done();
})
});