Skip to content

Commit 8c3e993

Browse files
committed
[test] Simplified tests. Added tests for experimental websocket support
1 parent f84880f commit 8c3e993

File tree

5 files changed

+171
-339
lines changed

5 files changed

+171
-339
lines changed

lib/node-http-proxy.js

+67-81
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ var sys = require('sys'),
2828
http = require('http'),
2929
events = require('events'),
3030
pool = require('./../vendor/pool/main'),
31-
eyes = require('eyes'),
3231
min = 0,
3332
max = 100;
3433

@@ -44,13 +43,9 @@ exports.createServer = function () {
4443
if (args[0]) port = args[0];
4544
if (args[1]) host = args[1];
4645

47-
var server = http.createServer(function (req, res){
46+
var server = http.createServer(function (req, res) {
4847
var proxy = new HttpProxy(req, res);
4948

50-
proxy.emitter.on('proxy', function (err, body) {
51-
server.emit('proxy', err, body);
52-
});
53-
5449
// If we were passed a callback to process the request
5550
// or response in some way, then call it.
5651
if(callback) {
@@ -61,15 +56,14 @@ exports.createServer = function () {
6156
}
6257
});
6358

64-
// If callback is empty - tunnel websocket request automatically
6559
if (!callback) {
66-
// WebSocket support
60+
// WebSocket support: if callback is empty tunnel
61+
// websocket request automatically
6762
server.on('upgrade', function(req, socket, head) {
6863
var proxy = new HttpProxy(req, socket, head);
6964

7065
// Tunnel websocket requests too
7166
proxy.proxyWebSocketRequest(port, host);
72-
7367
});
7468
}
7569

@@ -87,7 +81,6 @@ exports.setMax = function (value) {
8781
};
8882

8983
var HttpProxy = function (req, res, head) {
90-
this.emitter = new(events.EventEmitter);
9184
this.events = {};
9285
this.req = req;
9386

@@ -145,96 +138,90 @@ HttpProxy.prototype = {
145138
self.body = '';
146139

147140
// Open new HTTP request to internal resource with will act as a reverse proxy pass
148-
//var p = manager.getPool(port, server);
149-
//sys.puts('Current pool count for ' + req.headers.host + ":" + port + ' ' + p.clients.length + ', Busy: ' + p.getBusy() + ', Free: ' + p.getFree());
141+
var p = manager.getPool(port, server);
150142

151-
//p.on('error', function (err) {
143+
p.on('error', function (err) {
152144
// Remark: We should probably do something here
153145
// but this is a hot-fix because I don't think 'pool'
154146
// should be emitting this event.
155-
//});
147+
});
156148

157149
var client = http.createClient(port, server);
158-
var reverse_proxy = client.request(req.method, req.url, req.headers);
159-
160-
//p.request(req.method, req.url, req.headers, function (reverse_proxy) {
161-
162-
// Create an error handler so we can use it temporarily
163-
function error(obj) {
164-
var fn = function (err) {
165-
res.writeHead(500, {'Content-Type': 'text/plain'});
166-
167-
if(req.method !== 'HEAD') {
168-
res.write('An error has occurred: ' + JSON.stringify(err));
169-
}
150+
p.request(req.method, req.url, req.headers, function (reverse_proxy) {
151+
// Create an error handler so we can use it temporarily
152+
function error(obj) {
153+
var fn = function (err) {
154+
res.writeHead(500, {'Content-Type': 'text/plain'});
155+
156+
if(req.method !== 'HEAD') {
157+
res.write('An error has occurred: ' + JSON.stringify(err));
158+
}
170159

171-
// Response end may never come so removeListener here
172-
obj.removeListener('error', fn);
173-
res.end();
174-
};
160+
// Response end may never come so removeListener here
161+
obj.removeListener('error', fn);
162+
res.end();
163+
};
175164

176-
return fn;
177-
};
165+
return fn;
166+
};
178167

179-
// Add a listener for the connection timeout event
180-
var reverseProxyError = error(reverse_proxy),
181-
clientError = error(client);
168+
// Add a listener for the connection timeout event
169+
var reverseProxyError = error(reverse_proxy),
170+
clientError = error(client);
182171

183-
reverse_proxy.addListener('error', reverseProxyError);
184-
client.addListener('error', clientError);
172+
reverse_proxy.addListener('error', reverseProxyError);
173+
client.addListener('error', clientError);
185174

186-
// Add a listener for the reverse_proxy response event
187-
reverse_proxy.addListener('response', function (response) {
188-
if (response.headers.connection) {
189-
if (req.headers.connection) response.headers.connection = req.headers.connection;
190-
else response.headers.connection = 'close';
191-
}
192-
193-
// Set the response headers of the client response
194-
res.writeHead(response.statusCode, response.headers);
175+
// Add a listener for the reverse_proxy response event
176+
reverse_proxy.addListener('response', function (response) {
177+
if (response.headers.connection) {
178+
if (req.headers.connection) response.headers.connection = req.headers.connection;
179+
else response.headers.connection = 'close';
180+
}
195181

196-
// Status code = 304
197-
// No 'data' event and no 'end'
198-
if (response.statusCode === 304) {
199-
res.end();
200-
return;
201-
}
182+
// Set the response headers of the client response
183+
res.writeHead(response.statusCode, response.headers);
202184

203-
// Add event handler for the proxied response in chunks
204-
response.addListener('data', function (chunk) {
205-
if(req.method !== 'HEAD') {
206-
res.write(chunk, 'binary');
207-
self.body += chunk;
185+
// Status code = 304
186+
// No 'data' event and no 'end'
187+
if (response.statusCode === 304) {
188+
res.end();
189+
return;
208190
}
209-
});
210191

211-
// Add event listener for end of proxied response
212-
response.addListener('end', function () {
213-
// Remark: Emit the end event for testability
214-
self.emitter.emit('proxy', null, self.body);
215-
reverse_proxy.removeListener('error', reverseProxyError);
216-
res.end();
192+
// Add event handler for the proxied response in chunks
193+
response.addListener('data', function (chunk) {
194+
if(req.method !== 'HEAD') {
195+
res.write(chunk, 'binary');
196+
self.body += chunk;
197+
}
198+
});
199+
200+
// Add event listener for end of proxied response
201+
response.addListener('end', function () {
202+
reverse_proxy.removeListener('error', reverseProxyError);
203+
res.end();
204+
});
217205
});
218-
});
219206

220-
// Chunk the client request body as chunks from the proxied request come in
221-
req.addListener('data', function (chunk) {
222-
reverse_proxy.write(chunk, 'binary');
223-
})
207+
// Chunk the client request body as chunks from the proxied request come in
208+
req.addListener('data', function (chunk) {
209+
reverse_proxy.write(chunk, 'binary');
210+
})
224211

225-
// At the end of the client request, we are going to stop the proxied request
226-
req.addListener('end', function () {
227-
reverse_proxy.end();
228-
});
212+
// At the end of the client request, we are going to stop the proxied request
213+
req.addListener('end', function () {
214+
reverse_proxy.end();
215+
});
229216

230-
// On 'close' event remove 'error' listener
231-
client.addListener('close', function() {
232-
client.removeListener('error', clientError);
233-
});
217+
// On 'close' event remove 'error' listener
218+
client.addListener('close', function() {
219+
client.removeListener('error', clientError);
220+
});
234221

235-
self.unwatch(req);
222+
self.unwatch(req);
236223

237-
//});
224+
});
238225
},
239226

240227
proxyWebSocketRequest: function (port, server, host) {
@@ -251,8 +238,7 @@ HttpProxy.prototype = {
251238
return h;
252239
}
253240

254-
// WebSocket requests has
255-
// method = GET
241+
// WebSocket requests has method = GET
256242
if (req.method !== 'GET' || headers.upgrade.toLowerCase() !== 'websocket') {
257243
// This request is not WebSocket request
258244
return;

test/node-http-proxy-notarget.js

-140
This file was deleted.

0 commit comments

Comments
 (0)