【问题标题】:Using nock to mock superagent requests in mocha with certain request headers使用 nock 在 mocha 中模拟具有某些请求标头的超级代理请求
【发布时间】:2015-03-25 22:12:31
【问题描述】:

我有以下代码:

var request = require('superagent');
var nock = require('nock')
var scope = nock('http://localhost:80', {
    reqheaders: {
        'Content-Type': 'text/html'
    }
});
scope.post('/api/test', {
    test: 'data'
})
.reply(200, {
    test: 'data2'
});

describe.only('test', function() {
    it('should fail', function(done) {
        request
        .post('/api/test')
        .set('Content-Type', 'application/json')
        .send({test: 'data'})
        .end(function(response) {
            expect(response.body).to.deep.equal({test: 'data2'});
            done();
        });
    });
});

现在,除非我无法理解 reqheaders,否则我预计此测试会失败,因为我将请求标头设置为 application/json 而不是 text/html,但测试通过了。

我想念reqheaders 的用法吗?如何使用 nock 模拟请求中具有某些标头的请求?

【问题讨论】:

    标签: javascript mocking mocha.js superagent nock


    【解决方案1】:

    我是个白痴,通过阅读更多文档,我意识到我需要使用.matchHeader()

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 2017-04-15
      • 2021-02-03
      • 2015-10-11
      相关资源
      最近更新 更多