Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.
Open
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: 4 additions & 9 deletions src/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ function createClient (config) {
base: config.base || '',
headers: config.headers || {},
defaultParams: config.defaultParams || {},
timeline: config.timeline || [],
cache: config.cache || {}
timeline: config.timeline || []
};

return {
Expand All @@ -30,16 +29,12 @@ function get (url, params, opts) {
url = `${url}?${sortedQS}`;
}

const {base, headers, timeline, cache} = opts;
const cached = cache[url];
if (cached) {
return cached;
}
const {base, headers, timeline} = opts;

const httpCall = {url, start: Date.now()};
timeline.push(httpCall);

cache[url] = fetch(
const res = fetch(
base + url,
{headers: Object.assign({}, getUserAgent(), headers)}
)
Expand All @@ -49,7 +44,7 @@ function get (url, params, opts) {
return res.json();
});

return cache[url];
return res;
}

function checkStatus (res) {
Expand Down
12 changes: 6 additions & 6 deletions test/http-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test('http-client: non 2xx response codes', function (t) {
});
});

test('http-client: reuses already fired requests', function (t) {
test('http-client: does *not* reuse already fired requests', function (t) {
t.plan(2);
const {fetch, http} = prepare();

Expand All @@ -94,8 +94,8 @@ test('http-client: reuses already fired requests', function (t) {

Promise.all([p1, http.get('/two'), p2])
.then(() => {
t.equal(p1, p2);
t.equal(fetch.callCount, 2);
t.notEqual(p1, p2);
t.equal(fetch.callCount, 3);
});
});

Expand All @@ -108,10 +108,10 @@ test('http-client: sorts parameters', function (t) {

Promise.all([p1, http.get('/two', {omega: true, alfa: false}), p2])
.then(() => {
t.equal(p1, p2);
t.equal(fetch.callCount, 2);
t.notEqual(p1, p2);
t.equal(fetch.callCount, 3);
t.equal(fetch.firstCall.args[0], 'http://test.com/one?a=456&z=123');
t.equal(fetch.secondCall.args[0], 'http://test.com/two?alfa=false&omega=true');
t.equal(fetch.thirdCall.args[0], 'http://test.com/two?alfa=false&omega=true');
});
});

Expand Down