断言具有相同内容的文件

时间:2022-02-26 16:46:13

I am using mocha/supertest/should.js to test my Rest Service

我正在使用mocha / supertest / should.js来测试我的Rest Service

GET /files/<hash> returns file as stream.

GET / files / 将文件作为流返回。

How can I assert in should.js that file contents are the same?

如何在should.js中断言文件内容是否相同?

it('should return file as stream', function (done) {
    var writeStream = fs.createWriteStream('test/fixtures/tmp.json');

    var req = api.get('/files/676dfg1430af3595');
    req.on('end', function(){
       var tmpBuf = fs.readFileSync('test/fixtures/tmp.json');
       var testBuf = fs.readFileSync('test/fixtures/test.json');

       // How to assert with should.js file contents are the same (tmpBuf == testBuf )
       // ...

       done();
    });
});

5 个解决方案

#1


1  

Surprisingly, no one has suggested Buffer.equals. That seems to be the fastest and simplest approach and has been around since v0.11.

令人惊讶的是,没有人建议使用Buffer.equals。这似乎是最快最简单的方法,自v0.11以来一直存在。

So your code would become tmpBuf.equals(testBuf)

所以你的代码将成为tmpBuf.equals(testBuf)

#2


3  

You have 3 solutions:

你有3个解决方案:

First:

Compare the result strings

比较结果字符串

tmpBuf.toString() === testBuf.toString();

Second:

Using a loop to read the buffers byte by byte

使用循环逐字节读取缓冲区

var index = 0,
    length = tmpBuf.length,
    match = true;

while (index < length) {
    if (tmpBuf[index] === testBuf[index]) {
        index++;
    } else {
        match = false;
        break;
    }
}

match; // true -> contents are the same, false -> otherwise

Third:

Using a third-party module like buffertools and buffertools.compare(buffer, buffer|string) method.

使用第三方模块,如buffertools和buffertools.compare(缓冲区,缓冲区|字符串)方法。

#3


3  

In should.js you can use .eql to compare Buffer's instances:

在should.js中,您可以使用.eql来比较Buffer的实例:

> var buf1 = new Buffer('abc');
undefined
> var buf2 = new Buffer('abc');
undefined
> var buf3 = new Buffer('dsfg');
undefined
> buf1.should.be.eql(buf1)
...
> buf1.should.be.eql(buf2)
...
> buf1.should.be.eql(buf3)
AssertionError: expected <Buffer 61 62 63> to equal <Buffer 64 73 66 67>
    ...
> 

#4


2  

Solution using file-compare and node-temp:

使用file-compare和node-temp的解决方案:

it('should return test2.json as a stream', function (done) {
    var writeStream = temp.createWriteStream();
    temp.track();

    var req = api.get('/files/7386afde8992');

    req.on('end', function() {
        comparator.compare(writeStream.path, TEST2_JSON_FILE, function(result, err) {
            if (err) {
                return done(err);
            }

            result.should.true;
            done();
        });
    });

    req.pipe(writeStream);
});

#5


0  

for comparing large files e.g. images when asserting file uploads a comparison of buffers or strings with should.eql takes ages. i recommend asserting the buffer hash with the crypto module:

用于比较大文件,例如断言文件上传时的图像缓冲区或字符串与should.eql的比较需要很长时间。我建议使用crypto模块声明缓冲区哈希:

const buf1Hash = crypto.createHash('sha256').update(buf1).digest();
const buf2Hash = crypto.createHash('sha256').update(buf2).digest();
buf1Hash.should.eql(buf2Hash);

an easier approach is asserting the buffer length like so:

一种更简单的方法是断言缓冲区长度,如下所示:

buf1.length.should.eql(buf2.length)

instead of using shouldjs as assertion module you can surely use a different tool

而不是使用shouldjs作为断言模块,你肯定可以使用不同的工具

#1


1  

Surprisingly, no one has suggested Buffer.equals. That seems to be the fastest and simplest approach and has been around since v0.11.

令人惊讶的是,没有人建议使用Buffer.equals。这似乎是最快最简单的方法,自v0.11以来一直存在。

So your code would become tmpBuf.equals(testBuf)

所以你的代码将成为tmpBuf.equals(testBuf)

#2


3  

You have 3 solutions:

你有3个解决方案:

First:

Compare the result strings

比较结果字符串

tmpBuf.toString() === testBuf.toString();

Second:

Using a loop to read the buffers byte by byte

使用循环逐字节读取缓冲区

var index = 0,
    length = tmpBuf.length,
    match = true;

while (index < length) {
    if (tmpBuf[index] === testBuf[index]) {
        index++;
    } else {
        match = false;
        break;
    }
}

match; // true -> contents are the same, false -> otherwise

Third:

Using a third-party module like buffertools and buffertools.compare(buffer, buffer|string) method.

使用第三方模块,如buffertools和buffertools.compare(缓冲区,缓冲区|字符串)方法。

#3


3  

In should.js you can use .eql to compare Buffer's instances:

在should.js中,您可以使用.eql来比较Buffer的实例:

> var buf1 = new Buffer('abc');
undefined
> var buf2 = new Buffer('abc');
undefined
> var buf3 = new Buffer('dsfg');
undefined
> buf1.should.be.eql(buf1)
...
> buf1.should.be.eql(buf2)
...
> buf1.should.be.eql(buf3)
AssertionError: expected <Buffer 61 62 63> to equal <Buffer 64 73 66 67>
    ...
> 

#4


2  

Solution using file-compare and node-temp:

使用file-compare和node-temp的解决方案:

it('should return test2.json as a stream', function (done) {
    var writeStream = temp.createWriteStream();
    temp.track();

    var req = api.get('/files/7386afde8992');

    req.on('end', function() {
        comparator.compare(writeStream.path, TEST2_JSON_FILE, function(result, err) {
            if (err) {
                return done(err);
            }

            result.should.true;
            done();
        });
    });

    req.pipe(writeStream);
});

#5


0  

for comparing large files e.g. images when asserting file uploads a comparison of buffers or strings with should.eql takes ages. i recommend asserting the buffer hash with the crypto module:

用于比较大文件,例如断言文件上传时的图像缓冲区或字符串与should.eql的比较需要很长时间。我建议使用crypto模块声明缓冲区哈希:

const buf1Hash = crypto.createHash('sha256').update(buf1).digest();
const buf2Hash = crypto.createHash('sha256').update(buf2).digest();
buf1Hash.should.eql(buf2Hash);

an easier approach is asserting the buffer length like so:

一种更简单的方法是断言缓冲区长度,如下所示:

buf1.length.should.eql(buf2.length)

instead of using shouldjs as assertion module you can surely use a different tool

而不是使用shouldjs作为断言模块,你肯定可以使用不同的工具