From c3c06f2872475af700ee27dccb5511b3ad7b555e Mon Sep 17 00:00:00 2001 From: Alex Boklin Date: Wed, 14 Mar 2018 10:55:14 +0100 Subject: [PATCH 1/3] Remove caching logic from http client --- src/http-client.js | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/http-client.js b/src/http-client.js index 8667268..e6b8bff 100644 --- a/src/http-client.js +++ b/src/http-client.js @@ -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 { @@ -30,26 +29,22 @@ 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)} ) - .then(checkStatus) - .then(res => { - httpCall.duration = Date.now()-httpCall.start; - return res.json(); - }); + .then(checkStatus) + .then(res => { + httpCall.duration = Date.now()-httpCall.start; + return res.json(); + }); - return cache[url]; + return res; } function checkStatus (res) { From f4cbeef50348b05fe4f4e1fdb6e1b87208155712 Mon Sep 17 00:00:00 2001 From: Alex Boklin Date: Wed, 14 Mar 2018 11:05:13 +0100 Subject: [PATCH 2/3] Fix indentation --- src/http-client.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/http-client.js b/src/http-client.js index e6b8bff..06bb65d 100644 --- a/src/http-client.js +++ b/src/http-client.js @@ -38,11 +38,11 @@ function get (url, params, opts) { base + url, {headers: Object.assign({}, getUserAgent(), headers)} ) - .then(checkStatus) - .then(res => { - httpCall.duration = Date.now()-httpCall.start; - return res.json(); - }); + .then(checkStatus) + .then(res => { + httpCall.duration = Date.now()-httpCall.start; + return res.json(); + }); return res; } From 50f91f61451f035d8d392746d16d811c1f2719a6 Mon Sep 17 00:00:00 2001 From: Alex Boklin Date: Wed, 14 Mar 2018 11:10:23 +0100 Subject: [PATCH 3/3] Amend tests --- test/http-client.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/http-client.test.js b/test/http-client.test.js index 78f3255..247582f 100644 --- a/test/http-client.test.js +++ b/test/http-client.test.js @@ -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(); @@ -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); }); }); @@ -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'); }); });