[test] add test for forwardstream

This commit is contained in:
yawnt 2013-08-28 14:49:27 +02:00
parent f4e9945856
commit 8fc3389367
4 changed files with 25 additions and 17 deletions

View File

@ -13,15 +13,16 @@ var common = exports;
* @param {Object} Outgoing Base object to be filled with required properties
* @param {Object} Options Config object passed to the proxy
* @param {ClientRequest} Req Request Object
* @param {String} Forward String to select forward or target
* 
* @return {Object} Outgoing Object with all required properties set
*
* @api private
*/
common.setupOutgoing = function(outgoing, options, req) {
common.setupOutgoing = function(outgoing, options, req, forward) {
['host', 'hostname', 'port', 'socketPath'/*, 'agent'*/].forEach(
function(e) { outgoing[e] = options.target[e]; }
function(e) { outgoing[e] = options[forward || 'target'][e]; }
);
['method', 'path', 'headers'].forEach(

View File

@ -33,7 +33,6 @@ function createRightProxy(type) {
return function(req, res) {
var self = this,
ev = 'caronte:' + type + ':';
//self.emit(ev + 'begin', req, res);
passes.forEach(function(pass) {

View File

@ -50,8 +50,8 @@ require('util').inherits(ForwardStream, Writable);
*/
ForwardStream.prototype.onPipe = function(request) {
this.forwardReq = (options.ssl ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, request)
this.forwardReq = (this.options.ssl ? https : http).request(
common.setupOutgoing(this.options.ssl || {}, this.options, request, 'forward')
);
this.forwardReq.on('error', function() {}); /** Fire and forget */
@ -85,4 +85,4 @@ ForwardStream.prototype.onFinish = function() {
ForwardStream.prototype._write = function(chunk, encoding, clb) {
this.forwardReq.write(chunk, encoding, clb);
};
};

View File

@ -1,4 +1,5 @@
var ForwardStream = require('../lib/caronte/streams/forward'),
var caronte = require('../'),
ForwardStream = require('../lib/caronte/streams/forward');
expect = require('expect.js'),
Writable = require('stream').Writable,
http = require('http');
@ -22,16 +23,23 @@ describe('lib/caronte/passes/web.js', function () {
});
describe('should pipe the request and finish it', function () {
it('should make the request on pipe and finish it');
var stubOptions = {
target: {
hostname : 'www.google.com',
port : '80',
path : '/'
}
};
it('should make the request on pipe and finish it', function(done) {
var result;
var p = caronte.createProxyServer({
forward: 'http://127.0.0.1:8080'
}).listen('8081')
var forwardProxy = new ForwardStream({});
var s = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
s.close();
p.close();
done();
});
s.listen('8080');
http.request('http://127.0.0.1:8081', function() {}).end();
});
});
});
});