Skip to content

Commit 8fc3389

Browse files
committed
[test] add test for forwardstream
1 parent f4e9945 commit 8fc3389

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

lib/caronte/common.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ var common = exports;
1313
* @param {Object} Outgoing Base object to be filled with required properties
1414
* @param {Object} Options Config object passed to the proxy
1515
* @param {ClientRequest} Req Request Object
16+
* @param {String} Forward String to select forward or target
1617
1718
* @return {Object} Outgoing Object with all required properties set
1819
*
1920
* @api private
2021
*/
2122

22-
common.setupOutgoing = function(outgoing, options, req) {
23+
common.setupOutgoing = function(outgoing, options, req, forward) {
2324
['host', 'hostname', 'port', 'socketPath'/*, 'agent'*/].forEach(
24-
function(e) { outgoing[e] = options.target[e]; }
25+
function(e) { outgoing[e] = options[forward || 'target'][e]; }
2526
);
2627

2728
['method', 'path', 'headers'].forEach(

lib/caronte/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function createRightProxy(type) {
3333
return function(req, res) {
3434
var self = this,
3535
ev = 'caronte:' + type + ':';
36-
3736
//self.emit(ev + 'begin', req, res);
3837

3938
passes.forEach(function(pass) {

lib/caronte/streams/forward.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ require('util').inherits(ForwardStream, Writable);
5050
*/
5151

5252
ForwardStream.prototype.onPipe = function(request) {
53-
this.forwardReq = (options.ssl ? https : http).request(
54-
common.setupOutgoing(options.ssl || {}, options, request)
53+
this.forwardReq = (this.options.ssl ? https : http).request(
54+
common.setupOutgoing(this.options.ssl || {}, this.options, request, 'forward')
5555
);
5656

5757
this.forwardReq.on('error', function() {}); /** Fire and forget */
@@ -85,4 +85,4 @@ ForwardStream.prototype.onFinish = function() {
8585

8686
ForwardStream.prototype._write = function(chunk, encoding, clb) {
8787
this.forwardReq.write(chunk, encoding, clb);
88-
};
88+
};

test/lib-caronte-streams-forward-test.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var ForwardStream = require('../lib/caronte/streams/forward'),
1+
var caronte = require('../'),
2+
ForwardStream = require('../lib/caronte/streams/forward');
23
expect = require('expect.js'),
34
Writable = require('stream').Writable,
45
http = require('http');
@@ -22,16 +23,23 @@ describe('lib/caronte/passes/web.js', function () {
2223
});
2324

2425
describe('should pipe the request and finish it', function () {
25-
it('should make the request on pipe and finish it');
26-
var stubOptions = {
27-
target: {
28-
hostname : 'www.google.com',
29-
port : '80',
30-
path : '/'
31-
}
32-
};
26+
it('should make the request on pipe and finish it', function(done) {
27+
var result;
28+
29+
var p = caronte.createProxyServer({
30+
forward: 'http://127.0.0.1:8080'
31+
}).listen('8081')
3332

34-
var forwardProxy = new ForwardStream({});
33+
var s = http.createServer(function(req, res) {
34+
expect(req.method).to.eql('GET');
35+
s.close();
36+
p.close();
37+
done();
38+
});
3539

40+
s.listen('8080');
41+
42+
http.request('http://127.0.0.1:8081', function() {}).end();
43+
});
3644
});
37-
});
45+
});

0 commit comments

Comments
 (0)