route-cache
Advanced tools
Comparing version 0.2.3 to 0.2.4
25
index.js
@@ -14,3 +14,5 @@ var Eidetic = require('eidetic'); | ||
var key = req.originalUrl; | ||
if (redirects[key]) return res.redirect(redirects[key]); | ||
if (redirects[key]) { | ||
return res.redirect(redirects[key].status, redirects[key].url); | ||
} | ||
@@ -79,6 +81,19 @@ var value = cacheStore.get(key); | ||
// subsequent requests. | ||
res.redirect = function(string) { | ||
var body = string instanceof Buffer ? string.toString() : string; | ||
redirects[key] = body; | ||
res.original_redirect(body); | ||
res.redirect = function(url) { | ||
var address = url; | ||
var status = 302; | ||
// allow statusCode for 301 redirect. See: https://github.com/expressjs/express/blob/master/lib/response.js#L857 | ||
if (arguments.length === 2) { | ||
if (typeof arguments[0] === 'number') { | ||
status = arguments[0]; | ||
address = arguments[1]; | ||
} else { | ||
console.log('res.redirect(url, status): Use res.redirect(status, url) instead'); | ||
status = arguments[1]; | ||
} | ||
} | ||
redirects[key] = {url: address, status:status}; | ||
res.original_redirect(status, address); | ||
}; | ||
@@ -85,0 +100,0 @@ |
{ | ||
"name": "route-cache", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "express middleware for caching your routes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,28 +0,34 @@ | ||
var request = require('supertest'), | ||
should = require('should'), | ||
routeCache = require('../index'), | ||
express = require('express'); | ||
should = require('should'), | ||
routeCache = require('../index'), | ||
express = require('express'); | ||
var app = express(); | ||
var testindex = 0; | ||
var testindexRemove = 0; | ||
var testindex = 0; | ||
var testindexRemove = 0; | ||
describe('# RouteCache middleware test', function(){ | ||
describe('# RouteCache middleware test', function () { | ||
var app = express(); | ||
app.get('/hello', routeCache.cacheSeconds(1), function(req, res){ | ||
testindex++; | ||
res.send('Hello ' + testindex) | ||
app.get('/hello', routeCache.cacheSeconds(1), function (req, res) { | ||
testindex++; | ||
res.send('Hello ' + testindex); | ||
}); | ||
app.get('/500', routeCache.cacheSeconds(10), function(req, res){ | ||
app.get('/500', routeCache.cacheSeconds(10), function (req, res) { | ||
res.status(500).send('Internal server error: ' + Math.random()); | ||
}); | ||
app.get('/redirect-to-hello', routeCache.cacheSeconds(1), function(req, res) { | ||
app.get('/redirect-to-hello', routeCache.cacheSeconds(1), function (req, res) { | ||
res.redirect('/hello'); | ||
}); | ||
app.get('/hello-remove', routeCache.cacheSeconds(3600), function(req, res){ | ||
app.get('/301-redirect-to-hello', routeCache.cacheSeconds(1), function (req, res) { | ||
res.redirect(301, '/hello'); | ||
}); | ||
app.get('/302-redirect-to-hello', routeCache.cacheSeconds(1), function (req, res) { | ||
res.redirect(302, '/hello'); | ||
}); | ||
app.get('/hello-remove', routeCache.cacheSeconds(3600), function (req, res) { | ||
testindexRemove++; | ||
@@ -33,3 +39,3 @@ res.send('Hello remove ' + testindexRemove) | ||
app.get('/hello-api', routeCache.cacheSeconds(3600), function (req, res) { | ||
res.json({ msg: 'Hello' }); | ||
res.json({msg: 'Hello'}); | ||
}); | ||
@@ -39,21 +45,21 @@ | ||
it('GET #1: Hello 1', function(done){ | ||
it('1st Hello', function (done) { | ||
agent | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
}); | ||
it('GET #2: Hello 1', function(done){ | ||
it('2nd Hello', function (done) { | ||
agent | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
}); | ||
it('GET #3: Hello 1', function(done){ | ||
it('1st Redirect to hello', function (done) { | ||
agent | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
.get('/redirect-to-hello') | ||
.expect(302, /\/hello/, done); | ||
}); | ||
it('GET #4: Redirect to hello 1', function(done) { | ||
it('2nd Redirect to hello', function (done) { | ||
agent | ||
@@ -64,23 +70,37 @@ .get('/redirect-to-hello') | ||
it('GET #5: Redirect to hello 2', function(done) { | ||
it('301 Redirect to hello', function (done) { | ||
agent | ||
.get('/redirect-to-hello') | ||
.get('/301-redirect-to-hello') | ||
.expect(301, /\/hello/, done); | ||
}); | ||
it('~ delayed 301 Redirect to hello', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/301-redirect-to-hello') | ||
.expect(301, /\/hello/, done); | ||
}, 1200); | ||
}); | ||
it('Explicit 302 Redirect to hello', function (done) { | ||
agent | ||
.get('/302-redirect-to-hello') | ||
.expect(302, /\/hello/, done); | ||
}); | ||
it('GET #6 ~ delayed: Hello 2', function(done){ | ||
setTimeout(function() { | ||
it('~ delayed: Hello 2', function (done) { | ||
setTimeout(function () { | ||
agent | ||
.get('/hello') | ||
.expect('Hello 2', done); | ||
.get('/hello') | ||
.expect('Hello 2', done); | ||
}, 1200); | ||
}); | ||
it('GET #7: Error states doesn\'t get cached', function(done){ | ||
it('Error states don\'t get cached', function (done) { | ||
var message; | ||
agent.get('/500').expect(500).end(function(req, res){ | ||
agent.get('/500').expect(500).end(function (req, res) { | ||
message = res.text; | ||
agent.get('/500').expect(500).end(function(req, res){ | ||
agent.get('/500').expect(500).end(function (req, res) { | ||
if (message == res.text) return done(Error('Got same error message as before')); | ||
@@ -93,24 +113,24 @@ done(); | ||
it('GET #8: test removeCache', function(done){ | ||
it('test removeCache', function (done) { | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 1').end(function(req, res){ | ||
.get('/hello-remove') | ||
.expect('Hello remove 1').end(function (req, res) { | ||
setTimeout(function() { | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 1').end(function(req, res){ | ||
setTimeout(function () { | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 1').end(function (req, res) { | ||
routeCache.removeCache('/hello-remove'); | ||
routeCache.removeCache('/hello-remove'); | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 2', done) | ||
}); | ||
}, 1200); | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 2', done) | ||
}); | ||
}, 1200); | ||
}); | ||
}); | ||
}); | ||
it('GET #9: res.json headers', function (done) { | ||
it('res.json headers', function (done) { | ||
agent | ||
@@ -126,2 +146,2 @@ .get('/hello-api') | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
10067
7
213