superagent-cache
Advanced tools
Comparing version 1.3.3 to 1.3.4
{ | ||
"name": "superagent-cache", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "Superagent with flexible built-in caching.", | ||
@@ -11,5 +11,6 @@ "main": "superagentCache.js", | ||
"devDependencies": { | ||
"mocha": "1.13.0", | ||
"es6-promise": "3.0.2", | ||
"expect": "1.6.0", | ||
"express": "3.4.3", | ||
"mocha": "1.13.0", | ||
"mock-localstorage": "0.1.3" | ||
@@ -16,0 +17,0 @@ }, |
@@ -113,4 +113,7 @@ var utils = require('./utils'); | ||
Request.prototype.end = function(cb){ | ||
var _this = this; | ||
var curProps = props; | ||
props = utils.resetProps(superagent.defaults); | ||
this.scRedirectsList = this.scRedirectsList || []; | ||
this.scRedirectsList = this.scRedirectsList.concat(this._redirectList); | ||
if(~supportedMethods.indexOf(this.method)){ | ||
@@ -127,6 +130,7 @@ var _this = this; | ||
_this._end(function (err, response){ | ||
if(err) { | ||
if(err){ | ||
return utils.callbackExecutor(cb, err, response, key); | ||
} | ||
else if(!err && response){ | ||
response.redirects = _this.scRedirectsList; | ||
if(curProps.prune){ | ||
@@ -133,0 +137,0 @@ response = curProps.prune(response); |
@@ -8,3 +8,2 @@ var name = require.resolve('superagent'); | ||
var cModule = require('cache-service-cache-module'); | ||
//var cacheModule = new cModule({backgroundRefreshInterval: 500}); | ||
require('../../superagentCache')(superagent, {backgroundRefreshInterval: 500}, null); | ||
@@ -48,2 +47,6 @@ //To make sure requiring a second time won't break anything | ||
app.get('/redirect', function(req, res){ | ||
res.redirect('/one'); | ||
}); | ||
app.listen(3000); | ||
@@ -50,0 +53,0 @@ |
@@ -8,3 +8,2 @@ var name = require.resolve('superagent'); | ||
var cModule = require('cache-service-cache-module'); | ||
//var cacheModule = new cModule({backgroundRefreshInterval: 500}); | ||
require('../../superagentCache')(superagent, {backgroundRefreshInterval: 500}, null); | ||
@@ -48,2 +47,6 @@ //To make sure requiring a second time won't break anything | ||
app.get('/redirect', function(req, res){ | ||
res.redirect('/one'); | ||
}); | ||
app.listen(3000); | ||
@@ -50,0 +53,0 @@ |
@@ -10,6 +10,18 @@ var name = require.resolve('superagent'); | ||
var storageMock = new mockStorage(); | ||
//var cacheModule = new cModule({backgroundRefreshInterval: 500}); | ||
require('../../superagentCache')(superagent, {backgroundRefreshInterval: 500, storageMock: storageMock}, null); | ||
var prune = function(res) { | ||
return { | ||
body: res.body, | ||
text: res.text, | ||
headers: res.headers, | ||
statusCode: res.statusCode, | ||
status: res.status, | ||
ok: res.ok, | ||
redirects: res.redirects | ||
} | ||
} | ||
require('../../superagentCache')(superagent, {backgroundRefreshInterval: 500, storageMock: storageMock}, {prune: prune}); | ||
//To make sure requiring a second time won't break anything | ||
require('../../superagentCache')(superagent, {backgroundRefreshInterval: 500, storageMock: storageMock}, null); | ||
require('../../superagentCache')(superagent, {backgroundRefreshInterval: 500, storageMock: storageMock}, {prune: prune}); | ||
@@ -50,5 +62,9 @@ var app = express(); | ||
app.get('/redirect', function(req, res){ | ||
res.redirect('/one'); | ||
}); | ||
app.listen(3000); | ||
function checkBrowserStorage(key, value, cb){ | ||
function checkBrowserStorage(key, value){ | ||
setTimeout(function(){ | ||
@@ -63,3 +79,2 @@ var data = storageMock.getItem('cache-module-storage-mock'); | ||
} | ||
if(cb) cb(); | ||
}, 1); | ||
@@ -116,2 +131,16 @@ } | ||
it('.get(redirect) .end() should cache the result of the redirect using the original request\'s key', function (done) { | ||
superagent | ||
.get('http://localhost:3000/redirect') | ||
.end(function (err, response, key){ | ||
expect(key).toBe('{"method":"GET","uri":"http://localhost:3000/redirect","params":null,"options":null}'); | ||
expect(response.body.key).toBe('one'); | ||
superagent.cache.get(key, function (err, response) { | ||
expect(response.body.key).toBe('one'); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
it('.get() then .put() should invalidate cache', function (done) { | ||
@@ -124,15 +153,12 @@ superagent | ||
expect(response.body.key).toBe('one'); | ||
checkBrowserStorage(key, 'one', function (){ | ||
superagent | ||
.put('localhost:3000/one') | ||
.end(function (err, response, key){ | ||
expect(response.body.key).toBe('put'); | ||
superagent.cache.get(key, function (err, response) { | ||
expect(response).toBe(null); | ||
checkBrowserStorage(key, false); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
superagent | ||
.put('localhost:3000/one') | ||
.end(function (err, response, key){ | ||
expect(response.body.key).toBe('put'); | ||
superagent.cache.get(key, function (err, response) { | ||
expect(response).toBe(null); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
@@ -143,3 +169,3 @@ } | ||
it('.get() then .del() should invalidate the generated cache key', function (done) { | ||
it('.get() then .del() should invalidate cache', function (done) { | ||
superagent | ||
@@ -151,15 +177,12 @@ .get('localhost:3000/one') | ||
expect(response.body.key).toBe('one'); | ||
checkBrowserStorage(key, 'one', function (){ | ||
superagent | ||
.del('localhost:3000/one') | ||
.end(function (err, response, key){ | ||
expect(response.body.key).toBe('delete'); | ||
superagent.cache.get(key, function (err, response){ | ||
expect(response).toBe(null); | ||
checkBrowserStorage(key, false); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
superagent | ||
.del('localhost:3000/one') | ||
.end(function (err, response, key){ | ||
expect(response.body.key).toBe('delete'); | ||
superagent.cache.get(key, function (err, response){ | ||
expect(response).toBe(null); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
@@ -172,2 +195,105 @@ } | ||
describe('res.redirects tests to ensure superagent-cache matches superagent', function(){ | ||
var Promise = require('es6-promise').Promise; | ||
beforeEach(function(){ | ||
superagent.cache.flush(); | ||
}); | ||
after(function(){ | ||
require('../../superagentCache')(superagent, {backgroundRefreshInterval: 500, storageMock: storageMock}, null); | ||
}); | ||
function superagentRunPromise(url, redirectsExpected) { | ||
return new Promise(function(resolve, reject) { | ||
superagent | ||
.get(url) | ||
.set('Accept', 'application/ld+json, application/json') | ||
.end(function(err, res) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
expect(res.redirects).toEqual(redirectsExpected); | ||
return resolve(); | ||
}); | ||
}); | ||
} | ||
function runNoRedirects(input) { | ||
return superagentRunPromise( | ||
'localhost:3000/one', | ||
[]); | ||
} | ||
var runWithRedirectsList = [ | ||
function(input) { | ||
return superagentRunPromise( | ||
'http://localhost:3000/redirect', | ||
['http://localhost:3000/one']); | ||
}, | ||
function(input) { | ||
return superagentRunPromise( | ||
'http://localhost:3000/redirect', | ||
['http://localhost:3000/one']); | ||
} | ||
]; | ||
// each of the following fails with superagent-cache, | ||
// but passes with just with superagent | ||
it('.get(noRedirect) then .get(redirect0) then .get(redirect0) then .get(redirect1)', function (done) { | ||
runNoRedirects() | ||
.then(runWithRedirectsList[0]) | ||
.then(runWithRedirectsList[0]) | ||
.then(runWithRedirectsList[1]) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('.get(noRedirect) then .get(redirect0) then .get(redirect1) then .get(redirect0)', function (done) { | ||
runNoRedirects() | ||
.then(runWithRedirectsList[0]) | ||
.then(runWithRedirectsList[1]) | ||
.then(runWithRedirectsList[0]) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('.get(noRedirect) then .get(redirect1) then .get(redirect0) then .get(redirect0)', function (done) { | ||
runNoRedirects() | ||
.then(runWithRedirectsList[1]) | ||
.then(runWithRedirectsList[0]) | ||
.then(runWithRedirectsList[0]) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('.get(redirect1) then .get(noRedirect) then .get(redirect0) then .get(redirect0)', function (done) { | ||
runWithRedirectsList[1]() | ||
.then(runNoRedirects) | ||
.then(runWithRedirectsList[0]) | ||
.then(runWithRedirectsList[0]) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('.get(redirect1) then .get(redirect0) then .get(noRedirect) then .get(redirect0)', function (done) { | ||
runWithRedirectsList[1]() | ||
.then(runWithRedirectsList[0]) | ||
.then(runNoRedirects) | ||
.then(runWithRedirectsList[0]) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('.get(redirect0) then .get(redirect1) then .get(redirect0) then .get(noRedirect)', function (done) { | ||
runWithRedirectsList[0]() | ||
.then(runWithRedirectsList[1]) | ||
.then(runWithRedirectsList[0]) | ||
.then(runNoRedirects) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
}); | ||
}); |
@@ -47,2 +47,6 @@ var name = require.resolve('superagent'); | ||
app.get('/redirect', function(req, res){ | ||
res.redirect('/one'); | ||
}); | ||
app.listen(3000); | ||
@@ -49,0 +53,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59787
1154
5