Skip to content

Commit

Permalink
adds gzip tests with new accumulator method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Whitley committed Jan 11, 2017
1 parent 902a154 commit b5a5fe9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"compression": "^1.6.2",
"express": "^4.14.0",
"mocha": "^3.0.2",
"redis": "^2.6.3",
Expand Down
7 changes: 0 additions & 7 deletions src/apicache.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ function ApiCache() {
}

function cacheResponse(key, value, duration) {
console.log('caching response', key, value)
var redis = globalOptions.redisClient
if (redis) {
redis.hset(key, "response", JSON.stringify(value))
Expand Down Expand Up @@ -126,14 +125,12 @@ function ApiCache() {

// patch res.write
res.write = function(content) {
// console.log('patched write', content)
accumulateContent(res, content);
return res._apicache.write.apply(this, arguments);
}

// patch res.end
res.end = function(content, encoding) {
// console.log('patched end', content, encoding)
if (shouldCacheResponse(res)) {

accumulateContent(res, content);
Expand Down Expand Up @@ -162,8 +159,6 @@ function ApiCache() {
'apicache-version': pkg.version
})

console.log('building response from', cacheObject)

// unstringify buffers
var data = cacheObject.data
if (data && data.type === 'Buffer') {
Expand Down Expand Up @@ -301,15 +296,13 @@ function ApiCache() {

// if not forced bypass of cache from client request, attempt cache hit
if (!req.headers['x-apicache-force-fetch']) {
console.log('attempting key hits for', key)
// console.log('current cache:', memCache.cache)
// attempt cache hit
var redis = globalOptions.redisClient
var cached = !redis ? memCache.getValue(key) : null

// send if cache hit from memory-cache
if (cached) {
console.log('cache hit in memory for', key)
// console log
var elapsed = new Date() - req.apicacheTimer
debug('sending cached (memory-cache) version of', key, logDuration(elapsed))
Expand Down
51 changes: 49 additions & 2 deletions test/apicache_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('.middleware {MIDDLEWARE}', function() {
})
})

it('properly returns a cached request', function(done) {
it('properly returns a cached JSON request', function(done) {
var mockAPI = require('./mock_api')('10 seconds')

request(mockAPI)
Expand All @@ -170,6 +170,31 @@ describe('.middleware {MIDDLEWARE}', function() {
})
})

it('properly returns a cached JSON request when gzipped', function(done) {
var mockAPI = require('./mock_api_gzip')('10 seconds')

request(mockAPI)
.get('/api/gzip/movies')
.end(function(err, res1) {
expect(res1.status).to.equal(200)
expect(res1.body.length).to.equal(2)
expect(mockAPI.requestsProcessed).to.equal(1)

request(mockAPI)
.get('/api/gzip/movies')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end(function(err, res2) {
expect(res2.status).to.equal(200)
expect(res2.body.length).to.equal(2)
expect(res2.body[0].title).to.equal('The Prestige')

expect(mockAPI.requestsProcessed).to.equal(1)
done()
})
})
})

it('returns cached response from write+end', function(done) {
var mockAPI = require('./mock_api')('10 seconds')

Expand All @@ -192,6 +217,28 @@ describe('.middleware {MIDDLEWARE}', function() {
})
})

it('returns cached response from write+end when gzipped', function(done) {
var mockAPI = require('./mock_api_gzip')('10 seconds')

request(mockAPI)
.get('/api/gzip/writeandend')
.end(function(err, res1, body) {
expect(res1.status).to.equal(200)
expect(res1.text).to.equal('abc')
expect(mockAPI.requestsProcessed).to.equal(1)

request(mockAPI)
.get('/api/gzip/writeandend')
.end(function(err, res2) {
expect(res2.status).to.equal(200)
expect(res2.text).to.equal('abc')

expect(mockAPI.requestsProcessed).to.equal(1)
done()
})
})
})

it('embeds store type and apicache version in cached responses', function(done) {
var mockAPI = require('./mock_api')('10 seconds')

Expand Down Expand Up @@ -318,7 +365,7 @@ describe('.middleware {MIDDLEWARE}', function() {
setTimeout(function() {
expect(mockAPI.apicache.getIndex().all).to.have.length(0)
done()
}, 15)
}, 25)
})

})
Expand Down
62 changes: 62 additions & 0 deletions test/mock_api_gzip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var movies = [{
title: 'The Prestige',
director: 'Christopher Nolan',
},{
title: 'Schindler\'s List',
director: 'Steven Spielberg'
}]

var instances = []

function MockAPI(expiration, options) {
// console.log('creating MockAPI with expiration in ' + expiration + ' with ApiCache config', options)
var express = require('express')
var compression = require('compression')
var apicache = require('../src/apicache').newInstance(options)

var app = express()
app.use(compression())

instances.push(this)

this.apicache = apicache
this.id = instances.length
this.app = app

// console.log('instantiating MockAPI:' + this.id + ' with expiration of ' + expiration)
// console.log('this.id vs this.apicache.id', this.id, this.apicache.id)
instances.forEach((instance, id) => {
// console.log('instance id:apicache.id', instance.id, instance.apicache.id)
if (instance.id !== this.id && this.apicache === instance.apicache) {
console.log('WARNING: SHARED APICACHE INSTANCE', id, this.id, this.apicache.id, instance.apicache.id)
}
if (instance.id !== this.id && this.app === instance.app) {
console.log('WARNING: SHARED EXPRESS INSTANCE', id, this.id)
}
})

app.use(this.apicache.middleware(expiration))

app.get('/api/gzip/movies', function(req, res) {
app.requestsProcessed++

res.json(movies)
})

app.get('/api/gzip/writeandend', function(req, res) {
app.requestsProcessed++

res.write('a')
res.write('b')
res.write('c')

res.end()
})

app.apicache = apicache
app.requestsProcessed = 0

return app
}

module.exports = function(expiration, config) { return new MockAPI(expiration, config) }

0 comments on commit b5a5fe9

Please sign in to comment.