mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
merge with @cronopio
This commit is contained in:
commit
0fb33810f5
@ -20,10 +20,12 @@ var passes = exports;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function checkMethodAndHeader (req, res, options) {
|
function checkMethodAndHeader (req, res, options) {
|
||||||
if (req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket') {
|
if (req.method !== 'GET' || !req.headers.upgrade) {
|
||||||
req.end();
|
req.end(); return true;
|
||||||
|
}
|
||||||
return true;
|
|
||||||
|
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
|
||||||
|
req.end(); return true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,8 @@ var Duplex = require('stream').Duplex,
|
|||||||
http = require('http'),
|
http = require('http'),
|
||||||
https = require('https');
|
https = require('https');
|
||||||
|
|
||||||
|
module.exports = ProxyStream;
|
||||||
|
|
||||||
function ProxyStream(options, res) {
|
function ProxyStream(options, res) {
|
||||||
Duplex.call(this);
|
Duplex.call(this);
|
||||||
|
|
||||||
@ -100,5 +102,3 @@ ProxyStream.prototype._read = function(size) {
|
|||||||
|
|
||||||
this.push(chunk);
|
this.push(chunk);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = ProxyStream;
|
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
var Duplex = require('stream').Duplex,
|
var Duplex = require('stream').Duplex,
|
||||||
common = require('common'),
|
common = require('../common'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
https = require('https');
|
https = require('https');
|
||||||
|
|
||||||
|
module.exports = WebsocketStream;
|
||||||
|
|
||||||
function WebsocketStream(options, res) {
|
function WebsocketStream(options, res) {
|
||||||
Duplex.call(this);
|
Duplex.call(this);
|
||||||
|
|
||||||
@ -34,7 +36,7 @@ WebsocketStream.prototype.onPipe = function(req) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WebsocketStream.prototye.onFinish = function() {
|
WebsocketStream.prototype.onFinish = function() {
|
||||||
this.proxyReq.end();
|
this.proxyReq.end();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -55,7 +57,4 @@ WebsocketStream.prototype._write = function(chunk, encoding, callback) {
|
|||||||
|
|
||||||
WebsocketStream.prototype._read = function(size) {
|
WebsocketStream.prototype._read = function(size) {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
WebsocketStream.prototype
|
|
||||||
87
test/lib-caronte-passes-ws-test.js
Normal file
87
test/lib-caronte-passes-ws-test.js
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
var caronte = require('../lib/caronte/passes/ws'),
|
||||||
|
expect = require('expect.js');
|
||||||
|
|
||||||
|
describe('lib/caronte/passes/ws.js', function () {
|
||||||
|
describe('#checkMethodAndHeader', function () {
|
||||||
|
it('should drop non-GET connections', function () {
|
||||||
|
var endCalled = false,
|
||||||
|
stubRequest = {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {},
|
||||||
|
end: function () {
|
||||||
|
// Simulate Stream.end() method when call
|
||||||
|
endCalled = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
|
||||||
|
expect(returnValue).to.be(true);
|
||||||
|
expect(endCalled).to.be(true);
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should drop connections when no upgrade header', function () {
|
||||||
|
var endCalled = false,
|
||||||
|
stubRequest = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {},
|
||||||
|
end: function () {
|
||||||
|
// Simulate Stream.end() method when call
|
||||||
|
endCalled = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
|
||||||
|
expect(returnValue).to.be(true);
|
||||||
|
expect(endCalled).to.be(true);
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should drop connections when upgrade header is different of `websocket`', function () {
|
||||||
|
var endCalled = false,
|
||||||
|
stubRequest = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
upgrade: 'anotherprotocol'
|
||||||
|
},
|
||||||
|
end: function () {
|
||||||
|
// Simulate Stream.end() method when call
|
||||||
|
endCalled = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
|
||||||
|
expect(returnValue).to.be(true);
|
||||||
|
expect(endCalled).to.be(true);
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return nothing when all is ok', function () {
|
||||||
|
var endCalled = false,
|
||||||
|
stubRequest = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
upgrade: 'websocket'
|
||||||
|
},
|
||||||
|
end: function () {
|
||||||
|
// Simulate Stream.end() method when call
|
||||||
|
endCalled = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
|
||||||
|
expect(returnValue).to.be(undefined);
|
||||||
|
expect(endCalled).to.be(false);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#XHeaders', function () {
|
||||||
|
// var stubRequest = {
|
||||||
|
// connection: {
|
||||||
|
// remoteAddress: '192.168.1.2',
|
||||||
|
// remotePort: '8080'
|
||||||
|
// },
|
||||||
|
// headers: {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// it('set the correct x-forwarded-* headers', function () {
|
||||||
|
// caronte.XHeaders(stubRequest, {}, { xfwd: true });
|
||||||
|
// expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
|
||||||
|
// expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
|
||||||
|
// expect(stubRequest.headers['x-forwarded-proto']).to.be('http');
|
||||||
|
// });
|
||||||
|
});
|
||||||
|
});
|
||||||
109
test/lib-caronte-streams-websockets-test.js
Normal file
109
test/lib-caronte-streams-websockets-test.js
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
var caronte = require('../'),
|
||||||
|
WebSocket = require('../lib/caronte/streams/websocket');
|
||||||
|
expect = require('expect.js'),
|
||||||
|
Duplex = require('stream').Duplex,
|
||||||
|
http = require('http');
|
||||||
|
|
||||||
|
|
||||||
|
describe('lib/caronte/streams/websocket.js', function () {
|
||||||
|
describe('WebSocket stream constructor', function () {
|
||||||
|
it('should be an instance of Duplex stream and get the correct options and methods', function () {
|
||||||
|
var stubOptions = {
|
||||||
|
key: 'value'
|
||||||
|
};
|
||||||
|
var WebSocketStream = new WebSocket(stubOptions);
|
||||||
|
|
||||||
|
expect(WebSocketStream).to.be.a(Duplex);
|
||||||
|
expect(WebSocketStream.options).to.eql({ key: 'value' });
|
||||||
|
expect(WebSocketStream.onPipe).to.be.a('function');
|
||||||
|
expect(WebSocketStream.onFinish).to.be.a('function');
|
||||||
|
expect(WebSocketStream._events).to.have.property('pipe');
|
||||||
|
expect(WebSocketStream._events).to.have.property('finish');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('caronte createWebSocketServer() method', function () {
|
||||||
|
// it('should make the request on pipe and finish it', function(done) {
|
||||||
|
// var proxy = caronte.createProxyServer({
|
||||||
|
// target: 'http://127.0.0.1:8080'
|
||||||
|
// }).listen('8081');
|
||||||
|
|
||||||
|
// var source = http.createServer(function(req, res) {
|
||||||
|
// expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
|
||||||
|
// source.close();
|
||||||
|
// proxy.close();
|
||||||
|
// done();
|
||||||
|
// });
|
||||||
|
|
||||||
|
// source.listen('8080');
|
||||||
|
|
||||||
|
// http.request({
|
||||||
|
// hostname: '127.0.0.1',
|
||||||
|
// port: '8081',
|
||||||
|
// method: 'POST',
|
||||||
|
// headers: {
|
||||||
|
// 'x-forwarded-for': '127.0.0.1'
|
||||||
|
// }
|
||||||
|
// }, function() {}).end();
|
||||||
|
// });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('caronte createProxyServer() method with response', function () {
|
||||||
|
// it('should make the request, handle response and finish it', function(done) {
|
||||||
|
// var proxy = caronte.createProxyServer({
|
||||||
|
// target: 'http://127.0.0.1:8080'
|
||||||
|
// }).listen('8081');
|
||||||
|
|
||||||
|
// var source = http.createServer(function(req, res) {
|
||||||
|
// expect(req.method).to.eql('GET');
|
||||||
|
// res.writeHead(200, {'Content-Type': 'text/plain'})
|
||||||
|
// res.end('Hello from ' + source.address().port);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// source.listen('8080');
|
||||||
|
|
||||||
|
// http.request({
|
||||||
|
// hostname: '127.0.0.1',
|
||||||
|
// port: '8081',
|
||||||
|
// method: 'GET',
|
||||||
|
// }, function(res) {
|
||||||
|
// expect(res.statusCode).to.eql(200);
|
||||||
|
|
||||||
|
// res.on('data', function (data) {
|
||||||
|
// expect(data.toString()).to.eql('Hello from 8080');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// res.on('end', function () {
|
||||||
|
// source.close();
|
||||||
|
// proxy.close();
|
||||||
|
// done();
|
||||||
|
// });
|
||||||
|
// }).end();
|
||||||
|
// });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('caronte createProxyServer() method with error response', function () {
|
||||||
|
// it('should make the request and response with error', function(done) {
|
||||||
|
// var proxy = caronte.createProxyServer({
|
||||||
|
// target: 'http://127.0.0.1:8080'
|
||||||
|
// }).listen('8081');
|
||||||
|
|
||||||
|
// http.request({
|
||||||
|
// hostname: '127.0.0.1',
|
||||||
|
// port: '8081',
|
||||||
|
// method: 'GET',
|
||||||
|
// }, function(res) {
|
||||||
|
// expect(res.statusCode).to.eql(500);
|
||||||
|
|
||||||
|
// res.on('data', function (data) {
|
||||||
|
// expect(data.toString()).to.eql('Internal Server Error');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// res.on('end', function () {
|
||||||
|
// proxy.close();
|
||||||
|
// done();
|
||||||
|
// });
|
||||||
|
// }).end();
|
||||||
|
// });
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user