From e3e7458eafeef64efa8b43922a13353320bb5f0f Mon Sep 17 00:00:00 2001 From: Alberto Miranda Date: Fri, 23 Sep 2016 12:56:57 -0300 Subject: [PATCH 1/3] added option to return cached data before updating cache; added unit test --- README.md | 5 ++-- lib/requestify.js | 18 +++++++++++++- package.json | 3 ++- test/requestify-spec.js | 53 ++++++++++++++++++++++++++++++++++------- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 403cb84..6dcfc41 100644 --- a/README.md +++ b/README.md @@ -111,14 +111,15 @@ If null will be given, the body will be served as string. #### `timeout {number} ` Set a timeout (in milliseconds) for the request. -#### `cache {{ cache: boolean, expires: number }}` +#### `cache {{ cache: boolean, expires: number, cachedFirst: boolean }}` Requistify has built-in Redis based caching mechanism. For using this feature, set the cache property to true using the following object: ```javascript requestify.get('http://examples.com/api/foo', { cache: { cache: true, // Will set caching to true for this request. - expires: 3600 // Time for cache to expire in milliseconds + expires: 3600, // Time for cache to expire in milliseconds + cachedFirst: false // Will return cached data before updating cache } }); ``` diff --git a/lib/requestify.js b/lib/requestify.js index a595099..cb994a2 100644 --- a/lib/requestify.js +++ b/lib/requestify.js @@ -158,6 +158,14 @@ var Requestify = (function() { cache.get(request.getFullUrl()) .then(function(data) { if (!data || (expirationTime(data) <= new Date().getTime())) { + // cachedFirst indicates if we should return the cached data + // before updating it + if (request.cache.cachedFirst) { + // return cached data + defer.resolve(new Response(data.code, data.headers, data.body)); + } + + // make request and update cache call(request, defer); return; } @@ -180,7 +188,15 @@ var Requestify = (function() { /** * Execute HTTP request based on the given method and body * @param {string} url - The URL to execute - * @param {{ method: string, dataType: string, headers: object, body: object, cookies: object, auth: object }} options + * @param {{ + * method: string, + * dataType: string, + * headers: object, + * body: object, + * cookies: object, + * auth: object, + * cache: object + * }} options * @returns {Q.promise} - Returns a promise, once resolved || rejected, Response object is given */ request: function(url, options) { diff --git a/package.json b/package.json index 7e4f43c..c526a79 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "description": "Simplifies node HTTP request making (HTTP client) with caching support.", "version": "0.2.3", "scripts": { - "test": "mocha -R spec test/**/*.js test/*.js" + "test": "mocha -R spec test/**/*.js test/*.js", + "test-watch": "mocha --watch -R spec test/**/*.js test/*.js" }, "license": "MIT", "tags": [ diff --git a/test/requestify-spec.js b/test/requestify-spec.js index 7b73897..bb9b142 100644 --- a/test/requestify-spec.js +++ b/test/requestify-spec.js @@ -5,22 +5,31 @@ var mocha = require('mocha'), expect = require('chai').expect, rewire = require('rewire'), requestify = rewire('../lib/requestify.js'), - Q = require('q'); + Q = require('q'), + Response = require('../lib/response'); describe('Requestify', function() { - var cacheStub; + var cacheStub, + cachedDataStub; afterEach(function() { requestify.responseEncoding('utf8'); }); beforeEach(function() { + cachedDataStub = { + created: new Date().getTime(), + code: 200, + headers: 'Content-Type:application/javascript; charset=utf-8', + body: {rock: "yeah!"} + }; cacheStub = { setCacheTransporter: sinon.stub(), - get: sinon.stub(), + get: sinon.stub().returns(Q.resolve(cachedDataStub)), + // get: sinon.stub(), set: sinon.stub(), purge: sinon.stub(), - isTransportAvailable: sinon.stub() + isTransportAvailable: function() { return true } }; requestify.__set__('cache', cacheStub); @@ -41,18 +50,19 @@ describe('Requestify', function() { describe('#request()', function() { var httpStub, - httpsStub, - cacheStub; + httpsStub; beforeEach(function() { httpStub = sinon.stub().returns({ on: function() {}, - end: function() {} + end: function() {}, + abort: function() {} }); httpsStub = sinon.stub().returns({ on: function() {}, - end: function() {} + end: function() {}, + abort: function() {} }); requestify.__set__('http', { @@ -97,6 +107,33 @@ describe('Requestify', function() { method: 'POST' })).to.have.property('then'); }); + + it('Should return cached response before updating cache', function(done) { + var success = sinon.spy(); + requestify.cacheTransporter(); + var response = new Response(cachedDataStub.code, cachedDataStub.headers, cachedDataStub.body); + + requestify.request('http://wix.com', { + method: 'GET', + cache: { + cache: true, + cachedFirst: true, + expires: -1 // force update + } + }).then((response) => { + try { + expect(response).to.eql(response); + done(); + } catch (e) { + done(e); + } + }); + + // expect(success.called).to.equal(true); + // console.log('---- success?', success.called); + // expect(requestify.request.called).to.equal(true); + // expect(requestify.request.returned(cachedDataStub)).to.equal(true); + }); }); describe('Method specific public methods', function() { From 86d338f7b77855d85224fffb4df29d6308821dd9 Mon Sep 17 00:00:00 2001 From: Alberto Miranda Date: Fri, 23 Sep 2016 13:33:36 -0300 Subject: [PATCH 2/3] removed commented code --- test/requestify-spec.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/requestify-spec.js b/test/requestify-spec.js index bb9b142..fb76c54 100644 --- a/test/requestify-spec.js +++ b/test/requestify-spec.js @@ -128,11 +128,6 @@ describe('Requestify', function() { done(e); } }); - - // expect(success.called).to.equal(true); - // console.log('---- success?', success.called); - // expect(requestify.request.called).to.equal(true); - // expect(requestify.request.returned(cachedDataStub)).to.equal(true); }); }); From 262ba5e978229fa81a1b110624ad5c4daaea5fdb Mon Sep 17 00:00:00 2001 From: Alberto Miranda Date: Fri, 23 Sep 2016 13:40:16 -0300 Subject: [PATCH 3/3] removed arrow function --- test/requestify-spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/requestify-spec.js b/test/requestify-spec.js index fb76c54..3b237ce 100644 --- a/test/requestify-spec.js +++ b/test/requestify-spec.js @@ -120,7 +120,7 @@ describe('Requestify', function() { cachedFirst: true, expires: -1 // force update } - }).then((response) => { + }).then(function(response) { try { expect(response).to.eql(response); done();