Skip to content
Myr

web-service acceptance tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 1ea221a · Browse files · 3 files+163 added−121 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+163 added−121 removed

  • tests:+48 added−0 removed
  • other:+115 added−121 removed

3 files+163 added−121 removed

deleted:examples/web-service/app.js-121 removed
12/**3 * Module dependencies.4 */56var express = require('../../lib/express');78var app = express.createServer();910// configuration1112// if we wanted to supply more than JSON, we could13// use something similar to the content-negotiation14// example.1516// here we validate the API key,17// by mounting this middleware to /api/v118// meaning only paths prefixed with "/api/v1"19// will cause this middleware to be invoked2021app.use('/api/v1', function(req, res, next){22 var key = req.query['api-key'];2324 // key isnt present25 if (!key) return next(new Error('api key required'));2627 // key is invalid28 if (!~apiKeys.indexOf(key)) return next(new Error('invalid api key'));2930 // all good, store req.key for route access31 req.key = key;32 next();33});3435// position our routes above the error handling middleware,36// and below our API middleware, since we want the API validation37// to take place BEFORE our routes38app.use(app.router);3940// middleware with an arity of 4 are considered41// error handling middleware. When you next(err)42// it will be passed through the defined middleware43// in order, but ONLY those with an arity of 4, ignoring44// regular middleware.45app.use(function(err, req, res, next){46 // whatever you want here, feel free to populate47 // properties on `err` to treat it differently in here,48 // or when you next(err) set res.statusCode= etc.49 res.send(500, { error: err.message });50});5152// our custom JSON 404 middleware. Since it's placed last53// it will be the last middleware called, if all others54// invoke next() and do not respond.55app.use(function(req, res){56 res.send(404, { error: "Lame, can't find that" });57});5859/**60 * Generate our unique identifier.61 */6263function uid() {64 return [65 Math.random() * 0xffff | 066 , Math.random() * 0xffff | 067 , Math.random() * 0xffff | 068 , Date.now()69 ].join('-');70}7172// map of valid api keys, typically mapped to73// account info with some sort of database like redis.74// api keys do _not_ serve as authentication, merely to75// track API usage or help prevent malicious behavior etc.7677var apiKeys = [uid(), uid(), uid()];7879console.log('valid keys:\n ', apiKeys.join('\n '));8081// these two objects will serve as our faux database8283var repos = [84 { name: 'express', url: 'http://github.com/visionmedia/express' }85 , { name: 'stylus', url: 'http://github.com/learnboost/stylus' }86 , { name: 'cluster', url: 'http://github.com/learnboost/cluster' }87];8889var users = [90 { name: 'tobi' }91 , { name: 'loki' }92 , { name: 'jane' }93];9495var userRepos = {96 tobi: [repos[0], repos[1]]97 , loki: [repos[1]]98 , jane: [repos[2]]99};100101// we now can assume the api key is valid,102// and simply expose the data103104app.get('/api/v1/users', function(req, res, next){105 res.send(users);106});107108app.get('/api/v1/repos', function(req, res, next){109 res.send(repos);110});111112app.get('/api/v1/user/:name/repos', function(req, res, next){113 var name = req.params.name114 , user = userRepos[name];115 116 if (user) res.send(user);117 else next();118});119120app.listen(3000);121console.log('Express server listening on port 3000');
added:examples/web-service/index.js+115 added
12/**3 * Module dependencies.4 */56var express = require('../../');78var app = module.exports = express();910// create an error with .status. we11// can then use the property in our12// custom error handler (Connect repects this prop as well)1314function error(status, msg) {15 var err = new Error(msg);16 err.status = status;17 return err;18}1920// if we wanted to supply more than JSON, we could21// use something similar to the content-negotiation22// example.2324// here we validate the API key,25// by mounting this middleware to /api26// meaning only paths prefixed with "/api"27// will cause this middleware to be invoked2829app.use('/api', function(req, res, next){30 var key = req.query['api-key'];3132 // key isnt present33 if (!key) return next(error(400, 'api key required'));3435 // key is invalid36 if (!~apiKeys.indexOf(key)) return next(error(401, 'invalid api key'));3738 // all good, store req.key for route access39 req.key = key;40 next();41});4243// position our routes above the error handling middleware,44// and below our API middleware, since we want the API validation45// to take place BEFORE our routes46app.use(app.router);4748// middleware with an arity of 4 are considered49// error handling middleware. When you next(err)50// it will be passed through the defined middleware51// in order, but ONLY those with an arity of 4, ignoring52// regular middleware.53app.use(function(err, req, res, next){54 // whatever you want here, feel free to populate55 // properties on `err` to treat it differently in here.56 res.send(err.status || 500, { error: err.message });57});5859// our custom JSON 404 middleware. Since it's placed last60// it will be the last middleware called, if all others61// invoke next() and do not respond.62app.use(function(req, res){63 res.send(404, { error: "Lame, can't find that" });64});6566// map of valid api keys, typically mapped to67// account info with some sort of database like redis.68// api keys do _not_ serve as authentication, merely to69// track API usage or help prevent malicious behavior etc.7071var apiKeys = ['foo', 'bar', 'baz'];7273// these two objects will serve as our faux database7475var repos = [76 { name: 'express', url: 'http://github.com/visionmedia/express' }77 , { name: 'stylus', url: 'http://github.com/learnboost/stylus' }78 , { name: 'cluster', url: 'http://github.com/learnboost/cluster' }79];8081var users = [82 { name: 'tobi' }83 , { name: 'loki' }84 , { name: 'jane' }85];8687var userRepos = {88 tobi: [repos[0], repos[1]]89 , loki: [repos[1]]90 , jane: [repos[2]]91};9293// we now can assume the api key is valid,94// and simply expose the data9596app.get('/api/users', function(req, res, next){97 res.send(users);98});99100app.get('/api/repos', function(req, res, next){101 res.send(repos);102});103104app.get('/api/user/:name/repos', function(req, res, next){105 var name = req.params.name106 , user = userRepos[name];107 108 if (user) res.send(user);109 else next();110});111112if (!module.parent) {113 app.listen(3000);114 console.log('Express server listening on port 3000');115}
added:test/acceptance/web-service.js+48 added