-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.tests.js
118 lines (108 loc) · 4.43 KB
/
index.tests.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
115
116
117
118
'use strict';
var spawnReverseProxy = require('../example/simple'),
chai = require('chai'),
async = require('async'),
http = require('http'),
request = require('request'),
expect = chai.expect;
describe('Proxy Routes', function () {
var proxyServerPort = 6010;
var targetPort = 8080;
var targetFQDN = '127.0.0.1';
before(function (done) {
// runs before all tests in this block
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify({
// response includes the url that you tried to access
translatedPath: req.url
}));
}).listen(targetPort, function mockServerReady() {
spawnReverseProxy(function proxyServerReady() {
done(); // call done to start running test suite
});
});
});
it('should translate the url correctly', function (done) {
var urlTranslationTests = [{
// visited path matches rule (1).
visitedPath: '/test',
newUrlTarget: targetFQDN + ':' + targetPort + '/cool'
}, {
// visited path matches rule (1).
// whatever portion is not part of the match is carried over
// to the new path.
// in this case, the '/' is carried over.
visitedPath: '/test/',
newUrlTarget: targetFQDN + ':' + targetPort + '/cool/'
}, {
// visited path matches rule (1).
// query parameters are carried over.
visitedPath: '/test?hi=5/',
newUrlTarget: targetFQDN + ':' + targetPort + '/cool?hi=5/'
}, {
// visited path matches rule (2).
// the unmatched portion '/yo' is carried over.
visitedPath: '/test2/yo',
newUrlTarget: targetFQDN + ':' + targetPort + '/cool2/yo'
}, {
// visited path matches rule (1).
// note that the key is interpreted as a regex expression and the
// module matches against the visited path, and not the entire url.
visitedPath: '/fuzzyshoe/test',
newUrlTarget: targetFQDN + ':' + targetPort + '/cool'
}, {
// visited path matches rule (1).
// the unmatched portion '/seven' is carried over.
visitedPath: '/test/seven',
newUrlTarget: targetFQDN + ':' + targetPort + '/cool/seven'
}, {
// no rule matched, so the module uses the specified default target.
// the entire visited path is carried over.
// see the `Other Notes` section of README to see why specifying
// the rule `.*/test` does not match `/testalmost`.
visitedPath: '/testalmost',
newUrlTarget: targetFQDN + ':' + targetPort + '/testalmost'
}, {
// no rule matched, so the module uses the specified default target.
// the entire visited path is carried over.
visitedPath: '/testalmost/5',
newUrlTarget: targetFQDN + ':' + targetPort + '/testalmost/5'
}, {
// visited path matched rule (3).
// regex placeholder should be matched and replaced correctly
visitedPath: '/posts/11/comments/12',
newUrlTarget: targetFQDN + ':' + targetPort + '/p/11/c/12'
}, {
// visited path matches rule (3).
// whatever portion is not part of the match is carried over
// to the new path.
// in this case, the '/' is carried over.
visitedPath: '/posts/11/comments/12/',
newUrlTarget: targetFQDN + ':' + targetPort + '/p/11/c/12/'
}, {
// visited path matches rule (3).
// query parameters are carried over.
visitedPath: '/posts/11/comments/12?hi=5/',
newUrlTarget: targetFQDN + ':' + targetPort + '/p/11/c/12?hi=5/'
}, {
// visited path matches rule (4).
// parameters substitution should work correctly for rules with slash at the end
visitedPath: '/author/11/posts/12/',
newUrlTarget: targetFQDN + ':' + targetPort + '/a/11/p/12/'
}
];
// makes a bunch of requests in parallel, and then calls
// `done` after we receive all responses back
async.each(urlTranslationTests, function makeRequest(comparisonObj, cb) {
request({
url: 'http://127.0.0.1:' + proxyServerPort + comparisonObj.visitedPath,
json: true
}, function processResp(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(targetFQDN + ':' + targetPort + body.translatedPath).to.equal(comparisonObj.newUrlTarget);
cb();
});
}, done);
});
});