From 1d45b2026866c8544135cc535264da91e09549cb Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Thu, 23 Apr 2015 17:21:58 -0400 Subject: [PATCH] Load markdown file into memory and check if client requested html --- controller/index.js | 55 ++++++++++++++++++++++------------- test/unit/controller/index.js | 31 ++++++++++++++++---- 2 files changed, 60 insertions(+), 26 deletions(-) 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 ); }); };