如何模拟客户端中止

How to simulate Client aborted

我被困在我的一项测试中。在我的测试中,我需要检测客户端(浏览器)是否已取消或中止图像上传。我的问题是如何在连接中止时进行模拟。在我的控制器中,可以说我模拟了连接丢失,并且我的代码反应正确。但正如我提到的,问题仍然是如何从测试中触发它。 我使用 Mocha 3.2.0 作为测试套件,在控制器端我使用 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...

此模拟按预期工作。代码识别适当的条件(请求中止)。 当然,我不能离开这个节流阀和 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);

多方侦听该事件,并做出相应反应。

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

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

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

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

child.close();

或从过程本身:

process.exit(0);