Permalink
Browse files

Allowing HEAD to versioned

  • Loading branch information...
serby committed Dec 5, 2013
1 parent 412621d commit 49ca54fe90e8a07516b28bbb0326d60182d0cbfa
Showing with 52 additions and 4 deletions.
  1. +3 −1 lib/middleware/basic.js
  2. +3 −3 lib/middleware/mapped.js
  3. +23 −0 test/basic-versionator.test.js
  4. +23 −0 test/mapped-versionator.test.js
View
@@ -34,7 +34,9 @@ module.exports = function(version) {
},
middleware: function(req, res, next) {
- if (req.method !== 'GET') {
+
+ // We only do this on GET and HEAD requests
+ if ('GET' !== req.method && 'HEAD' !== req.method) {
return next()
}
View
@@ -40,11 +40,11 @@ module.exports = function(mappedPaths) {
},
middleware: function(req, res, next) {
-
- // We only do this on GET requests
- if (req.method !== 'GET') {
+ // We only do this on GET and HEAD requests
+ if ('GET' !== req.method && 'HEAD' !== req.method) {
return next()
}
+
req.url = reverseMap[req.url] || req.url
next()
}
@@ -50,6 +50,7 @@ describe('versionator', function() {
var app = appEngine.createServer(
versionator.createBasic('v0.1.2').middleware,
function(req, res) {
+ res.setHeader('x-url', req.url)
res.end(req.url)
}
)
@@ -68,6 +69,28 @@ describe('versionator', function() {
})
})
+ it('should allow GET', function(done) {
+
+ var app = startServer(9898)
+
+ request.get('http://localhost:9898/images/v0.1.2/sprite.png', function(error, response, data) {
+ data.should.eql('/images/sprite.png')
+ app.close()
+ done()
+ })
+ })
+
+ it('should allow HEAD', function(done) {
+
+ var app = startServer(9898)
+
+ request.head('http://localhost:9898/images/v0.1.2/sprite.png', function(error, response) {
+ response.headers['x-url'].should.eql('/images/sprite.png')
+ app.close()
+ done()
+ })
+ })
+
it('req.url is unchanged if no version match is found', function(done) {
var app = startServer(9899)
@@ -72,6 +72,7 @@ describe('versionator', function() {
var app = appEngine.createServer(
mapped.middleware,
function(req, res) {
+ res.setHeader('x-url', req.url)
res.end(req.url)
}
)
@@ -93,6 +94,28 @@ describe('versionator', function() {
})
})
+ it('should allow GET', function(done) {
+
+ var app = startServer({'/images/sprite.png': '/images/VERSIONHASH/sprite.png' }, 9901).app
+
+ request.get('http://localhost:9901/images/VERSIONHASH/sprite.png', function(error, response, data) {
+ data.should.eql('/images/sprite.png')
+ app.close()
+ done()
+ })
+ })
+
+ it('should allow HEAD', function(done) {
+
+ var app = startServer({'/images/sprite.png': '/images/VERSIONHASH/sprite.png' }, 9901).app
+
+ request.head('http://localhost:9901/images/VERSIONHASH/sprite.png', function(error, response) {
+ response.headers['x-url'].should.eql('/images/sprite.png')
+ app.close()
+ done()
+ })
+ })
+
it('req.url mapped url is mapped correctly', function(done) {
var app = startServer({'/images/sprite.png': '/images/VERSIONHASH/sprite.png' }, 9901).app

0 comments on commit 49ca54f

Please sign in to comment.