本文介绍了superagent和nock如何协同工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 node.js 中,我无法让 superagent 和 nock 一起工作.如果我使用 request 而不是 superagent,它可以完美运行.

In node.js, I have trouble making superagent and nock work together. If I use request instead of superagent, it works perfectly.

这是一个简单的例子,其中 superagent 无法报告模拟数据:

Here is a simple example where superagent fails to report the mocked data:

var agent = require('superagent');
var nock = require('nock');

nock('http://thefabric.com')
  .get('/testapi.html')
  .reply(200, {yes: 'it works !'});

agent
  .get('http://thefabric.com/testapi.html')
  .end(function(res){
    console.log(res.text);
  });

res 对象没有文本"属性.出了点问题.

the res object has no 'text' property. Something went wrong.

现在,如果我使用 request 做同样的事情:

Now if I do the same thing using request:

var request = require('request');
var nock = require('nock');

nock('http://thefabric.com')
  .get('/testapi.html')
  .reply(200, {yes: 'it works !'});

request('http://thefabric.com/testapi.html', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body)
  }
})

模拟内容显示正确.

我们在测试中使用了 superagent,所以我宁愿坚持使用它.有谁知道如何使它工作?

We used superagent in the tests so I'd rather stick with it. Does anyone know how to make it work ?

非常感谢,泽维尔

推荐答案

我的假设是 Nock 使用 application/json 作为 mime 类型响应,因为您使用 {yes:'它有效'}.查看 Superagent 中的 res.body.如果这不起作用,请告诉我,我会仔细查看.

My presumption is that Nock is responding with application/json as the mime type since you're responding with {yes: 'it works'}. Look at res.body in Superagent. If this doesn't work, let me know and I'll take a closer look.

试试这个:

var agent = require('superagent');
var nock = require('nock');

nock('http://localhost')
.get('/testapi.html')
.reply(200, {yes: 'it works !'}, {'Content-Type': 'application/json'}); //<-- notice the mime type?

agent
.get('http://localhost/testapi.html')
.end(function(res){
  console.log(res.text) //can use res.body if you wish
});

或...

var agent = require('superagent');
var nock = require('nock');

nock('http://localhost')
.get('/testapi.html')
.reply(200, {yes: 'it works !'});

agent
.get('http://localhost/testapi.html')
.buffer() //<--- notice the buffering call?
.end(function(res){
  console.log(res.text)
});

现在任何一个都有效.这就是我相信正在发生的事情.nock 没有设置 mime 类型,默认是默认的.我假设默认是 application/octet-stream.如果是这种情况,则 superagent 将不会缓冲响应以节省内存.您必须强制它缓冲它.这就是为什么如果你指定了一个 MIME 类型,你的 HTTP 服务无论如何都应该这样做,超级代理知道如何处理 application/json 以及为什么如果你可以使用 res.textres.body(解析的 JSON).

Either one works now. Here's what I believe is going on. nock is not setting a mime type, and the default is assumed. I assume the default is application/octet-stream. If that's the case, superagent then does not buffer the response to conserve memory. You must force it to buffer it. That's why if you specify a mime type, which your HTTP service should anyways, superagent knows what to do with application/json and why if you can use either res.text or res.body (parsed JSON).

这篇关于superagent和nock如何协同工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 08:55