/
server.js
114 lines (88 loc) · 2.53 KB
/
server.js
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
var assert = require('assert'),
request = require('request'),
forever = require('forever'),
highkick = require('highkick'),
join = require('path').join,
config = require('../lib/config'),
report = require('../lib/report'),
logging = require('../lib/logging');
const CONN_TIMEOUT = 2000;
var serverProcess;
function init(options, callback){
if(serverProcess){
callback(undefined, get, post);
return;
}
config.filename('test/tmp-config.json');
var configdoc = config();
report.reset(function(error){
if(error){
logging.error('Failed to reset report document.');
logging.error(error);
return;
}
serverProcess = forever.start(['./bin/lowkick', 'publish', 'test/tmp-config.json'], {
silent: true
});
serverProcess.on('start', function(process, data){
var url = 'http://' + configdoc.server.host + ':' + configdoc.server.port,
startTS = +(new Date);
(function waitServerToStart(){
request.get(url, function(error){
if(error && +(new Date)-startTS<CONN_TIMEOUT) {
setTimeout(waitServerToStart, 100);
return;
} else if(error){
end(function(){
callback(new Error('Connection timeout. ('+(+(new Date)-startTS)+' ms)'));
});
return;
}
callback(undefined, get, post);
});
})();
});
});
}
function end(callback){
if(!serverProcess){
logging.warn('Server is already not running');
callback();
return;
}
try {
serverProcess.stop();
callback();
} catch(err) {
callback(err);
}
}
function get(path, callback){
var configdoc = config();
request.get({
'headers': { 'content-type' : 'application/json' },
'url': 'http://' + join(configdoc.server.host+':'+configdoc.server.port, 'api', path)
}, callback);
}
function post(path, body, callback){
var configdoc = config();
request.post({
'headers': { 'content-type' : 'application/json' },
'url': 'http://' + join(configdoc.server.host+':'+configdoc.server.port, 'api', path),
'body': JSON.stringify(body)
}, callback);
}
function testAPI(get, post, callback){
highkick({ module:require('./api'), name:'API', 'silent': false, 'ordered':true, 'get': get, 'post': post }, function(error, result){
if(result.fail>0){
callback(new Error('API tests were failed.'));
return;
}
callback();
});
}
module.exports = {
'init': init,
'end': end,
'testAPI': testAPI
};