-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlogger.js
More file actions
137 lines (112 loc) · 3.84 KB
/
Copy pathlogger.js
File metadata and controls
137 lines (112 loc) · 3.84 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
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
'use strict'
var should = require('should'),
request = require('supertest'),
http = require('http'),
express = require('express'),
sinon = require('sinon'),
stackify = require('../'),
logger = require('../lib/logger'),
sender = require('../lib/sender'),
exception = require('../lib/exception'),
config = require('../config/config.js');
process.env['STACKIFY_TEST'] = true
/**
* Setup basic Express middle ware for tests
* @param app
*/
function setupBasicExpressMiddleware(app) {
app.get('/', function(req,res, next) {
var testError = new Error("Test GET Error");
stackify.info('info message')
next(testError);
});
app.use(stackify.expressExceptionHandler);
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});
}
/**
* Stub out the "send" method of the "sender" module. This way we don't have to worry about outgoing requests.
*
* @param options
* @param cb
* @param fail
*/
function senderStubFunc(options, cb, fail) {
var urlFullPath = options.url,
testIdenitfyAppPath = config.PROTOCOL + '://' + config.HOST + config.IDENTIFY_PATH,
testLogSavePath = config.PROTOCOL + '://' + config.HOST + config.LOG_SAVE_PATH,
responseObj;
if (urlFullPath == testIdenitfyAppPath) {
responseObj = {
"DeviceID":null,
"DeviceAppID":null,
"AppNameID":"unique-app-name-id",
"EnvID":9,
"Env":"Local",
"AppName":"stackify-node-testapp",
"AppEnvID":"unique-app-env-id",
"DeviceAlias":null
};
} else if (urlFullPath == testLogSavePath) {
responseObj = {
"success": true,
"took": 500
};
}
cb(responseObj);
}
describe('Logger', function() {
context('Express middleware with no API key specified', function(){
var app;
before(function(){
app = express();
setupBasicExpressMiddleware(app);
});
it('sets a proper state when no API key has been specified', function(done){
request(app)
.get('/')
.end(function (err, res){
if (err) throw err;
logger.hasAppDetails().should.equal(false);
logger.flushLogs(); // Flush for the next tests.
done();
});
});
});
context('Express middleware with valid API key setup', function () {
var app, agent, senderStub;
before(function(){
app = express();
setupBasicExpressMiddleware(app);
agent = request.agent(app);
senderStub = sinon.stub(sender, "send", senderStubFunc);
sinon.stub(exception, "catchException"); // Disable the uncaught exception handler to prevent handling of failed tests.
stackify.start({appName:'Test App', apiKey: 'test API Key', env: 'Local'});
});
after(function(){
sinon.restore();
});
it('should successfully return and have sent logs to Stackify', function (done) {
agent
.get("/")
.end(function(err, res){
if (err) throw err;
logger.flushLogs();
logger.size().should.be.exactly(0);
done();
});
});
it('should process a second request and return successfully without timing out and have sent logs to Stackify', function (done) {
agent
.get('/')
.end(function(err, res){
if (err) throw err;
logger.flushLogs();
logger.size().should.be.exactly(0);
done();
});
});
});
})