Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
More coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
suprememoocow committed Mar 17, 2015
1 parent b8995ac commit 9b2a9c9
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 19 deletions.
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gulp.task('test', function() {
.src(['test/*.test.js'])
.pipe(mocha({
env: { },
timeout: 10000,
istanbul: {
dir: 'output/coverage/'
}
Expand Down
40 changes: 21 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,27 @@ HttpCache.prototype = {

self.stats.increment('miss');

var responseEtag = response.headers.etag;

if (response.headers['cache-control']) {
var cacheHeader = Wreck.parseCacheControl(response.headers['cache-control']);
var expiry = Date.now();
if (cacheHeader && cacheHeader['max-age']) {
expiry += cacheHeader['max-age'] * 1000;
}

if (responseEtag && response.statusCode === 200) {
/* Store the cache response async */
self._storeResponse(requestUrl, options.headers, response, expiry, responseEtag, body, requestStartTime, function(err) {
if (err) {
/* WARN */
debug('http.cache cache storage failure: ' + err, { exception: err});
self.stats.increment('backend.error');
}
});
}
if (response.headers) {
var responseEtag = response.headers.etag;

if (response.headers['cache-control']) {
var cacheHeader = Wreck.parseCacheControl(response.headers['cache-control']);
var expiry = Date.now();
if (cacheHeader && cacheHeader['max-age']) {
expiry += cacheHeader['max-age'] * 1000;
}

if (responseEtag && response.statusCode === 200) {
/* Store the cache response async */
self._storeResponse(requestUrl, options.headers, response, expiry, responseEtag, body, requestStartTime, function(err) {
if (err) {
/* WARN */
debug('http.cache cache storage failure: ' + err, { exception: err});
self.stats.increment('backend.error');
}
});
}
}
}

callback(null, response, body);
Expand Down
147 changes: 147 additions & 0 deletions test/request-http-cache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,5 +637,152 @@ describe('request-http-cache', function() {

});

describe('hash collisions', function() {
it('should treat hash collisions as cache missed', function(done) {
var mockBackend = new RequestHttpCache.backends.InMemory();
var k = keyGenerator('https://api.github.com/xxxx', {}, null);
var calls = 0;
mockBackend.store(k, {
url: 'https://api.github.com/yyyyy', // <-- NB
statusCode: 200,
etag: '2345',
expiry: Date.now() + 1000, // Not expired
headers: {
'content-type': MIME_TEXT
},
body: "WRONG"
}, function() {});

var httpRequestCache = new RequestHttpCache({
backend: mockBackend
});


var mockRequest = function(options, callback) {
assert.strictEqual(options.url, 'https://api.github.com/xxxx');
calls++;
callback(null, { statusCode: 200 }, "hello");
};

httpRequestCache.extension({ url: 'https://api.github.com/xxxx' }, function(err, response, body) {
if (err) return done(err);
assert.strictEqual(body, "hello");
assert.strictEqual(calls, 1);
done();
}, mockRequest);

});

it('should treat hash collisions as cache missed during content fetch', function(done) {
var mockBackend = new RequestHttpCache.backends.InMemory();
var k = keyGenerator('https://api.github.com/xxxx', {}, null);
var calls = 0;
mockBackend.store(k, {
url: 'https://api.github.com/xxxx', // <-- NB
statusCode: 200,
etag: '2345',
expiry: Date.now() + 1000, // Not expired
headers: {
'content-type': MIME_TEXT
},
body: "WRONG"
}, function() {});

mockBackend.getEtagExpiry = function(key, callback) {
mockBackend.store(k, {
url: 'https://api.github.com/yyyy', // <-- NB
statusCode: 200,
etag: '2345',
expiry: Date.now() + 1000, // Not expired
headers: {
'content-type': MIME_TEXT
},
body: "WRONG"
}, function() {});

return callback(null, {
url: 'https://api.github.com/xxxx', // <-- NB
etag: '2345',
expiry: Date.now() + 1000, // Not expired
});
};

var httpRequestCache = new RequestHttpCache({
backend: mockBackend
});

var mockRequest = function(options, callback) {
assert.strictEqual(options.url, 'https://api.github.com/xxxx');
calls++;
callback(null, { statusCode: 200 }, "hello");
};

httpRequestCache.extension({ url: 'https://api.github.com/xxxx' }, function(err, response, body) {
if (err) return done(err);
assert.strictEqual(body, "hello");
assert.strictEqual(calls, 1);
done();
}, mockRequest);

});

});

describe('backend errors', function() {
var mockBackend, httpRequestCache, mockRequest, calls;

beforeEach(function() {
calls = 0;
mockBackend = new RequestHttpCache.backends.InMemory();
httpRequestCache = new RequestHttpCache({
backend: mockBackend
});
mockRequest = function(options, callback) {
calls++;
callback(null, { statusCode: 200 }, "hello");
};
});

it('should deal with getVaryHeaders errors', function(done) {
mockBackend.getVaryHeaders = function(url, callback) {
assert.strictEqual(url, 'https://api.github.com/x');
callback(new Error('fail'));
};

httpRequestCache.extension({ url: 'https://api.github.com/x' }, function(err, response, body) {
assert.strictEqual(body, "hello");
assert.strictEqual(calls, 1);
done();
}, mockRequest);

});

it('should deal with getEtagExpiry errors', function(done) {
var k = keyGenerator('https://api.github.com/x', {}, null);

mockBackend.getEtagExpiry = function(key, callback) {
assert.strictEqual(key, k);
callback(new Error('fail'));
};

mockBackend.store(k, {
url: 'https://api.github.com/x', // <-- NB
statusCode: 200,
etag: '2345',
expiry: Date.now() - 1000, // Expired
body: "WRONG"
}, function() {});

httpRequestCache.extension({ url: 'https://api.github.com/x' }, function(err, response, body) {
assert.strictEqual(body, "hello");
assert.strictEqual(calls, 1);
done();
}, mockRequest);

});


});


});

0 comments on commit 9b2a9c9

Please sign in to comment.