Nock headers - Error: Nock: No match for request

6.8k views Asked by At

Here's my example code for some reason Nock is failing for me as it can't match the URL when headers are added, when commenting the code like below the test passes. I can't figure out why nock doesn't understand the headers as the doc says to do this which i've done that: reqheaders: { 'authorization' : 'Basic Auth' }

Hoping someone might able to pick up something odd i'm doing.

const axios = require('axios');

async function postAPI(params) {
    let response1 = '';
    try {
        response1 =  await axios.post('http://someurl/test2', params);
        
    } catch(error) {
        throw error;
    }

    try {
        console.log("Im here", response1.data.sample)
    const response = await axios.get('http://testurl/testing', {
        // headers: {
        //   'authorization' : 'Basic Auth' //+ response1.data.sample 
        // }
       });
       return response.data;
    
    } catch(err) {
            console.log("Error", err)
    }
}

exports.postAPI = postAPI;

Test

it('make an api call - POST', async () => {

      nock('http://someurl')
      .persist()
      .defaultReplyHeaders({
        'access-control-allow-origin': '*',
        'access-control-allow-credentials': 'true' 
      })
        .post('/test2')
        .reply(200, {
                sample : 'test2'     
  });

    const test = nock('http://testurl', {
    //  reqheaders: {
    //    'authorization' : 'Basic Auth'
    //  }
    })
    .defaultReplyHeaders({
      'access-control-allow-origin': '*',
      'access-control-allow-credentials': 'true'
    })
      .get('/testing')
      .reply(200, { data : 'test' });

      const response = await postAPI();
      console.log("XXXX", response)
      expect(response.data).toEqual("test");
    });
1

There are 1 answers

1
juzeff On BEST ANSWER

Your reqheaders must match what you pass in axios requst headers.

reqheaders: {
    'authorization' : 'Basic Auth test2'
}

when you concatenate authorization header in main function remember to add a space between Auth and response1.data.sample :)

'authorization' : 'Basic Auth ' + response1.data.sample

I tried your code and it works. Full test:

const nock = require('nock');
const { postAPI } = require('./index');
const { expect } = require('chai');

describe('postapi', () => {
    it('make an api call - POST', async () => {

        nock('http://someurl')
        .defaultReplyHeaders({
          'access-control-allow-origin': '*',
          'access-control-allow-credentials': 'true' 
        })
          .post('/test2')
          .reply(200, {
                  sample : 'test2'     
        });
    
        nock('http://testurl', {
            reqheaders: {
            'authorization' : 'Basic Auth test2'
            }
        })
        .defaultReplyHeaders({
            'access-control-allow-origin': '*',
            'access-control-allow-credentials': 'true'
        })
        .get('/testing')
        .reply(200, { data : 'test' });
    
        const response = await postAPI();
        console.log("XXXX", response)
        expect(response.data).to.be.eql("test");
      });
});