How to check if two files have the same content?

前端 未结 6 584
说谎
说谎 2021-02-05 10:35

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

GET /files/ returns file as stream.

How can I assert in should.js

6条回答
  •  渐次进展
    2021-02-05 10:55

    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:

    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

提交回复
热议问题