Skip to content

Commit ead7567

Browse files
committed
[api] Updated http-proxy to work with vows
1 parent 30b68c1 commit ead7567

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

lib/node-proxy.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ exports.NodeProxy.prototype = {
4545
this.events = [];
4646

4747
this.onData = function () {
48-
self.events.push(["data"].concat(self.toArray(arguments)));
48+
self.events.push(['data'].concat(self.toArray(arguments)));
4949
};
5050
this.onEnd = function () {
51-
self.events.push(["end"].concat(self.toArray(arguments)));
51+
self.events.push(['end'].concat(self.toArray(arguments)));
5252
};
5353

54-
req.addListener("data", this.onData);
55-
req.addListener("end", this.onEnd);
54+
req.addListener('data', this.onData);
55+
req.addListener('end', this.onEnd);
5656
},
5757

5858
proxyRequest: function (server, port, req, res) {
@@ -67,45 +67,45 @@ exports.NodeProxy.prototype = {
6767
var reverse_proxy = c.request(req.method, req.url, req.headers);
6868

6969
// Add a listener for the connection timeout event
70-
reverse_proxy.connection.addListener('error', function(err) {
70+
reverse_proxy.connection.addListener('error', function (err) {
7171
res.writeHead(200, {'Content-Type': 'text/plain'});
7272

7373
if(req.method !== 'HEAD') {
74-
res.write('Not a HEAD request');
74+
res.write('An error has occurred: ' + sys.puts(JSON.stringify(err)));
7575
}
7676

7777
res.end();
7878
});
7979

8080
// Add a listener for the reverse_proxy response event
81-
reverse_proxy.addListener("response", function (response) {
81+
reverse_proxy.addListener('response', function (response) {
8282
// Set the response headers of the client response
8383
res.writeHead(response.statusCode, response.headers);
8484

8585
// Add event handler for the proxied response in chunks
86-
response.addListener("data", function (chunk) {
86+
response.addListener('data', function (chunk) {
8787
if(req.method !== 'HEAD') {
8888
res.write(chunk, 'binary');
89-
this.body += chunk;
89+
self.body += chunk;
9090
}
9191
});
9292

9393
// Add event listener for end of proxied response
94-
response.addListener("end", function () {
94+
response.addListener('end', function () {
95+
// Remark: Emit the end event for testability
96+
self.emitter.emit('end', null, self.body);
97+
9598
res.end();
9699
});
97100
});
98101

99102
// Chunk the client request body as chunks from the proxied request come in
100-
req.addListener("data", function (chunk) {
103+
req.addListener('data', function (chunk) {
101104
reverse_proxy.write(chunk, 'binary');
102105
})
103106

104107
// At the end of the client request, we are going to stop the proxied request
105-
req.addListener("end", function () {
106-
// Remark: Emit the end event for testability
107-
self.emitter.emit('something', self.body);
108-
108+
req.addListener('end', function () {
109109
reverse_proxy.end();
110110
});
111111

test/node-proxy-test.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
var vows = require('vows'),
1010
sys = require('sys'),
11+
eyes = require('eyes'),
1112
assert = require('assert'),
1213
http = require('http');
1314

@@ -19,7 +20,7 @@ var NodeProxy = require('node-proxy').NodeProxy;
1920
// Simple 'hello world' response for test purposes
2021
//
2122
var helloWorld = function(req, res) {
22-
res.sendHeader(200, {'Content-Type': 'text/plain'});
23+
res.writeHead(200, {'Content-Type': 'text/plain'});
2324
res.write('hello world')
2425
res.end();
2526
};
@@ -54,22 +55,20 @@ var startProxyTest = function () {
5455
return proxy;
5556
};
5657

57-
var proxy = startProxyTest();
58-
proxy.emitter.addListener('something', function (body) {
59-
sys.puts(body);
60-
})
6158

62-
/*vows.describe('node-proxy').addBatch({
59+
vows.describe('node-proxy').addBatch({
6360
"When an incoming request is proxied to the helloNode server" : {
6461
topic: function () {
62+
// Create the proxy and start listening
6563
var proxy = startProxyTest();
6664
proxy.emitter.addListener('end', this.callback);
65+
6766
var client = http.createClient(8080, '127.0.0.1');
68-
client.request('GET', '/');
67+
var request = client.request('GET', '/');
68+
request.end();
6969
},
70-
"it should received 'hello world'": function (num) {
71-
sys.puts('got callback');
72-
//assert.equal(body, 'hello world');
70+
"it should received 'hello world'": function (err, body) {
71+
assert.equal(body, 'hello world');
7372
}
7473
}
75-
}).export(module);*/
74+
}).export(module);

0 commit comments

Comments
 (0)