I'm trying to use mocha, request, and a SHA1 hash to write an integration test to confirm that the favicon being served from Express is the same as the one on the file system. I get two different hashes, and can't figure out why. Is it possible the encoding is changing?
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" // Avoids DEPTH_ZERO_SELF_SIGNED_CERT error for self-signed certs
var request = require("request");
var crypto = require('crypto');
var fs = require('fs');
var favicon = crypto.createHash('sha1').update(fs.readFileSync(__dirname + '/../../public/img/favicon.ico')).digest('hex');
var app = require("../../server.js");
var expect = require('expect.js');
describe("Static tests", function () {
it("responds successfully", function (done) {
request.get("https://localhost:" + process.env.PORT + "/favicon.ico", function (err, res, body) {
// console.log(res)
expect(res.statusCode).to.be(200);
done();
});
});
it("serves out the file correctly", function (done) {
request.get("https://localhost:" + process.env.PORT + "/favicon.ico", function (err, res, body) {
// console.log(res)
expect(crypto.createHash('sha1').update(body).digest('hex')).to.be(favicon);
done();
});
});
});
Test 1 passes and then I get: "1) Server Static tests serves out the file Error: expected 'b09865f78dae40afa5f31503c208f5474e1d76a9' to equal 'd3e242e289b401c18d6e96526f586abf06385108'"
Any ideas why the same favicon might be hashing differently when being sent over HTTP versus read off the filesystem?
Assuming you are using the request module from npm, you should verify the type of the object you are receiving for the
body
argument is aBuffer
. Looking at the source for the request module, I suspect you are getting aString
instead. You might try doing the following when requiring request:That should tell the request module that you want a
Buffer
object by default.