Skip to content

Commit

Permalink
fix: better parse of the baseURL (#104)
Browse files Browse the repository at this point in the history
* This fixes the bug that was happening when using the nextPage and previousPage helpers

fixes #103
  • Loading branch information
lholmquist committed Jul 10, 2020
1 parent b29d1a4 commit 6c25d0c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/swapi-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ function sendRequest (options) {
}

function makeRequest (url) {
const parsedBaseUrl = new URL(BASE_URL);
const options = {
url: (url.includes(BASE_URL)) ? url : BASE_URL + url,
url: (url.includes(parsedBaseUrl.hostname)) ? url : BASE_URL + url,
headers: {
'User-Agent': 'swapi-node',
'SWAPI-Node-Version': require('../package.json').version
Expand Down
57 changes: 57 additions & 0 deletions test/swapi-paging-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { version } = require('../package.json');
const test = require('tape');

const BASE_URL = 'https://swapi.dev/api';
const BASE_HTTP_URL = 'http://swapi.dev/api';

nock.disableNetConnect();

Expand Down Expand Up @@ -38,6 +39,35 @@ test('returned value should have a nextPage added', async t => {
t.end();
});

test('returned value should have a nextPage added using http', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/people/')
.reply(200, {
count: 82,
next: `${BASE_HTTP_URL}/people/?page=2`,
previous: null
});

const result = await swapi.get(`${BASE_URL}/people/`);
t.equal(typeof result.nextPage, 'function', 'should be a next function');

nock(`${BASE_HTTP_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/people/?page=2')
.reply(200, {
count: 82,
next: `${BASE_URL}/people/?page=3`,
previous: null
});

await result.nextPage();
t.pass('success returned');
t.end();
});

test('returned value should have a previousPage added', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
Expand All @@ -64,3 +94,30 @@ test('returned value should have a previousPage added', async t => {
t.pass('success returned');
t.end();
});

test('returned value should have a previousPage added http', async t => {
nock(`${BASE_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/people/')
.reply(200, {
count: 82,
previous: `${BASE_HTTP_URL}/people/?page=2`
});

const result = await swapi.get(`${BASE_URL}/people/`);
t.equal(typeof result.previousPage, 'function', 'should be a next function');

nock(`${BASE_HTTP_URL}/`)
.matchHeader('User-Agent', 'swapi-node')
.matchHeader('SWAPI-Node-Version', version)
.get('/people/?page=2')
.reply(200, {
count: 82,
previous: `${BASE_URL}/people/?page=1`
});

await result.nextPage();
t.pass('success returned');
t.end();
});

0 comments on commit 6c25d0c

Please sign in to comment.