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"} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What I can't seem to find is any way to do anything we want with an
Accept-Version
header value of1.9.0
.