对Favicon静态图像的哈希进行Mocha测试失败 [英] Failing mocha test on hash of favicon static image

查看:106
本文介绍了对Favicon静态图像的哈希进行Mocha测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mocha,request和SHA1哈希来编写集成测试,以确认Express所提供的favicon与文件系统上的favicon相同.我得到两个不同的哈希值,不知道为什么.编码可能会更改吗?

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

测试1通过,然后得到:"1)服务器静态测试处理了文件错误:预期的'b09865f78dae40afa5f31503c208f5474e1d76a9'等于'd3e242e289b401c18d6e96526f586abf06385108'"

Test 1 passes and then I get: "1) Server Static tests serves out the file Error: expected 'b09865f78dae40afa5f31503c208f5474e1d76a9' to equal 'd3e242e289b401c18d6e96526f586abf06385108'"

有什么想法为什么同一个图标在通过HTTP发送与读取文件系统时可能会以不同的方式进行哈希处理?

Any ideas why the same favicon might be hashing differently when being sent over HTTP versus read off the filesystem?

推荐答案

假设您使用的是请求模块从npm,您应该验证为body参数接收的对象的类型是Buffer.查看请求模块的源代码,我怀疑您得到的是String.当需要请求时,您可以尝试执行以下操作:

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

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

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

这篇关于对Favicon静态图像的哈希进行Mocha测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆