简体   繁体   中英

Using nock to mock superagent requests in mocha with certain request headers

I have the following code:

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

Now unless I am miss understanding the reqheaders , I would expect this test to fail being I am setting the request header to application/json instead of text/html but the test is passing.

Am I miss understanding the use of reqheaders ? How do I use nock to mock requests that have certain headers in the request?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM