Skip to content

Instantly share code, notes, and snippets.

@robwilkerson
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwilkerson/6f965d5863388b60b6e5 to your computer and use it in GitHub Desktop.
Save robwilkerson/6f965d5863388b60b6e5 to your computer and use it in GitHub Desktop.
A simple test of restify versioning.
/* 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"}
@robwilkerson
Copy link
Author

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 to 1.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.

@robwilkerson
Copy link
Author

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