From d440ebf7b13dace496ab2a72a8ef51659b0aa816 Mon Sep 17 00:00:00 2001 From: Sambego Date: Tue, 31 Jan 2017 14:36:04 +0100 Subject: [PATCH 1/2] Respect pathname in rootUrl when using an absolute URL --- src/AbstractRequest.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/AbstractRequest.js b/src/AbstractRequest.js index ffd550a..e634f8c 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 = `${path.replace(/^\/|\/$/g, '')}/${resource}`; const query = this.query; const built = buildQuery({protocol, host, pathname, query}); From d8f49e1e1d4f4dbe6f470baa4102033eff46375f Mon Sep 17 00:00:00 2001 From: Sambego Date: Tue, 31 Jan 2017 15:07:04 +0100 Subject: [PATCH 2/2] Added a test for an absolute URL with extra pathnames --- src/AbstractRequest.js | 2 +- tests/AbstractRequest.test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/AbstractRequest.js b/src/AbstractRequest.js index e634f8c..002ddbc 100644 --- a/src/AbstractRequest.js +++ b/src/AbstractRequest.js @@ -45,7 +45,7 @@ export default class AbstractRequest { */ buildEndpoint(resource) { const {protocol, host, path} = parseUrl(this.rootUrl); - const pathname = `${path.replace(/^\/|\/$/g, '')}/${resource}`; + 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';