-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_cli.spec.js
More file actions
107 lines (88 loc) · 2.9 KB
/
proxy_cli.spec.js
File metadata and controls
107 lines (88 loc) · 2.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
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
'use strict';
var startService = require('./service')
, proxyConfig = require('./proxy_config.json')
, request = require('request')
, assert = require('assert')
, spawn = require('child_process').spawn;
describe('jsonscript proxy cli', function() {
var proxy;
before(function (done) {
proxy = spawnProxy(['proxy_config.json']);
startService('service1', 3003);
startService('service2', 3004);
setTimeout(done, 1000);
});
after(function (done) {
proxy.on('exit', done);
proxy.kill();
});
describe('parallel evaluation', function() {
it('should process script via proxy', function (done) {
testProxy(3000, '/js', done);
});
});
describe('port option', function() {
it('should support -p option', function (done) {
var proxy = spawnProxy(['-p', '3010', 'proxy_config.json']);
testNewProxy(proxy, 3010, '/js', done);
});
it('should support --port option', function (done) {
var proxy = spawnProxy(['--port', '3020', 'proxy_config.json']);
testNewProxy(proxy, 3020, '/js', done);
});
});
describe('api option', function() {
it('should support -a option', function (done) {
var proxy = spawnProxy(['-a', '/jsonscript', '-p', '3030', 'proxy_config.json']);
testNewProxy(proxy, 3030, '/jsonscript', done);
});
it('should support --api option', function (done) {
var proxy = spawnProxy(['--api', '/jsonscript', '-p', '3040', 'proxy_config.json']);
testNewProxy(proxy, 3040, '/jsonscript', done);
});
});
});
function spawnProxy(params) {
return spawn('../bin/jsproxy.js', params, { cwd: __dirname });
}
function testProxy(port, api, callback) {
send(port, api, {
script: {
obj1: { '$$service1.get': { path: '/object/1' } },
obj2: { '$$service2.get': { path: '/object/2' } }
}
}, function (err, resp, body) {
assert(!err, err && err.message);
assert.equal(resp.statusCode, 200);
assertGetResult(body.obj1, 'service1', 'object', '1');
assertGetResult(body.obj2, 'service2', 'object', '2');
callback();
});
}
function testNewProxy(proxy, port, api, callback) {
setTimeout(function() {
testProxy(port, api, function() {
proxy.on('exit', callback);
proxy.kill();
});
}, 1000);
}
function send(port, api, reqBody, callback) {
request.post({
uri: 'http://localhost:' + port + api,
headers: { Accept: 'application/json' },
json: reqBody
}, callback);
}
function assertGetResult(result, serviceName, name, id) {
assert.equal(result.statusCode, 200);
assert.equal(typeof result.headers, 'object');
assert.deepEqual(result.service, { name: serviceName, basePath: proxyConfig.services[serviceName].basePath });
assert.deepEqual(result.request, { method: 'get', path: '/' + name + '/' + id })
assert.deepEqual(result.body, {
name: name,
id: id,
service: serviceName,
info: 'resource ' + name + ' id ' + id
});
}