ryanzec
ryanzec

Reputation: 28030

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?

Upvotes: 1

Views: 1344

Answers (1)

ryanzec
ryanzec

Reputation: 28030

I'm an idiot, reading more through the docs, I realize I need to use .matchHeader().

Upvotes: 3

Related Questions