Skip to content

Commit 32bb766

Browse files
committed
Allow using an aboslute URL when a baseURL is set
1 parent 457921f commit 32bb766

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/handle_request.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ function makeResponse(result, config) {
1010
}
1111

1212
function handleRequest(mockAdapter, resolve, reject, config) {
13-
config.url = config.url.slice(config.baseURL ? config.baseURL.length : 0);
13+
if (config.baseURL && config.url.substr(0, config.baseURL.length) === config.baseURL) {
14+
config.url = config.url.slice(config.baseURL ? config.baseURL.length : 0);
15+
}
1416
config.adapter = null;
1517

1618
var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data);
@@ -27,6 +29,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
2729
utils.settle(resolve, reject, makeResponse(handler.slice(2), config), mockAdapter.delayResponse);
2830
} else {
2931
var result = handler[2](config);
32+
// TODO throw a sane exception when return value is incorrect
3033
if (!(result.then instanceof Function)) {
3134
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse);
3235
} else {

test/basics.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ describe('MockAdapter basics', function() {
158158
});
159159
});
160160

161+
it('allows using an absolute URL when a baseURL is set', function() {
162+
instance.defaults.baseURL = 'http://www.example.org';
163+
164+
mock.onAny().reply(function(config) {
165+
return [200, config.url];
166+
});
167+
168+
return instance.get('http://www.foo.com/bar')
169+
.then(function(response) {
170+
expect(response.status).to.equal(200);
171+
expect(response.data).to.equal('http://www.foo.com/bar');
172+
});
173+
});
174+
161175
it('allows multiple consecutive requests for the mocked url', function() {
162176
mock.onGet('/foo').reply(200);
163177

0 commit comments

Comments
 (0)