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

       done();
    });
});

You have 3 solutions:

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.

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

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

Solution using file-compare and 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);
});