Failing mocha test on hash of favicon static image

做~自己de王妃 提交于 2019-12-23 17:55:34

问题


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?


回答1:


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 a Buffer. Looking at the source for the request module, I suspect you are getting a String instead. You might try doing the following when requiring request:

var request = require("request").defaults({ encoding: null });

That should tell the request module that you want a Buffer object by default.



来源:https://stackoverflow.com/questions/16619980/failing-mocha-test-on-hash-of-favicon-static-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!