diff --git a/controller/index.js b/controller/index.js index eb37f86e..0c2f5ab0 100644 --- a/controller/index.js +++ b/controller/index.js @@ -5,32 +5,47 @@ var fs = require('fs'); function setup(){ - function controller( req, res, next ){ - - fs.readFile('./DOCS.md', 'utf8', function (err, content) { - if (!err) { - var header = '# Pelias API\n'; - var version = '### Version: ['+ pkg.version+ '](https://github.com/pelias/api/releases)\n'; - var style = ''; - - res.send(style + markdown.toHTML(header + version + content)); - - } else { - // stats - res.json({ - name: pkg.name, - version: { - number: pkg.version - } - }); + var text = null; + function getText( callback ) { + if( text ) { + process.nextTick( callback.bind( null, null, text ) ); + return; + } + + fs.readFile( './DOCS.md', 'utf8', function ( err, content ) { + if( err ) { + callback( err ); + return; } - + text = '# Pelias API\n'; + text += '### Version: ['+ pkg.version+ '](https://github.com/pelias/api/releases)\n'; + text += content; + + callback( null, text ); }); + } + function controller( req, res, next ) { + getText( function ( err, content ) { + if( !err ) { + if( req.accepts( 'html' ) ) { + var style = ''; + res.send( style + markdown.toHTML( content ) ); + } + else { + // stats + res.json({ + name: pkg.name, + version: { + number: pkg.version + } + }); + } + } + }); } return controller; - } module.exports = setup; \ No newline at end of file diff --git a/test/unit/controller/index.js b/test/unit/controller/index.js index 4068cd5e..ea4f6991 100644 --- a/test/unit/controller/index.js +++ b/test/unit/controller/index.js @@ -11,21 +11,40 @@ module.exports.tests.interface = function(test, common) { }); }; -module.exports.tests.info = function(test, common) { - test('returns server info', function(t) { +module.exports.tests.info_json = function(test, common) { + test('returns server info in json', function(t) { var controller = setup(); + var req = { + accepts: function (format) { + t.equal(format, 'html', 'check for Accepts:html'); + return false; + } + }; var res = { json: function( json ){ t.equal(typeof json, 'object', 'returns json'); t.equal(typeof json.name, 'string', 'name'); t.equal(typeof json.version, 'object', 'version'); t.equal(typeof json.version.number, 'string', 'version number'); t.end(); - }, - send: function( html ){ - t.equal(typeof html, 'string', 'returns string'); + }}; + controller( req, res ); + }); +}; + +module.exports.tests.info_html = function(test, common) { + test('returns server info in json', function(t) { + var controller = setup(); + var req = { + accepts: function () { + return true; + } + }; + var res = { send: function( content ){ + t.equal(typeof content, 'string', 'returns string'); + t.assert(content.indexOf('') === 0, 'style set to monospace'); t.end(); }}; - controller( null, res ); + controller( req, res ); }); };