【问题标题】:Difficulty in Testing Response Header Fields Other Than Status Code难以测试状态码以外的响应标头字段
【发布时间】:2014-04-23 23:48:27
【问题描述】:

我正在使用 Express 框架。任务是从数据库中检索一个集合并将自定义消息插入返回给客户端的响应标头中(下面显示的 Node.js 代码有效,我可以在返回标头中看到我的自定义消息):

if (req.accepts('json')) {
    **res.header('Warning', 'my_customized_message');**
    res.header('Content-Type', 'application/json');
    res.send(res.locals.items, 200);
}

现在,我尝试使用“mocha”对这行新添加的代码使用 MOCHA 进行单元测试:

request = require('request');
should = require('should');

describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
    this.timeout(15000);
    it ('Check header message', function(done) {
        request.get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d', function(err, response, header) {
            response.statusCode.should.equal(200);                                     
            response.warning.should.equal('my_customized_message');   // Uncaught TypeError: Cannot read property 'should' of undefined
            done();
        } )
    } )
} )

如果我只测试 response.statusCode,没有问题。 MOCHA 测试成功通过。但是,如果我测试 response.warning,我会收到一条错误消息,指出未定义属性“应该”(我已经运行了 npm should --save-dev)。看起来“警告”没有被识别为“响应”的字段。但是,“警告”是响应字段之一。

如果我尝试测试 response.contentType,也会发生同样的错误。我认为“Content-Type”是一个众所周知的响应头字段。无论如何,我真正感兴趣的是测试“警告”标题。请帮忙。谢谢。

【问题讨论】:

标签: node.js express mocha.js


【解决方案1】:

我想你想要这个(responsehttp.IncomingMessage):

response.headers.warning.should.equal('my_customized_message');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2023-03-14
    • 2015-07-11
    相关资源
    最近更新 更多