diff --git a/src/AbstractRequest.js b/src/AbstractRequest.js index ffd550a..002ddbc 100644 --- a/src/AbstractRequest.js +++ b/src/AbstractRequest.js @@ -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}); diff --git a/tests/AbstractRequest.test.js b/tests/AbstractRequest.test.js index a2f2a3c..3dffe07 100644 --- a/tests/AbstractRequest.test.js +++ b/tests/AbstractRequest.test.js @@ -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(); @@ -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';