Browse Source

Load markdown file into memory and check if client requested html

pull/108/head
Diana Shkolnikov 10 years ago
parent
commit
1d45b20268
  1. 35
      controller/index.js
  2. 31
      test/unit/controller/index.js

35
controller/index.js

@ -5,17 +5,34 @@ var fs = require('fs');
function setup(){
function controller( req, res, next ){
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) {
var header = '# Pelias API\n';
var version = '### Version: ['+ pkg.version+ '](https://github.com/pelias/api/releases)\n';
var style = '<style>html{font-family:monospace}</style>';
if( err ) {
callback( err );
return;
}
text = '# Pelias API\n';
text += '### Version: ['+ pkg.version+ '](https://github.com/pelias/api/releases)\n';
text += content;
res.send(style + markdown.toHTML(header + version + content));
callback( null, text );
});
}
} else {
function controller( req, res, next ) {
getText( function ( err, content ) {
if( !err ) {
if( req.accepts( 'html' ) ) {
var style = '<style>html{font-family:monospace}</style>';
res.send( style + markdown.toHTML( content ) );
}
else {
// stats
res.json({
name: pkg.name,
@ -24,13 +41,11 @@ function setup(){
}
});
}
}
});
}
return controller;
}
module.exports = setup;

31
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('<style>html{font-family:monospace}</style>') === 0, 'style set to monospace');
t.end();
}};
controller( null, res );
controller( req, res );
});
};

Loading…
Cancel
Save