Tag: nock

Node.js – Nock和nedb – 在callback完成之前执行回复

我使用nock来拦截对我的API主机的调用,并在返回响应之前做一些本地数据库查找。 请参考下面的代码。 我拦截到“入口点”的调用,并希望回答从本地数据存储区获取的数据。 我相信这是一个nock模块本身的问题,我听到一些build议使用stream。 你能帮助克服这个问题吗? // … Code Block var nock = require('nock'), request = require('request'), DataStore = require('nedb'), db = new DataStore({filename: './nedb.data'}); db.loadDatabase(function(err) { if (err) throw err; }); db.insert ({'record1': { "key1" : "value1"} }); db.insert ({'record2': { "key2" : "value2"} }); db.insert ({'record3': { "key3" : "value3"} }); db.insert ({'record4': { […]

用nock和mocha嘲笑HTTPS

我试图嘲笑一个要求条纹,但我很难。 我在我的摩卡describe块的顶部做了一些简单的事情,看起来像 describe('Billing call', function(){ var api = Nock('https://api.stripe.com').log(console.log); // … down here there's a call that uses a stripe api }); 只是为了看看它是否拦截任何东西。 但事实并非如此。 我找不到任何地方的例子,我恐怕Nock不支持它,在这种情况下,我需要find一个更好的工具。 任何人都可以为我清除这个? 谢谢!

运行npmtesting时导入Nock npm模块会产生错误

如果我删除了nock导入,我将不会收到错误消息。 我已经安装了npm npm。 testing文件 import configureMockStore from 'redux-mock-store' import { promiseHandler } from '../middleware' import * as actions from './index' import nock from 'nock' import expect from 'expect' const middlewares = [promiseHandler] const mockStore = configureMockStore(middlewares) 错误信息 FAIL src/actions/index.test.js Runtime Error – TypeError: debug is not a function at overrideClientRequest (node_modules/nock/lib/intercept.js:221:3) at activate (node_modules/nock/lib/intercept.js:320:3) at […]

在Express中testing中间件时提供会话

我尝试使用supertest和nock在我的Express应用程序中testing中间件function,但遇到了问题,我设置的路由由中间件的前面部分检查,以确保传入的req对象上存在会话属性。 我需要在运行testing之前以某种方式模拟会话对象,但不确定如何处理这个问题。 首先让我分享一些代码: router.js app.use('/api', helpers.hasAccessToken, require('./routes.js')); routes.js router.route('/*') .get(routesController.fetch) helpers.js module.exports.hasAccessToken = function(req, res, next) { if(req.session.accessToken){ next(); } else { res.status(401).send('LOGIN_SESSION_ENDED'); } }; routesController.js module.exports.fetch = function(req, res) { var options = helpers.buildAPIRequestOptions(req); request(options, function(err, response, body){ res.status(response.statusCode).send(body); }); }; routesController.spec.js var app = require('./index.js'), request = require('supertest')(app), expect = require('chai').expect, nock = […]

用nock嘲笑肥皂服务

我正在研究与肥皂服务进行通信的node应用程序,使用foam模块将jsonparsing为有效的肥皂请求,并在接收到响应时再返回。 这一切工作正常时,与肥皂服务进行沟通。 我遇到的问题是为此编写unit testing(集成testing工作正常)。 我使用nock来模拟http服务并发送回复。 这个答复确实被foam分析,然后我可以对这个答复做出断言。 所以我不能传递一个JSON对象作为回复,因为foam需要一个肥皂响应。 如果我尝试这样做,我得到的错误: Error: Start tag expected, '<' not found 在JavaScriptvariables中存储XML是痛苦的,并且不起作用(即,将其包含在引号中,并且转义内部引号无效),所以我想将嘲讽的XML响应放入文件中,并将其作为回复传递。 我已经尝试读取stream中的文件 return fs.createReadStream('response.xml') …并用文件回复 .replyWithFile(201, __dirname + 'response.xml'); 两者都失败,错误 TypeError: Cannot read property 'ObjectReference' of undefined 这是文件中的XML <env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'> <env:Header></env:Header> <env:Body> <FLNewIndividualID xmlns='http://www.lagan.com/wsdl/FLTypes'> <ObjectType>1</ObjectType> <ObjectReference>12345678</ObjectReference> <ObjectReference xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/> <ObjectReference xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/> </FLNewIndividualID> </env:Body> </env:Envelope> 正在testing的模块是 var foam = require('./foam-promise.js'); […]

模拟一个HEAD请求redirect(通过nock模块)

在我的项目中,我尝试扩展推文以充分显示。 由bit.ly缩短的链接被这个和平的代码扩展(find@ stackoverflow)。 function expandUrl(shortUrl,callback) { debug("expandUrl"); request( { method: "HEAD", url: shortUrl, followAllRedirects: true }, function (error, response) { if (error) return callback(null,shortUrl); return callback(null,response.request.href); } ); } 在摩卡testing期间不需要在线,我想用下面的代码来禁止这部分代码: nock('http://bit.ly') .intercept("/1Ghc7dI","HEAD") .reply(200,undefined,{location:"http://discoverspatial.com/courses/qgis-for-beginners"}); 但是这不起作用。 这个工作后,response.request.href是“undefined”。 (我试过href而不是位置,这没有什么区别。

重置nock范围的pendingMocks

我试图对nock范围运行多个断言,并嘲笑beforeEach中的请求,但是我看到scope.pendingMocks()与请求,即使我已经清理scope.pendingMocks()所有: afterEach(() => { nock.cleanAll(); }); 在多个请求的同一个主机上设置mock的最好方法是什么,并且在每个testing中新鲜地断言请求?

如何将请求中的模拟数据返回到Node中的Twitter Streaming API

我有一个连接到Twitter REST和stream媒体API的Node应用程序。 为了testing向REST API发出请求的代码,我可以使用Nock拦截HTTP请求并返回模拟数据,如下所示: var nock = require('nock') var mockData = […] nock('https://api.twitter.com/1.1') .get('/search/tweets.json') .reply(200, mockData) 我的应用程序还连接到stream媒体API端点状态/filter,并对通过stream式API接收的推文执行一些分析。 在Nock的README中,它声明你可以传递一个函数给.query()返回一个stream,但是我一直无法得到这个工作。 如何使用Nock(或其他库,如果有必要)从请求返回模拟数据到这个端点? 理想情况下,我希望能够在我的testing中随时发送推文到stream中,例如: it('should correctly process tweets coming in on the streaming API', () => { var mockTweet = {…} sendMockTweetToStream(mockTweet) …verify that the mock tweet was received and processed })

用nock模拟一个开放的networking套接字

我正在尝试使用nock对Oanda交易API编写的代码进行反向testing。 为此,我需要模拟stream媒体价格API(请参阅http://developer.oanda.com/rest-practice/streaming/上的stream媒体stream量)。 但是,看起来nock只能让你回复一个单一的回复,即使回应是一个stream。 有没有办法将数以千计的价格事件stream作为个人对单个请求的回应? var scopeStream = nock('https://stream-fxpractice.oanda.com') .persist() .filteringPath(function(path) { return '/stream'; }) .get('/stream') .reply(function(uri, requestBody) { return [200, {"tick":{"instrument":"AUD_CAD","time":"2014-01-30T20:47:08.066398Z","bid":0.98114,"ask":0.98139}}] })

Nock +多部分表单数据=请求不匹配

使用Nocktesting我的节点应用程序时遇到问题。 我通过nock.recorder.reclogging所有的请求,但其中有多重请求。 我使用表单数据 。 当我使用函数form.append时候,这个模块把边界放在请求体中。 问题是,边界总是不同的,当我运行与logging数据testing诺克无法find匹配的请求(因为在请求身体boyundary不是什么时候录音)。 可以做什么? 对不起,我的英语不好。