Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion api/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ var defaultHeaders = {
* @private
*/
function parseConfig(config) {
var base = config.url ? url.parse(config.url, true) : {query: {}};
var base;
if (config.url) {
var resolved;
if (typeof location !== 'undefined') {
resolved = url.resolve(location.href, config.url);
} else {
resolved = config.url;
}
base = url.parse(resolved, true);
} else {
base = {query: {}};
}
if (config.query) {
config.path = url.format({
pathname: base.pathname || config.pathname || '/',
Expand Down
50 changes: 50 additions & 0 deletions test/api/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ describe('api/request', function() {
var parseConfig = req.parseConfig;
var defaultHeaders = {accept: 'application/json'};

var existingLocation;
beforeEach(function() {
existingLocation = global.location;
});
afterEach(function() {
global.location = existingLocation;
});

it('generates request options from a URL', function() {
var config = {
url: 'http://example.com'
Expand All @@ -465,6 +473,48 @@ describe('api/request', function() {
assert.deepEqual(parseConfig(config), options);
});

it('resolves a relative URL if location is defined', function() {
global.location = {
href: 'http://example.com/foo/bar.html'
};

var config = {
url: './relative/path/to/data.json'
};

var options = {
protocol: 'http:',
hostname: 'example.com',
port: '80',
method: 'GET',
path: '/foo/relative/path/to/data.json',
headers: defaultHeaders
};

assert.deepEqual(parseConfig(config), options);
});

it('works for root relative URLs', function() {
global.location = {
href: 'http://example.com/foo/bar.html'
};

var config = {
url: '/root/path/to/data.json'
};

var options = {
protocol: 'http:',
hostname: 'example.com',
port: '80',
method: 'GET',
path: '/root/path/to/data.json',
headers: defaultHeaders
};

assert.deepEqual(parseConfig(config), options);
});

it('adds user provided headers', function() {
var config = {
url: 'http://example.com',
Expand Down