Last active
August 29, 2015 14:20
-
-
Save robwilkerson/6f965d5863388b60b6e5 to your computer and use it in GitHub Desktop.
A simple test of restify versioning.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jshint node:true */ | |
'use strict'; | |
var restify = require("restify"), | |
server = restify.createServer({ version: '1.0.0' }); | |
server.pre(function (req, res, next) { | |
console.log('VERSION HEADER: %s', req.headers['accept-version']); | |
console.log('VERSION VIA RESTIFY: %s', req.version()); | |
if (!req.headers['accept-version']) { | |
console.log('----> no version sent...updating to 1.0.0'); | |
req._version = '1.0.0'; | |
} | |
return next(); | |
}); | |
var PATH1 = "/hello/:name"; | |
var PATH2 = "/goodbye/:name"; | |
server.get(PATH1, function(req,res,next) { | |
console.log('v0.0.0'); | |
res.json(201, { hello: 'v0.0.0' }); | |
return next(); | |
}); | |
server.get({ path: PATH1, version: ['1.8.0'] }, function(req,res,next) { | |
console.log('v1.8.0'); | |
res.json(201, { hello: 'v1.8.0' }); | |
return next(); | |
}); | |
/** | |
* ********** PATH 2 ********** | |
*/ | |
server.get(PATH2, function(req,res,next) { | |
console.log('v0.0.0'); | |
res.json(201, { hello: 'v0.0.0' }); | |
return next(); | |
}); | |
server.listen(8989); | |
/** | |
* REQUEST OUTPUT | |
*/ | |
$ curl -is localhost:8989/hello/me | |
HTTP/1.1 201 Created | |
Content-Type: application/json | |
Content-Length: 18 | |
Date: Mon, 11 May 2015 18:03:03 GMT | |
Connection: keep-alive | |
{"hello":"v0.0.0"} | |
$ curl -is localhost:8989/goodbye/me | |
HTTP/1.1 201 Created | |
Content-Type: application/json | |
Content-Length: 18 | |
Date: Mon, 11 May 2015 18:05:41 GMT | |
Connection: keep-alive | |
{"hello":"v0.0.0"} | |
$ curl -is localhost:8989/hello/me -H 'accept-version: 1.8.0' | |
HTTP/1.1 201 Created | |
Content-Type: application/json | |
Content-Length: 18 | |
Date: Mon, 11 May 2015 18:08:36 GMT | |
Connection: keep-alive | |
{"hello":"v1.8.0"} | |
$ curl -is localhost:8989/goodbye/me -H 'accept-version: 1.8.0' | |
HTTP/1.1 400 Bad Request | |
Content-Type: application/json | |
Content-Length: 79 | |
Date: Mon, 11 May 2015 18:09:20 GMT | |
Connection: keep-alive | |
{"code":"InvalidVersion","message":"1.8.0 is not supported by GET /goodbye/me"} | |
Quick, empirical notes:
- If no
Accept-Version
is sent, Restify always defaults to the latest version of a route if/when there is a mix of versioned and unversioned for the same route. - I can write middleware to catch a non-existent
Accept-Version
and set the version to1.0.0
which will send a mixed (versioned/unversioned) route to the unversioned option. - If a route is unversioned only (e.g.
server.get('/foo/bar', handler)
) the default version doesn't fallback to the unversioned route, but throws a 400 instead. I'm not sure I understand that, but this is what my eyes tell me.
What I can't seem to find is any way to do anything we want with an Accept-Version
header value of 1.9.0
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following scenarios are what we need:
We had also discussed a way to fallback to the latest available route e.g. if the app sends Accept-Version higher than what's currently supported it should use the latest available... this requirement is optional and as discussed we might even be better with having to update it manually to require additional oversight for each new version.
An example of the above scenario would be:
How we handle the last case is up to you