gpt4 book ai didi

javascript - 如何模拟客户端中止

转载 作者:太空宇宙 更新时间:2023-11-04 00:19:43 25 4
gpt4 key购买 nike

我陷入了一项测试中。在我的测试中,我需要检测客户端(浏览器)已取消或中止图像上传。我的问题是如何模拟连接中止的情况。在我的 Controller 中,可以说我模拟了连接丢失,并且我的代码 react 正常。但正如我提到的,问题仍然是如何从测试中触发它。我使用 Mocha 3.2.0 作为测试套件,在 Controller 端我使用 multiparty .

这是原始代码:

return new Promise((resolve, reject) => {
let form = new multiparty.Form();
let fileCount = 0;
form.on('part', function (part) {
fileCount++;

let tmpFile = path.join(os.tmpDir(), `${userId}_${timePrefix}_${path.basename(part.filename)}`);
part.pipe(fs.createWriteStream(tmpFile));

part.on('end', () => {
resolve(fs.createReadStream(tmpFile));
});

part.on('error', function (error) {
reject(error);
});
});

form.on('error', (error) => {

reject(new LogicalError(
`Image input request is not valid`, errorCodes.VALIDATION.IMAGE_UPLOAD_DATA_INVALID);

});

form.parse(request);
}).then((imageStream) => {
//AWS S3 is hendled here...

现在我添加了一个新案例,我在其中触发客户端连接被取消。我用过Throttle 。 Throttle 模拟缓慢上传,并在超时时触发连接丢失。

return new Promise((resolve, reject) => {
let form = new multiparty.Form();
let fileCount = 0;
let throttle = new Throttle(1); //1 = 1 byte. 1000 = 1kb... all per second


form.on('part', function (part) {
fileCount++;

let tmpFile = path.join(os.tmpDir(), `${userId}_${timePrefix}_${path.basename(part.filename)}`);
//part.pipe(fs.createWriteStream(tmpFile));
//Slows down the upload
part.pipe(throttle).pipe(fs.createWriteStream(tmpFile));

//trigger connection aborted
setTimeout(function () {
request.emit('aborted');
},10);


part.on('end', () => {
resolve(fs.createReadStream(tmpFile));
});

part.on('error', function (error) {
reject(error);
});
});

form.on('error', (error) => {

let err = null;

if (error.message.includes('Request aborted')){
err = new LogicalError(
`Client aborted image upload process`, errorCodes.VALIDATION.IMAGE_UPLOAD_CLIENT_REQUEST_ABORTED);
} else {
err = new LogicalError(
`Image input request is not valid`, errorCodes.VALIDATION.IMAGE_UPLOAD_DATA_INVALID);
}
reject(err);

});

form.parse(request);
}).then((imageStream) => {
//AWS S3 is hendled here...

该模拟按预期工作。代码识别正确的条件(请求中止)。当然,我不能离开这个 throttle 和setTimeout。如何从测试中触发它?

这是当前的测试,正在上传图像,并且以 Controller 为目标。使用supertest :

describe('ImageControllerTest', function () {
it('should upload image with name for the given user', (done) => {
request(app)
.put('path-to-controller/test-image.jpg')
.attach('file', imagePath.testImagePath)
.set(cons.TOKEN.HEADERS)
.expect((res) => {
//series of expect statements.
})
.expect(200, done);
});
});

在我的第二次尝试(代码在上面)中,我通过这样做模拟了连接中止:

setTimeout(function () {
request.emit('aborted');
},10);

多方监听该事件,并做出相应 react 。

我怎样才能从测试中做到这一点?

最佳答案

简单的方法是 - 使用以下方法在子流程中创建客户端:

const fork = require('child_process').fork;
const path = require('path');
const child = fork(path.join(__dirname, './some-sub.js'));

当您需要触发取消时 - 只需关闭进程即可:

child.close();

或者从流程本身:

process.exit(0);

关于javascript - 如何模拟客户端中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44880096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com