Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix callback execution when request fails #6

Merged
merged 1 commit into from
Sep 29, 2015
Merged
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
8 changes: 8 additions & 0 deletions superagentCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ module.exports = function(agent, cache){
else{
if(curProps.doQuery){
_this._end(function (err, response){
if (err) {
return callbackExecutor(cb, err, response, key);
}

if(!err && response){
if(curProps.prune){
response = curProps.prune(response);
Expand Down Expand Up @@ -156,6 +160,10 @@ module.exports = function(agent, cache){
}
else{
this._end(function (err, response){
if (err) {
return callbackExecutor(cb, err, response, key);
}

if(!err && response){
superagent.cache.del(key, function (){
callbackExecutor(cb, err, response, key);
Expand Down
21 changes: 17 additions & 4 deletions test/server/superagent-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var superagent = require('superagent');
var cModule = require('cache-service-cache-module');
var cacheModule = new cModule({backgroundRefreshInterval: 500});
require('../../superagentCache')(superagent, cacheModule);

var app = express();

app.get('/one', function(req, res){
res.send(200, {key: 'one'});
});
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('Array', function(){
});

describe('superagentCache caching tests', function () {

it('.get() ._end() should bypass all caching logic', function (done) {
superagent
.get('localhost:3000/one')
Expand Down Expand Up @@ -321,10 +321,23 @@ describe('Array', function(){
);
});

describe('when response is not valid', function() {

it('.end() should not set \'err\' callback param on error', function (done) {
superagent
.get('localhost:3000/invalid')
.end(function (err, response){
expect(err).toExist();
done();
}
);
});
});

});

describe('superagentCache background refresh tests', function () {

it('.get() .expiration() .end() background refresh should not work if the chainable is not used', function (done) {
superagent
.get('localhost:3000/one')
Expand Down