关于favicon静态图像散列的失败摩卡testing

我正在尝试使用mocha,request和SHA1哈希编写集成testing,以确认从Express提供的图标与文件系统上的图标相同。 我得到了两个不同的哈希值,不知道为什么。 是否有可能编码正在改变?

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(); }); }); }); 

testing1通过,然后我得到:“1)服务器静态testing提供文件错误:预计'b09865f78dae40afa5f31503c208f5474e1d76a9'等于'd3e242e289b401c18d6e96526f586abf06385108'”

任何想法,为什么相同的favicon可能会通过HTTP发送与读取文件系统不同的哈希?

假设你正在使用npm的请求模块 ,你应该validation你正在接收的对象的types为body参数是一个Buffer 。 看看请求模块的来源,我怀疑你正在得到一个String 。 需要请求时您可以尝试执行以下操作:

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

这应该告诉请求模块默认情况下你想要一个Buffer对象。