Skip to content

Commit

Permalink
Merge pull request #6 from madewithlove/fix/#5-keep-pathname-in-rooturl
Browse files Browse the repository at this point in the history
Respect pathname in rootUrl when using an absolute URL
  • Loading branch information
Sambego authored Jan 31, 2017
2 parents 7d6880d + d8f49e1 commit 1474460
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/AbstractRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ export default class AbstractRequest {
subrequests = {};

/**
* @param {String} pathname
* @param {String} resource
*
* @return {String}
*/
buildEndpoint(pathname) {
const {protocol, host} = parseUrl(this.rootUrl);
buildEndpoint(resource) {
const {protocol, host, path} = parseUrl(this.rootUrl);
const pathname = protocol && host ? `${path.replace(/^\/|\/$/g, '')}/${resource}` : resource;
const query = this.query;

const built = buildQuery({protocol, host, pathname, query});
Expand Down
12 changes: 12 additions & 0 deletions tests/AbstractRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('AbstractRequest', () => {
Authorization: 'Bearer foo',
},
}));
fetchMock.mock('http://google.com/foo/bar/options', (url, options) => ({url, options}));

beforeEach(() => {
requests = new AbstractRequest();
Expand Down Expand Up @@ -195,6 +196,17 @@ describe('AbstractRequest', () => {
});
});

it('can have absolute rootUrls with extra pathnames', () => {
requests.rootUrl = 'http://google.com/foo/bar';

return requests
.make('options')
.then(response => {
assert.equal(response.data.options.method, 'GET');
assert.equal(response.data.url, 'http://google.com/foo/bar/options');
});
});

it('can route to subrequest', () => {
requests.resource = 'users';

Expand Down

0 comments on commit 1474460

Please sign in to comment.