Skip to content

Commit a65021d

Browse files
fix tests for maintaining proxy path
1 parent 511b7b3 commit a65021d

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

lib/http-proxy/common.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var common = exports,
2+
path = require('path'),
23
url = require('url'),
34
extend = require('util')._extend;
45

@@ -57,17 +58,22 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
5758
) { outgoing.headers.connection = 'close'; }
5859
}
5960

60-
61+
6162
// the final path is target path + relative path requested by user:
62-
outgoing.path = options.target.path;
63+
var target = options[forward || 'target'];
64+
var targetPath = target
65+
? (target.path || '')
66+
: '';
6367

6468
//
6569
// Remark: Can we somehow not use url.parse as a perf optimization?
6670
//
67-
outgoing.path += !options.toProxy
71+
var outgoingPath = !options.toProxy
6872
? url.parse(req.url).path
6973
: req.url;
7074

75+
outgoing.path = path.join(targetPath, outgoingPath);
76+
7177
return outgoing;
7278
};
7379

test/lib-http-proxy-common-test.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ describe('lib/http-proxy/common.js', function () {
117117

118118
expect(outgoing.port).to.eql(443);
119119
});
120+
121+
it('should keep the original target path in the outgoing path', function(){
122+
var outgoing = {};
123+
common.setupOutgoing(outgoing, {target:
124+
{ path: 'some-path' }
125+
}, { url : 'am' });
126+
127+
expect(outgoing.path).to.eql('some-path/am');
128+
});
129+
130+
it('should keep the original forward path in the outgoing path', function(){
131+
var outgoing = {};
132+
common.setupOutgoing(outgoing, {
133+
target: {},
134+
forward: {
135+
path: 'some-path'
136+
}
137+
}, {
138+
url : 'am'
139+
}, 'forward');
140+
141+
expect(outgoing.path).to.eql('some-path/am');
142+
});
120143
});
121144

122145
describe('#setupSocket', function () {
@@ -144,4 +167,4 @@ describe('lib/http-proxy/common.js', function () {
144167
expect(socketConfig.keepalive).to.eql(true);
145168
});
146169
});
147-
});
170+
});

0 commit comments

Comments
 (0)