How to check if two files have the same content?

前端 未结 6 578
说谎
说谎 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:45

    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.

提交回复
热议问题