diff --git a/test/fixture/empty-file b/test/fixture/empty-file new file mode 100644 index 00000000..e69de29b diff --git a/test/fixture/symlink-dir b/test/fixture/symlink-dir new file mode 120000 index 00000000..10ad175b --- /dev/null +++ b/test/fixture/symlink-dir @@ -0,0 +1 @@ +empty-dir \ No newline at end of file diff --git a/test/server/route.js b/test/server/route.js index 0e54be5b..29aa535e 100644 --- a/test/server/route.js +++ b/test/server/route.js @@ -1,12 +1,14 @@ 'use strict'; const path = require('path'); +const fs = require('fs'); const test = require('tape'); const promisify = require('es6-promisify'); const pullout = require('pullout'); const request = require('request'); -const route = require('../../server/route'); +const routePath = '../../server/route'; +const route = require(routePath); const before = require('../before'); const warp = (fn, ...a) => (...b) => fn(...b, ...a); @@ -22,7 +24,6 @@ const getStr = (url) => { .catch(console.log); }; - test('cloudcmd: route: no args', (t) => { t.throws(route, /req could not be empty!/, 'should throw when no args'); t.end(); @@ -106,12 +107,8 @@ test('cloudcmd: route: no index', (t) => { const name = path.join(__dirname, '../../dist-dev/index.html'); const nameAfter = path.join(__dirname, '../../dist-dev/index1.html'); - const fs = require('fs'); - fs.renameSync(name, nameAfter); - const before = require('../before'); - before({}, (port, after) => { getStr(`http://localhost:${port}/`) .then((result) => { @@ -123,3 +120,83 @@ test('cloudcmd: route: no index', (t) => { }); }); +test('cloudcmd: route: file', (t) => { + const root = path.join(__dirname, '..', 'fixture', 'empty-file'); + const config = { + root, + }; + + before({config}, (port, after) => { + getStr(`http://localhost:${port}/fs/`) + .then((empty) => { + t.equal(empty, '', 'should equal'); + t.end(); + after(); + }); + }); +}); + +test('cloudcmd: route: symlink', (t) => { + const root = path.join(__dirname, '..', 'fixture', 'symlink-dir'); + const config = { + root, + }; + + before({config}, (port, after) => { + getStr(`http://localhost:${port}/fs/`) + .then((empty) => { + t.ok(empty.length, 'should return html document'); + t.end(); + after(); + }); + }); +}); + +test('cloudcmd: route: not found', (t) => { + const root = path.join(__dirname, '..', 'fixture'); + const config = { + root, + }; + + before({config}, (port, after) => { + getStr(`http://localhost:${port}/fs/file-not-found`) + .then((data) => { + t.ok(~data.indexOf('ENOENT: no such file or directory'), 'should return error'); + t.end(); + after(); + }); + }); +}); + +test('cloudcmd: route: realpath: error', (t) => { + const error = 'realpath error'; + const {realpath} = fs; + + fs.realpath = (name, fn) => { + fn(error); + fs.realpath = realpath; + }; + + clean('../before'); + clean(routePath); + + const before = require('../before'); + const root = path.join(__dirname, '..', 'fixture'); + const config = { + root, + }; + + before({config}, (port, after) => { + getStr(`http://localhost:${port}/fs/empty-file`) + .then((data) => { + t.ok(/^ENOENT/.test(data), 'should return error'); + t.end(); + after(); + }); + }); +}); + +function clean(path) { + delete require.cache[require.resolve(path)]; +} +