Browse Source

Change to readFileSync and update test to use proxyquire

pull/108/head
Diana Shkolnikov 10 years ago
parent
commit
99ee2d95a3
  1. 49
      controller/index.js
  2. 11
      package.json
  3. 17
      test/unit/controller/index.js

49
controller/index.js

@ -5,42 +5,23 @@ var fs = require('fs');
function setup(){
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 );
});
}
var text = '# Pelias API\n';
text += '### Version: ['+ pkg.version+ '](https://github.com/pelias/api/releases)\n';
text += fs.readFileSync( './DOCS.md', 'utf8');
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,
version: {
number: pkg.version
}
});
}
if (req.accepts('html')) {
if( text ) {
var style = '<style>html{font-family:monospace}</style>';
res.send(style + markdown.toHTML(text));
}
return;
}
// default behaviour
res.json({
name: pkg.name,
version: {
number: pkg.version
}
});
}

11
package.json

@ -8,7 +8,7 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "npm run unit && npm run ciao",
"test": "npm run unit",
"unit": "node test/unit/run.js | tap-spec",
"ciao": "node node_modules/ciao/bin/ciao -c test/ciao.json test/ciao",
"audit": "npm shrinkwrap; node node_modules/nsp/bin/nspCLI.js audit-shrinkwrap; rm npm-shrinkwrap.json;",
@ -37,17 +37,18 @@
"geojson": "^0.2.0",
"geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "0.0.12",
"pelias-suggester-pipeline": "2.0.2",
"is-object": "^1.0.1",
"markdown": "0.5.0",
"pelias-esclient": "0.0.25",
"markdown": "0.5.0"
"pelias-suggester-pipeline": "2.0.2"
},
"devDependencies": {
"ciao": "^0.3.4",
"jshint": "^2.5.6",
"nsp": "^0.3.0",
"precommit-hook": "^1.0.7",
"tape": "^2.13.4",
"proxyquire": "^1.4.0",
"tap-spec": "^0.2.0",
"nsp": "^0.3.0"
"tape": "^2.13.4"
}
}

17
test/unit/controller/index.js

@ -33,6 +33,20 @@ module.exports.tests.info_json = function(test, common) {
module.exports.tests.info_html = function(test, common) {
test('returns server info in html', function(t) {
var style = '<style>html{font-family:monospace}</style>';
var mockText = 'this text should show up in the html content';
var fsMock = {
readFileSync: function (path, format) {
t.equal(path, './DOCS.md', 'open DOCS.md file');
t.equal(format, 'utf8', 'file format');
return mockText;
}
};
var proxyquire = require('proxyquire');
var setup = proxyquire('../../../controller/index', { 'fs': fsMock });
var controller = setup();
var req = {
accepts: function () {
@ -41,7 +55,8 @@ module.exports.tests.info_html = function(test, common) {
};
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.assert(content.indexOf(style) === 0, 'style set');
t.assert(content.indexOf(mockText) !== -1, 'file content added');
t.end();
}};
controller( req, res );

Loading…
Cancel
Save