Browse Source

Change to readFileSync and update test to use proxyquire

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

33
controller/index.js

@ -5,35 +5,19 @@ var fs = require('fs');
function setup(){ function setup(){
var text = null; var text = '# Pelias API\n';
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 += '### Version: ['+ pkg.version+ '](https://github.com/pelias/api/releases)\n';
text += content; text += fs.readFileSync( './DOCS.md', 'utf8');
callback( null, text );
});
}
function controller( req, res, next ) { function controller( req, res, next ) {
getText( function ( err, content ) {
if( !err ) {
if (req.accepts('html')) { if (req.accepts('html')) {
if( text ) {
var style = '<style>html{font-family:monospace}</style>'; var style = '<style>html{font-family:monospace}</style>';
res.send( style + markdown.toHTML( content ) ); res.send(style + markdown.toHTML(text));
}
return;
} }
else { // default behaviour
// stats
res.json({ res.json({
name: pkg.name, name: pkg.name,
version: { version: {
@ -41,9 +25,6 @@ function setup(){
} }
}); });
} }
}
});
}
return controller; return controller;
} }

11
package.json

@ -8,7 +8,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js", "start": "node index.js",
"test": "npm run unit && npm run ciao", "test": "npm run unit",
"unit": "node test/unit/run.js | tap-spec", "unit": "node test/unit/run.js | tap-spec",
"ciao": "node node_modules/ciao/bin/ciao -c test/ciao.json test/ciao", "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;", "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": "^0.2.0",
"geojson-extent": "^0.3.1", "geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "0.0.12", "geopipes-elasticsearch-backend": "0.0.12",
"pelias-suggester-pipeline": "2.0.2",
"is-object": "^1.0.1", "is-object": "^1.0.1",
"markdown": "0.5.0",
"pelias-esclient": "0.0.25", "pelias-esclient": "0.0.25",
"markdown": "0.5.0" "pelias-suggester-pipeline": "2.0.2"
}, },
"devDependencies": { "devDependencies": {
"ciao": "^0.3.4", "ciao": "^0.3.4",
"jshint": "^2.5.6", "jshint": "^2.5.6",
"nsp": "^0.3.0",
"precommit-hook": "^1.0.7", "precommit-hook": "^1.0.7",
"tape": "^2.13.4", "proxyquire": "^1.4.0",
"tap-spec": "^0.2.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) { module.exports.tests.info_html = function(test, common) {
test('returns server info in html', function(t) { 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 controller = setup();
var req = { var req = {
accepts: function () { accepts: function () {
@ -41,7 +55,8 @@ module.exports.tests.info_html = function(test, common) {
}; };
var res = { send: function( content ){ var res = { send: function( content ){
t.equal(typeof content, 'string', 'returns string'); 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(); t.end();
}}; }};
controller( req, res ); controller( req, res );

Loading…
Cancel
Save