-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathrouting-proxy-test.js
155 lines (145 loc) · 5.46 KB
/
routing-proxy-test.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
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
/*
* proxy-table-test.js: Tests for the ProxyTable object.
*
* (C) 2010, Charlie Robbins
*
*/
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
util = require('util'),
argv = require('optimist').argv,
request = require('request'),
vows = require('vows'),
helpers = require('../helpers');
var options = helpers.parseProtocol(),
testName = [options.source.protocols.http, options.target.protocols.http].join('-to-'),
runner = new helpers.TestRunner(options),
routeFile = path.join(__dirname, 'config.json');
var fileOptions = {
router: {
"foo.com": "127.0.0.1:8101",
"bar.com": "127.0.0.1:8102"
}
};
var defaultOptions = {
router: {
"foo.com": "127.0.0.1:8091",
"bar.com": "127.0.0.1:8092",
"baz.com/taco": "127.0.0.1:8098",
"pizza.com/taco/muffins": "127.0.0.1:8099",
}
};
var hostnameOptions = {
hostnameOnly: true,
router: {
"foo.com": "127.0.0.1:8091",
"bar.com": "127.0.0.1:8092"
}
};
var staticOptions = {
hostnameOnly: true,
router: {
"static.com": "127.0.0.1:8121",
"removed.com": "127.0.0.1:8122",
"change.com": "127.0.0.1:8123"
}
};
var dynamicHost = "dynamic1.com";
var dynamicTarget = "127.0.0.1:8125";
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"when passed a routing table": {
"and routing by RegExp": {
topic: function () {
this.server = runner.startProxyServerWithTable(8090, defaultOptions, this.callback);
},
"an incoming request to foo.com": runner.assertProxied('foo.com', 8090, 8091),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8090, 8092),
"an incoming request to baz.com/taco": runner.assertProxied('baz.com', 8090, 8098, "/taco", "/"),
"an incoming request to pizza.com/taco/muffins": runner.assertProxied('pizza.com', 8090, 8099, "/taco/muffins", "/taco"),
"an incoming request to unknown.com": runner.assertResponseCode(8090, 404)
},
"and routing by Hostname": {
topic: function () {
this.server = runner.startProxyServerWithTable(8093, hostnameOptions, this.callback);
},
"an incoming request to foo.com": runner.assertProxied('foo.com', 8093, 8094),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8093, 8095),
"an incoming request to unknown.com": runner.assertResponseCode(8093, 404)
},
"and adding a route by Hostname": {
topic: function () {
this.server = runner.startProxyServerWithTable(8120, staticOptions, this.callback);
this.server.removeHost('removed.com');
this.server.addHost(dynamicHost, dynamicTarget);
this.server.changeHost('change.com', '127.0.0.1:8124');
},
"an incoming request to static.com": runner.assertProxied('static.com', 8120, 8121),
"an incoming request to removed.com": !runner.assertProxied('removed.com', 8120, 8122),
"an incoming request to change.com": runner.assertProxied('change.com', 8120, 8124),
"an incoming request to dynamic1.com": runner.assertProxied('dynamic1.com', 8120, 8125)
}
},
"when passed a routing file": {
topic: function () {
fs.writeFileSync(routeFile, JSON.stringify(fileOptions));
this.server = runner.startProxyServerWithTable(8100, {
router: routeFile
}, this.callback);
},
"an incoming request to foo.com": runner.assertProxied('foo.com', 8100, 8101),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8100, 8102),
"an incoming request to unknown.com": runner.assertResponseCode(8100, 404),
"an incoming request to dynamic.com": {
"after the file has been modified": {
topic: function () {
var that = this,
data = fs.readFileSync(routeFile),
config = JSON.parse(data);
config.router['dynamic.com'] = "127.0.0.1:8103";
fs.writeFileSync(routeFile, JSON.stringify(config));
this.server.on('routes', function () {
runner.startTargetServer(8103, 'hello dynamic.com', function () {
request({
method: 'GET',
uri: options.source.protocols.http + '://localhost:8100',
headers: {
host: 'dynamic.com'
}
}, that.callback);
});
});
},
"should receive 'hello dynamic.com'": function (err, res, body) {
assert.equal(body, 'hello dynamic.com');
}
}
}
}
}
}).addBatch({
"When using an instance of ProxyTable combined with HttpProxy directly": {
topic: function () {
this.server = runner.startProxyServerWithTableAndLatency(8110, 100, {
router: {
'foo.com': 'localhost:8111',
'bar.com': 'localhost:8112'
}
}, this.callback);
},
"an incoming request to foo.com": runner.assertProxied('foo.com', 8110, 8111),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8110, 8112),
"an incoming request to unknown.com": runner.assertResponseCode(8110, 404)
}
}).addBatch({
"When the tests are over": {
topic: function () {
//fs.unlinkSync(routeFile);
return runner.closeServers();
},
"the servers should clean up": function () {
assert.isTrue(true);
}
}
}).export(module);