mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
test(route) coverage
This commit is contained in:
parent
408ad49dc0
commit
26e1e276ff
3 changed files with 84 additions and 6 deletions
0
test/fixture/empty-file
Normal file
0
test/fixture/empty-file
Normal file
1
test/fixture/symlink-dir
Symbolic link
1
test/fixture/symlink-dir
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
empty-dir
|
||||
|
|
@ -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)];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue