我正在为我的客户端 - 服务器系统编写测试。服务器是一个正在运行和监听的节点进程,直到它被终止。
当我运行服务器,然后运行测试时,一切正常,测试通过。
我尝试编写运行服务器的bash
脚本,然后才运行测试文件。结果是服务器以某种方式被杀死。
bash
脚本:
#!/usr/bin/env bash
output=$(mktemp "${TMPDIR:-/tmp/}$(basename 0).XXX")
node Server/Server --port 3300 &> $output &
server_pid=$!
echo "Initializing the server with pid: $server_pid . Wait:\n"
until grep -i 'Server is listening on ip ' $output
do
sleep 1
done
ps # here the server is running
npm run test
a=$?
ps # here the server is not running
kill -9 $server_pid
echo "Server killed"
exit $a
测试:
'use strict';
const cp = require('child_process');
const ip = require('my-local-ip');
const utils = require('../util/utils');
describe('Server and client connectivity:', () => {
it('should transfer index.html file to client', () => {
let basePath = __dirname.split('/');
basePath.pop();
basePath = basePath.join('/');
const clientPath = basePath + '/Client';
cp.execSync('node ' + clientPath + '/request', ['-t', ip(), '-p', 3300]);
expect(utils.isFileExistsInDirectory(clientPath, 'index.html')).toBe(true);
});
});
通过从测试中删除execSync
命令,测试失败,但服务器仍在后台运行。
如何通过在脚本中运行服务器,execSync
杀死服务器,手动运行服务器然后测试 - 一切正常?
编辑:
输出cat $output
:
/Users/yinonc/Desktop/DDoSMitigation/server/Server.js:52
throw err;
^
Error: ENOENT: no such file or directory, open './index.html'
Server.js文件的相关部分:
const server = http.createServer((req, res) => {
console.log(`${req.method} request for ${req.url}`);
console.log(req.connection.remoteAddress);
let fileName = req.url;
if (utils.isFileExistsInDirectory(__dirname, fileName)) {
if (_.includes(fileName, '.html')) {
fs.readFile(`./${fileName}`, (err, data) => {
if (err) {
throw err; // --------- this is line 52 ----------
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
} else if (req.url.match(/.jpg$/)) {
const imgPath = path.join(__dirname, 'images', fileName);
const imgStream = fs.createReadStream(imgPath);
res.writeHead(200, {'Content-Type': 'image/jpeg'});
imgStream.pipe(res);
} else if (req.url.match(/.png$/)) {
const imgPath = path.join(__dirname, 'images', fileName);
const imgStream = fs.createReadStream(imgPath);
res.writeHead(200, {'Content-Type': 'image/png'});
imgStream.pipe(res);
}
} else {
fileName = cleaner.cleanFileName(fileName);
fs.writeFileSync(fileName, 'File Not Found. Please check your request.\n', err => {
if (err) {
throw err;
}
});
fs.readFile(`./${fileName}`, (err, data) => {
if (err) {
console.log("error 2");
throw err;
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(data);
});
}
});
答案 0 :(得分:1)
您需要使用path.join
修复相对路径。运行测试时,您的工作目录就是运行测试的地方,这会导致相对路径中断。
const server = http.createServer((req, res) => {
console.log(`${req.method} request for ${req.url}`);
console.log(req.connection.remoteAddress);
let fileName = req.url;
if (utils.isFileExistsInDirectory(__dirname, fileName)) {
if (_.includes(fileName, '.html')) {
fs.readFile(path.join(__dirname, `./${fileName}`), (err, data) => {
if (err) {
throw err;
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
} else if (req.url.match(/.jpg$/)) {
const imgPath = path.join(__dirname, 'images', fileName);
const imgStream = fs.createReadStream(imgPath);
res.writeHead(200, {'Content-Type': 'image/jpeg'});
imgStream.pipe(res);
} else if (req.url.match(/.png$/)) {
const imgPath = path.join(__dirname, 'images', fileName);
const imgStream = fs.createReadStream(imgPath);
res.writeHead(200, {'Content-Type': 'image/png'});
imgStream.pipe(res);
}
} else {
fileName = cleaner.cleanFileName(fileName);
fs.writeFileSync(fileName, 'File Not Found. Please check your request.\n', err => {
if (err) {
throw err;
}
});
fs.readFile(path.join(__dirname, `./${fileName}`), (err, data) => {
if (err) {
console.log("error 2");
throw err;
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(data);
});
}
});