Skip to content

Commit

Permalink
[test] add test for forwardstream
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Aug 28, 2013
1 parent f4e9945 commit 8fc3389
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/caronte/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion lib/caronte/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions lib/caronte/streams/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -85,4 +85,4 @@ ForwardStream.prototype.onFinish = function() {

ForwardStream.prototype._write = function(chunk, encoding, clb) {
this.forwardReq.write(chunk, encoding, clb);
};
};
30 changes: 19 additions & 11 deletions test/lib-caronte-streams-forward-test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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();
});
});
});
});

0 comments on commit 8fc3389

Please sign in to comment.