首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Nock模拟NodeJS请求和响应

用Nock模拟NodeJS请求和响应
EN

Stack Overflow用户
提问于 2016-11-30 17:06:44
回答 1查看 5.3K关注 0票数 0

我试图掌握工具诺克,以便模拟执行调用的代码中的请求和响应。我使用npm请求作为一个简单的HTTP客户机来请求后端REST、希望库的Chai和运行我的测试的Mocha。下面是用于测试的代码:

代码语言:javascript
复制
 var nock = require('nock');
 var storyController = require('../modules/storyController');

 var getIssueResponse = {
   //JSON object that we expect from the API response.
 }

 it('It should get the issue JSON response', function(done) {
   nock('https://username.atlassian.net')
   .get('/rest/api/latest/issue/AL-6')
   .reply(200, getIssueResponse);

   storyController.getStoryStatus("AL-6", function(error, issueResponse) {
   var jsonResponse = JSON.parse(issueResponse);

   expect(jsonResponse).to.be.a('object');
   done();
 })
});

下面是执行GET请求的代码:

代码语言:javascript
复制
 function getStoryStatus(storyTicketNumber, callback) {
   https.get('https://username.atlassian.net/rest/api/latest/issue/' + storyTicketNumber, function (res) {

   res.on('data', function(data) {
    callback(null, data.toString());
   });

   res.on('error', function(error) {
    callback(error);
   });
  })
 }

这一次考试通过了,我不明白为什么。看起来它实际上是在做一个真正的调用,而不是使用我的假nock请求/响应。如果我评论nock部分或更改:

代码语言:javascript
复制
 .reply(200, getIssueResponse) to .reply(404)

它不会破坏测试,也不会改变什么,我不会用我的nock变量做任何事情。有谁能用一个明确的例子来解释一下,如何在我的NodeJS http-客户机中使用Nock来模拟请求和响应?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-30 22:18:22

TLDR:我认为您的代码比它告诉您的要做的更多。

注意:当在“流模式”中放置http请求时,data事件可能(而且可能确实)会被多次触发,每个事件用于“数据块”,在因特网块上的变量可能在1400-64000字节之间,因此期望多个回调调用(这是非常特殊的类型)。

作为一个简单的建议,您可以尝试使用请求或仅仅连接接收到的数据,然后调用end事件的回调。

我使用后一种技术尝试了一个非常小的片段。

代码语言:javascript
复制
var assert = require('assert');
var https = require('https');
var nock = require('nock');

function externalService(callback) {
  // directly from node documentation:
  // https://nodejs.org/api/https.html#https_https_get_options_callback
  https.get('https://encrypted.google.com/', (res) => {

    var data = '';
    res.on('data', (d) => {
      data += d;
    });

    res.on('end', () => callback(null, JSON.parse(data), res));
  // on request departure error (network is down?)
  // just invoke callback with first argument the error
  }).on('error', callback);
}


describe('Learning nock', () => {
  it('should intercept an https call', (done) => {
    var bogusMessage = 'this is not google, RLY!';

    var intercept = nock('https://encrypted.google.com').get('/')
      .reply(200, { message: bogusMessage });

    externalService((err, googleMessage, entireRes) => {
      if (err) return done(err);

      assert.ok(intercept.isDone());
      assert.ok(googleMessage);
      assert.strictEqual(googleMessage.message, bogusMessage);
      assert.strictEqual(entireRes.statusCode, 200);

      done();
    });

  })
})

它的工作非常好,即使使用on('data')

编辑:

关于如何正确处理流的参考

我将这个示例扩展为一个成熟的mocha示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40894178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档