hellboy hellboy - 1 year ago 71
Node.js Question

Asserting files that have the same content

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

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

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

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 )
// ...


Answer Source

You have 3 solutions:


Compare the result strings

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


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]) {
    } else {
        match = false;

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


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download