Skip to content

Commit

Permalink
fix(http-client): don't combine request url with base url when reques…
Browse files Browse the repository at this point in the history
…t url is absolute
  • Loading branch information
bryanrsmith committed Feb 11, 2016
1 parent 96be17d commit d1be3b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export class HttpClient {
}
}

const absoluteUrlRegexp = /^([a-z][a-z0-9+\-.]*:)?\/\//i;

function trackRequestStart() {
this.isRequesting = !!(++this.activeRequestCount);
}
Expand Down Expand Up @@ -170,7 +172,7 @@ function buildRequest(input, init) {
let parsedDefaultHeaders = parseHeaderValues(defaults.headers);
let requestInit = Object.assign({}, defaults, { headers: {} }, source, bodyObj);
let requestContentType = new Headers(requestInit.headers).get('Content-Type');
let request = new Request((this.baseUrl || '') + url, requestInit);
let request = new Request(getRequestUrl(this.baseUrl, url), requestInit);
if (!requestContentType && new Headers(parsedDefaultHeaders).has('content-type')) {
request.headers.set('Content-Type', new Headers(parsedDefaultHeaders).get('content-type'));
}
Expand All @@ -185,6 +187,14 @@ function buildRequest(input, init) {
return request;
}

function getRequestUrl(baseUrl, url) {
if (absoluteUrlRegexp.test(url)) {
return url;
}

return (baseUrl || '') + url;
}

function setDefaultHeaders(headers, defaultHeaders) {
for (let name in defaultHeaders || {}) {
if (defaultHeaders.hasOwnProperty(name) && !headers.has(name)) {
Expand Down
12 changes: 12 additions & 0 deletions test/http-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ describe('HttpClient', () => {
});
});

it('doesn\'t apply baseUrl to absolute URLs', (done) => {
fetch.and.returnValue(emptyResponse(200));
client.baseUrl = 'http://aurelia.io/';

client.fetch('https://example.com/test')
.then(() => {
let [request] = fetch.calls.first().args;
expect(request.url).toBe('https://example.com/test');
done();
});
});

it('applies default headers to requests with no headers', (done) => {
fetch.and.returnValue(emptyResponse(200));
client.defaults = { headers: { 'x-foo': 'bar' } };
Expand Down

0 comments on commit d1be3b4

Please sign in to comment.