- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 node.js 中,我无法让 superagent 和 nock 一起工作。如果我使用请求而不是 super 代理,它会完美运行。
这是一个简单的例子,superagent 无法报告模拟数据:
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 对象没有“文本”属性。出了点问题。
现在如果我使用请求做同样的事情:
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,所以我宁愿坚持下去。有谁知道如何让它工作?
非常感谢,泽维尔
最佳答案
我的假设是 Nock 使用 application/json
作为 mime 类型进行响应,因为您使用 {yes: 'it works'}
进行响应。查看 Superagent 中的 res.body
。如果这不起作用,请告诉我,我会仔细查看。
编辑:
试试这个:
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 服务无论如何都应该这样做,superagent 知道如何处理 application/json
以及为什么你可以使用 res.text
或res.body
(解析的 JSON)。
关于javascript - superagent 和 nock 如何协同工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14689252/
这是在后端和前端工作正常的 Express Route 代码。 //编辑/更新容器类型 by_id - 工作 router.put("/:id", (req, res, next) => { Ve
当发出请求但未被模拟时,我得到一个错误:Error: Nock: No match for request 但这并没有使测试失败。有没有办法确保没有发出模拟请求? 最佳答案 您可以从 nock noc
如何查看nock是否与请求匹配?有没有一种方法可以在控制台上记录有关 nock 是否与正在发出的请求匹配的信息? 最佳答案 log功能在较新版本的 nock 上不再可用,例如v13.0.0 但你可以设
我正在编写与 PayPal 经典 API 交互的代码。此交互的第一部分是向 PayPal 发送请求并从他们那里获取 token 。为此,我使用简单的 https 请求: function makePa
我一直在编写一些单元测试,我注意到我似乎找不到测试异步函数的好方法。所以我找到了诺克。看起来很酷,只要它有效。我显然错过了一些东西...... import nock from 'nock'; imp
使用 nock 模拟请求链的简单示例。 const request = require('request-promise'); module.exports = () => {
我不确定 nock 是否支持在调用拦截器时使用回调/事件发射器等待或通知。 例如: const scope = nock(config.research.url)
我不确定 nock 是否支持在调用拦截器时使用回调/事件发射器等待或通知。 例如: const scope = nock(config.research.url)
我正在使用 Nock 拦截 http 请求。 测试.js: const nock = require('nock'); const server = require('../server'); con
这是我的一个拦截函数现在的样子: interceptWithError() { nock(baseUrl) .get(/.*/) .replyWithError(500); n
我正在使用 nock 库来 stub 我的 http 调用。不同的测试文件 require('nock') 并进行 stub 。如果每个测试单独运行,则所有测试都通过。但是,如果所有测试一起运行,后面
我正在尝试使用业力服务器和诺克创建一些基本测试。似乎 nock 根本没有拦截我的请求,有人知道吗?我无法弄清楚缺少什么。我仍然得到真实的数据。 nock('https://api.github.com
我正在尝试使用 SuperTest 自动化一些 REST 服务。该服务是一个 POST 调用,它在内部调用另一个第三方服务的 GET 方法。我正在尝试模拟第三方服务以提高测试效率并减少测试执行时间。我
我按照 redux.org 的基本示例来测试异步操作 action.js 我的代码是这样的: import axios from 'axios' export function getGoodData
我有一个 Node.js 服务,它在向用户返回响应之前调用外部 API 来收集一些数据。我想模拟这个外部 API,以便测试我的服务,但不幸的是,我得到的响应没有内容(只有状态 200)。这是我的测试:
我正在尝试运行下面的一段代码: const nock = require('nock'); const request = require('request-promise'); nock('http
我有验证 response.connection.remoteAddress 的应用程序代码不是本地 IP,但在使用 nock 时 response.connection只是一个没有 remoteAd
我目前正在使用 node-fetch 和 nock 作为位于 Angular 项目之上的快速服务器。 我有以下正在调用 api 的中间件: export const middleware = asyn
我正在尝试模拟包含问号但不属于查询字符串的路径,例如: https://example.com/index.php?/api/v2/get-item/1 Nock 正在对问号处的路径进行切片,并期望我
我正在尝试使用 nock 对针对 Oanda 的交易 API 编写的代码运行回测。为此,我需要模拟流式价格 API(请参阅 http://developer.oanda.com/rest-practi
我是一名优秀的程序员,十分优秀!