route-cache
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -68,2 +68,3 @@ 'use strict' | ||
debug('hit!!', key) | ||
if (value.contentType) res.set('Content-Type', value.contentType) | ||
if (value.isJson) { | ||
@@ -106,3 +107,3 @@ res.status(value.status).json(value.body) | ||
const body = data instanceof Buffer ? data.toString() : data | ||
if (res.statusCode < 400) cacheStore.set(key, { status: res.statusCode, body: body, isJson: isJson }, ttl) | ||
if (res.statusCode < 400) cacheStore.set(key, { status: res.statusCode, body: body, isJson: isJson, contentType: res.getHeader('Content-Type') }, ttl) | ||
@@ -185,2 +186,3 @@ // send this response to everyone in the queue | ||
debug('>> queued hit:', key, value.length) | ||
if (value.contentType) res.set('Content-Type', value.contentType) | ||
if (value.isJson) { | ||
@@ -187,0 +189,0 @@ res.status(value.status || 200).json(value.body) |
{ | ||
"name": "route-cache", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "express middleware for caching your routes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var noContentIndex = 0; | ||
var contentTypeIndex = 0; | ||
@@ -47,2 +48,8 @@ var app = express(); | ||
app.get('/content-type', function (req, res) { | ||
contentTypeIndex++; | ||
res.set('Content-Type', 'application/xml'); | ||
res.send('<xml><node>This is some content</node></xml>') | ||
}); | ||
it('1st res.send', function (done) { | ||
@@ -143,2 +150,24 @@ agent | ||
}) | ||
it('1st content-type', function (done) { | ||
agent | ||
.get('/content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}) | ||
it('2st content-type', function (done) { | ||
agent | ||
.get('/content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}) | ||
}); |
@@ -12,2 +12,3 @@ /* globals decribe */ | ||
let testindexRemove = 0 | ||
let contentTypeIndex = 0 | ||
@@ -35,2 +36,8 @@ describe('# RouteCache middleware test', function () { | ||
app.get('/content-type', routeCache.cacheSeconds(10), function (req, res) { | ||
contentTypeIndex++ | ||
res.set('Content-Type', 'application/xml') | ||
res.send('<xml><node>This is some content</node></xml>') | ||
}) | ||
app.get('/500', routeCache.cacheSeconds(10), function (req, res) { | ||
@@ -114,2 +121,24 @@ res.status(500).send('Internal server error: ' + Math.random()) | ||
it('1st content-type', function (done) { | ||
agent | ||
.get('/content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}) | ||
it('2st content-type', function (done) { | ||
agent | ||
.get('/content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}) | ||
it('1st Redirect to hello', function (done) { | ||
@@ -116,0 +145,0 @@ agent |
@@ -9,2 +9,3 @@ 'use strict' | ||
var noContentIndex = 0 | ||
var contentTypeIndex = 0 | ||
@@ -24,2 +25,8 @@ describe('Params / Querystrings', function () { | ||
app.get('/params-content-type', routeCache.cacheSeconds(10, '/params-test-content-type'), function (req, res) { | ||
contentTypeIndex++ | ||
res.set('Content-Type', 'application/xml') | ||
res.send('<xml><node>This is some content</node></xml>') | ||
}) | ||
var agent = request.agent(app) | ||
@@ -74,2 +81,35 @@ | ||
}) | ||
it('content-type without params', function (done) { | ||
agent | ||
.get('/params-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}) | ||
it('content-type with a=1', function (done) { | ||
agent | ||
.get('/params-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}) | ||
it('content-type with a=2', function (done) { | ||
agent | ||
.get('/params-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}) | ||
}) |
118
test/ttls.js
@@ -16,2 +16,3 @@ // mocha -R list test/no-cache | ||
var noContentIndex = 0; | ||
var contentTypeIndex = 0; | ||
@@ -28,2 +29,8 @@ app.get('/cache-disabled', routeCache.cacheSeconds(-1), function (req, res) { | ||
app.get('/cache-disabled-content-type', routeCache.cacheSeconds(-1), function (req, res) { | ||
contentTypeIndex++ | ||
res.set('Content-Type', 'application/xml') | ||
res.send('<xml><node>This is some content</node></xml>') | ||
}) | ||
it('Should get Hit #1', function (done) { | ||
@@ -67,2 +74,26 @@ agent | ||
it('Should contentTypeIndex is 1', function (done) { | ||
agent | ||
.get('/cache-disabled-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}); | ||
it('Should contentTypeIndex is 2 (after nextTick)', function (done) { | ||
process.nextTick(function () { | ||
agent | ||
.get('/cache-disabled-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 2) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}); | ||
}); | ||
}) | ||
@@ -73,2 +104,3 @@ | ||
var noContentIndex = 0; | ||
var contentTypeIndex = 0; | ||
@@ -85,2 +117,8 @@ app.get('/cache-zero', routeCache.cacheSeconds(0), function (req, res) { | ||
app.get('/cache-zero-content-type', routeCache.cacheSeconds(0), function (req, res) { | ||
contentTypeIndex++ | ||
res.set('Content-Type', 'application/xml') | ||
res.send('<xml><node>This is some content</node></xml>') | ||
}) | ||
it('Should get Hit #1', function (done) { | ||
@@ -146,2 +184,38 @@ agent | ||
it('Should contentTypeIndex is 1', function (done) { | ||
agent | ||
.get('/cache-zero-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}); | ||
it('Should contentTypeIndex is 1 (after nextTick)', function (done) { | ||
process.nextTick(function () { | ||
agent | ||
.get('/cache-zero-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}); | ||
}); | ||
it('Should contentTypeIndex is 2 (after 200ms delay)', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/cache-zero-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 2) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}, 200); | ||
}); | ||
}) | ||
@@ -152,2 +226,3 @@ | ||
var noContentIndex = 0; | ||
var contentTypeIndex = 0; | ||
@@ -164,2 +239,8 @@ app.get('/cache-1s', routeCache.cacheSeconds(1), function (req, res) { | ||
app.get('/cache-1s-content-type', routeCache.cacheSeconds(1), function (req, res) { | ||
contentTypeIndex++ | ||
res.set('Content-Type', 'application/xml') | ||
res.send('<xml><node>This is some content</node></xml>') | ||
}) | ||
it('Should get Hit #1', function (done) { | ||
@@ -225,4 +306,39 @@ agent | ||
it('Should contentTypeIndex is 1', function (done) { | ||
agent | ||
.get('/cache-1s-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}); | ||
it('Should contentTypeIndex is 1 (after 200ms delay)', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/cache-1s-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 1) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}, 200); | ||
}); | ||
it('Should contentTypeIndex is 2 (after 1200ms delay)', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/cache-1s-content-type') | ||
.expect(200, (error, response) => { | ||
assert.equal(contentTypeIndex, 2) | ||
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8') | ||
assert.equal(response.text, '<xml><node>This is some content</node></xml>') | ||
done() | ||
}) | ||
}, 1200); | ||
}); | ||
}); | ||
}); |
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
53573
1484