From 551a774793611948d4a4237b458467919dd1b71a Mon Sep 17 00:00:00 2001 From: Aquassaut Date: Sat, 13 Dec 2014 21:51:21 +0100 Subject: [PATCH] Tests added, with mocha, native asserts and rewire --- package.json | 9 +++++--- test/test_server.js | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 test/test_server.js diff --git a/package.json b/package.json index b5f98d7..8995948 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,13 @@ "rss": "^1.0.0", "temp": "^0.8.1" }, - "devDependencies": {}, + "devDependencies": { + "mocha": "^2.0.1", + "rewire": "^2.1.3" + }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "node app.js" + "test": "mocha", + "start": "node lib/app.js" }, "author": "aquassaut", "license": "CeCILL-B" diff --git a/test/test_server.js b/test/test_server.js new file mode 100644 index 0000000..486d9ab --- /dev/null +++ b/test/test_server.js @@ -0,0 +1,51 @@ +var assert = require('assert'), + rewire = require('rewire'); + +var server = rewire('../lib/server.js'); +server.__get__('log').level = 'silent'; + +describe("server", function() { + var func; + describe('_getResourceFromPath', function() { + + var func = server.__get__('_getResourceFromPath'); + it('should not return the file extension', function() { + assert.equal(func('resource.mp3'), 'resource'); + }); + + it('should not return the leading slash', function() { + assert.equal(func('/resource'), 'resource'); + + }); + + it('should un-percent encode resources', function() { + assert.equal(func('a%20b%20c'), 'a b c'); + }); + }); + describe('_findResourceFullPath', function() { + before(function() { + func = server.__get__('_findResourceFullPath'); + var mockFs = { + existsSync: function( path ) { + return path.indexOf('existing') !== -1; + } + }; + server.__set__('fs', mockFs); + return func; + }); + + it('should handle a directory or an array of directories', function() { + assert.equal(func('/existing/dir', 'ection'), '/existing/dir/ection'); + assert.equal(func(['/existing/dir'], 'ection'), '/existing/dir/ection'); + }); + + it('should return null if the resource does not exist', function() { + assert.equal(func('/non/existant/dir', 'a resource'), null); + }); + + it('should return the first result', function() { + assert.equal(func(['/existing', '/also/existing'], 'res'), '/existing/res'); + }); + + }); +}); \ No newline at end of file