-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.js
More file actions
273 lines (261 loc) · 8.68 KB
/
Copy pathHost.js
File metadata and controls
273 lines (261 loc) · 8.68 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
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
'use strict';
var path = require('path');
var fs = require('fs');
var assert = require('./utils').assert;
var request = require('request');
var _ = require('lodash');
var Func = require('../lib/Func');
var Host = require('../lib/Host');
var echo = require('./test_functions/echo');
var version = require('../package').version;
var postToHost = require('./utils').postToHost;
describe('Host', function() {
describe('constructor', function() {
it('should be a function', function() {
assert.isFunction(Host);
});
it('should instantiate with properties set correctly', function() {
var host = new Host({silent: true});
assert.isObject(host.config);
assert.notStrictEqual(host.config, host.defaultConfig);
assert.isObject(host.functions);
assert.deepEqual(
_.omit(host.config, 'silent'),
_.omit(host.defaultConfig, 'silent')
);
assert.deepEqual(host.functions, Object.create(null));
});
it('the constructor should accept a config', function() {
var config = {silent: true};
var host = new Host(config);
assert.notStrictEqual(host.config, host.defaultConfig);
assert.strictEqual(host.config, config);
assert.deepEqual(
_.omit(host.config, 'silent'),
_.omit(host.defaultConfig, 'silent')
);
});
});
describe('#addFunction()', function() {
it('should accept an object', function() {
var host = new Host({silent: true});
var func = function() {};
host.addFunction('test', func);
assert.isDefined(host.functions.test);
assert.instanceOf(host.functions.test, Func);
assert.equal(host.functions.test.name, 'test');
assert.strictEqual(host.functions.test.handler, func);
});
it('can be called multiple times', function() {
var host = new Host({silent: true});
var func1 = function() {};
var func2 = function() {};
host.addFunction('test1', func1);
host.addFunction('test2', func2);
assert.isDefined(host.functions.test1);
assert.isDefined(host.functions.test2);
assert.instanceOf(host.functions.test1, Func);
assert.instanceOf(host.functions.test2, Func);
assert.equal(host.functions.test1.name, 'test1');
assert.equal(host.functions.test2.name, 'test2');
assert.isFunction(host.functions.test1.handler);
assert.isFunction(host.functions.test2.handler);
assert.strictEqual(host.functions.test1.handler, func1);
assert.strictEqual(host.functions.test2.handler, func2);
});
it('throws an error if a function is added with a conflicting name', function() {
var host = new Host({silent: true});
host.addFunction('test', function() {});
assert.throws(
function() {
host.addFunction('test', function() {});
},
'A function has already been defined with the name "test"'
);
});
});
describe('#getUrl()', function() {
it('should respect the defaults', function() {
assert.equal(
new Host({silent: true}).getUrl(),
'http://' + Host.prototype.defaultConfig.address + ':' + Host.prototype.defaultConfig.port
);
});
it('should respect the config', function() {
assert.equal(
new Host({
address: 'foo',
port: 'bar',
silent: true
}).getUrl(),
'http://foo:bar'
);
});
});
describe('#listen()', function() {
it('can start the listenerServer', function(done) {
var host = new Host({
outputOnListen: false,
silent: true
});
host.listen(function() {
host.stopListening();
done();
});
});
});
describe('#listenerServer', function() {
it('is set when listening', function(done) {
var host = new Host({
outputOnListen: false,
silent: true
});
assert.isNull(host.listenerServer);
host.listen(function() {
assert.isNotNull(host.listenerServer);
host.stopListening();
done();
});
});
it('is unset after listening has stopped', function(done) {
var host = new Host({
outputOnListen: false,
silent: true
});
assert.isNull(host.listenerServer);
host.listen(function() {
assert.isNotNull(host.listenerServer);
host.stopListening();
assert.isNull(host.listenerServer);
done();
});
});
});
describe('/status endpoint', function() {
it('can expose a host\'s status as JSON', function(done) {
var host = new Host({
outputOnListen: false,
silent: true,
functions: {
foo: function() {}
}
});
host.listen(function() {
request(host.getUrl() + '/status', function(err, res, body) {
assert.isNull(err);
var status = JSON.parse(body);
assert.isObject(status);
assert.equal(status.type, 'Host');
assert.isDefined(status.version);
assert.equal(status.version, version);
assert.isUndefined(status.logger);
var config = status.config;
assert.isObject(config);
assert.equal(config.address, host.config.address);
assert.equal(config.port, host.config.port);
assert.isArray(config.functions);
assert.include(config.functions, 'foo');
host.stopListening();
done();
});
});
});
});
describe('function routing and handling', function() {
it('requests can be routed to a function', function(done) {
var host = new Host({
outputOnListen: false,
silent: true
});
host.addFunction('function1', function(data, cb) {
cb(null, 'in handler1');
});
host.addFunction('function2', function(data, cb) {
cb(null, 'in handler2');
});
host.listen(function() {
postToHost(host, 'foo', function(err, res, body) {
assert.equal(res.statusCode, '404');
assert.equal(body, 'Not found. Unknown function "foo"');
postToHost(host, 'function1', function(err, res, body) {
assert.equal(body, 'in handler1');
postToHost(host, 'function2', function(err, res, body) {
assert.equal(body, 'in handler2');
postToHost(host, 'bar', function(err, res, body) {
assert.equal(res.statusCode, '404');
assert.equal(body, 'Not found. Unknown function "bar"');
host.stopListening();
done();
});
});
});
});
});
});
it('functions can receive and send large data sets', function(done) {
// A 2.5mb text file
var testTextFile = path.join(__dirname, 'test_data', 'test.txt');
var text = fs.readFileSync(testTextFile).toString('utf-8');
var host = new Host({
outputOnListen: false,
silent: true
});
host.addFunction('text-test', function(data, cb) {
if (data.text !== text) {
return cb('data.text does not match');
}
cb(null, 'success: ' + data.text);
});
host.listen(function() {
postToHost(host, 'text-test', {data: {text: text}}, function(err, res, body) {
assert.equal(body, 'success: ' + text);
host.stopListening();
done();
});
});
});
it('a function\'s `done` callback can only be called once', function(done) {
var host = new Host({
outputOnListen: false,
silent: true
});
host.addFunction('done-x1', function(data, cb) {
cb(null, 'some success');
});
host.addFunction('done-x2', function(data, cb) {
cb('x2');
cb(null, 'some success x2');
});
host.addFunction('done-x3', function(data, cb) {
cb(null, 'some success x3');
cb('x3');
cb(null, 'some other success x3');
});
host.listen(function() {
postToHost(host, 'done-x1', function(err, res, body) {
assert.equal(res.statusCode, 200);
assert.include(body, 'some success');
postToHost(host, 'done-x2', function(err, res, body) {
assert.equal(res.statusCode, 500);
assert.include(body, 'x2');
postToHost(host, 'done-x3', function(err, res, body) {
assert.equal(res.statusCode, 200);
assert.include(body, 'some success x3');
host.stopListening();
done();
});
});
});
});
});
});
describe('#logger', function() {
it('should be configurable via a config\'s `logger` prop', function() {
var logger = {};
var host = new Host({
logger: logger
});
assert.strictEqual(host.logger, logger);
});
});
});