【问题标题】:Nock headers - Error: Nock: No match for requestNock 标头 - 错误:Nock:与请求不匹配
【发布时间】:2021-02-03 15:28:29
【问题描述】:

这是我的示例代码,由于某种原因,Nock 对我来说失败了,因为它在添加标题时无法匹配 URL,当像下面的测试通过注释代码时。我不明白为什么 nock 不理解标题,因为文档说这样做我已经这样做了: reqheaders: { 'authorization' : 'Basic Auth' }

希望有人能找到我正在做的一些奇怪的事情。

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;

测试

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

【问题讨论】:

  • 您是否尝试过启用调试以查看为什么 Nock 与您的请求不匹配? github.com/nock/nock#debugging
  • 由于某种原因在工作代码上遇到困难,它没有点击 URL 并得到未定义的响应。就像在家里一样,除了身份验证头问题之外。打算改为尝试 axios mock,看看它是否对我更有效。还是谢谢。

标签: node.js testing jestjs nock


【解决方案1】:

您的 reqheaders 必须与您在 axios 请求标头中传递的内容相匹配。

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

当您在主函数中连接授权标头时,请记住在 Auth 和 response1.data.sample 之间添加一个空格 :)

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

我尝试了您的代码并且它有效。 全面测试:

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

【讨论】:

  • 谢谢,没有发现那个空间 ? 用 nock 做一些其他的东西,但很高兴知道我哪里出错了。
猜你喜欢
  • 2018-07-18
  • 2019-07-19
  • 2020-08-09
  • 2019-09-19
  • 2019-07-23
  • 1970-01-01
  • 2015-11-21
  • 2019-07-14
  • 2016-04-05
相关资源
最近更新 更多