Tag: nock

使用nock模仿多个沙发数据库请求

在我原来的function,我需要在同一个沙发login2个不同的数据库的请求。 var cloudant = require('cloudant')(https://cloudant_url); var userdb = cloudant.db.use('user'); var addrdb = cloudant.db.use('address'); function onChange(username) { userdb.get(username, function(err,resp) { var user_id = resp.id; addrdb.get(user_id,function(err1,resp1){ var addr = resp1.address; }); }); }; var nockVar = function() { nock(testCloudantDBURL) .get('/user/jack') .reply(200,{'id' : 123}); nock(testCloudantDBURL) .get('/address/123') .reply(200,{'address':'123}); }; describe('Test Cloudant Listener code' , function() { nockVar(); it('test get […]

如何与nock和请求库ETIMEDOUT

如何用nock ( https://github.com/node-nock/nock )强制ETIMEDOUT并request ? 我试图跟随,其中nock将延迟响应8000毫秒,而超时请求将被设置为5000毫秒,所以我期望看到ETIMEDOUT但事实并非如此。 代码将返回, then block should not execute: Hello from Google! 摩卡testing/ force-req-timeout.test.js –timeout = 10000 'use strict' const expect = require('chai').expect const nock = require('nock') const rp = require('request-promise') describe('force request timeout with nock', () => { it('should return ETIMEDOUT', (done) => { nock('http://www.google.com') .get('/') .delay(8000) .reply(200, 'Hello from Google!') […]

如何解码nocklogging的响应

如何特别解码/解压诺克logging器产生的输出,所以我们可以看到作为文本的响应? 我想我们不明白,如果回应是gzipped和/或编码 当我们把它加载到nock中时,这个对象就能find,我们的testing正如我们所期望的那样运行。 要查看API生成的内容,我们必须将日志logging置于实现文件中。 我们正在录制和保存JSON的回复: nock.recorder.rec({output_objects: true, dont_print: true}); JSON.stringify(nock.recorder.play()) 我们的文件如下所示: [ { "scope": "https://some.api.com:443", "method": "POST", "path": "/auth?key=some_key", "body": { "logonId": "user@api.com", "logonPassword": "secret" }, "status": 400, "response": [ "1f8b0800000000000000458cbd6ac34010067b3fc5c735691263bb741344ec42f827420a492916692d1d9cb461f71c218cdf3d97266e6786b92d00c7aaa205290d1c59cd6d71bb3fff8b376939a1cd6abd7ac003cf89b97a5f96757efecc8ef9aede9fb2fc586455f5f55eeedca33db119757f0f5704266334a2ca4d44ec19170941263f76f06657b62dd6cb2af919ec9357cc7255f0cb403e4014df643689b6687d3b3e450c149b1e534f1113a3a71f868cb8f8c04b7ca48b8fa08efcf8ea16f75fa1776d91ee000000" ], "headers": { "cache-control": "no-store, no-cache, must-revalidate", "content-encoding": "gzip", "content-type": "application/json", "transfer-encoding": "chunked", "connection": "Close" } } ]

诺克不拦截我的请求

我正在尝试使用karma服务器和nock来创build一些基本的testing。 看来nock并没有拦截我的请求,有没有人有想法? 我无法弄清楚什么是缺less的。 我仍然获得真实的数据。 nock('https://api.github.com/users/' + username).log(console.log) .get('/') .query(true) .reply(400, { statusMessage: 'Bad Request', foo: 'foo' }) http.get('https://api.github.com/users/' + username, function(res) { console.log('res', res) }) 我也加了这个中间件 const middlewares = [thunk]; const mockStore = configureStore(middlewares); ======更新6月6日====== 整个stream程使用react-redux这是我的testing: import configureStore from 'redux-mock-store'; import thunk from 'redux-thunk'; import axios from 'axios'; import expect from 'expect'; import * as […]

Node.js Nock模拟请求超时和随后的成功

我试图模拟服务请求超时testing节点requestretry模块,它允许您指定请求最大尝试重试次数和重试延迟。 为了testing这个,我需要使用nock来模拟第一个X个请求的超时,然后成功地响应相同的请求。 我知道有'socketDelay()'方法来延迟连接,但是如何在第一个延迟响应之后指定一个成功的响应呢? 我有这个,它模拟了第一个请求的超时 //delays the first request's response by 1500 nock(urlHost) .post('/' + uriPath) .socketDelay(1500) .reply(200, 'response body'); 但是如何才能更快地响应模拟服务恢复呢? 我正在寻找这方面的工作 //delays the first two request's responses by 1500 nock(urlHost) .post('/' + requestIdentifier.ttoRoutingInfo.uriPath) .socketDelay(1500) .reply(200, 'response body') .times(2) //delays the third request by only 300 .then .socketDelay(300) .reply(200, 'response body');

如何返回对象而不是string与诺克反应?

当我使用nock存根请求时,即使使用'Content-Type': 'application/json'也会返回String结果而不是Object 'Content-Type': 'application/json' : var response = { success: true, statusCode: 200, body: { "status": "OK", "id": "05056b27b82", } }; Test.BuildRequest(); Test.SendRequest(done); nock('https://someapi.com') // also tried // .defaultReplyHeaders({ // 'Content-Type': 'application/json', // 'Accept': 'application/json' // }) .post('/order') .reply(200, response.body, 'Content-Type': 'application/json', 'Accept': 'application/json'); 检查: console.log(put.response.body); console.log(put.response.body.id); 输出: {"status":"OK","id":"05056b27b82"} undefined 在代码中,我使用request模块返回Object与相同的数据。 我也试过sinon (不适用于我)和fakeweb但得到了同样的问题。 我的代码 ,我试图testing: […]

在Reduxtesting中,Nock不拦截API调用

我试图在一个REDX应用程序中testing一个API调用。 该代码非常符合redux文档的Async Action Creators部分中概述的模式: http://redux.js.org/docs/recipes/WritingTests.html 它的要点是,你使用减less模拟商店logging和断言任何触发的行动。 这是整个testing,用nock来模拟api调用: import React from 'React' import ReactDOM from 'react-dom' import expect from 'expect'; import expectJSX from 'expect-jsx'; import TestUtils from 'react-addons-test-utils' import configureMockStore from 'redux-mock-store' import thunk from 'redux-thunk' import nock from 'nock' expect.extend(expectJSX); import * as types from '../../constants/Actions' describe('Async Search Actions', () => { const thunkMiddleware = […]

如何用Nock请求testing错误?

我想在请求返回中testing错误。 我在testing中使用诺克,我怎么能强迫诺克挑起一个错误? 我想要达到100%的testing覆盖率,并且需要为此testingerr分支 request('/foo', function(err, res) { if(err) console.log('boom!'); }); 切勿进入if er分支。 即使命中错误是一个有效的回应,我的诺克线testing看起来像这样 nock('http://localhost:3000').get('/foo').reply(400); 编辑:多谢一些意见: 我试图嘲笑请求中的错误。 从节点手册: https : //nodejs.org/api/http.html#http_http_request_options_callback 如果在请求过程中遇到任何错误(包括DNSparsing,TCP级错误或实际的HTTPparsing错误),则“错误”事件是在返回的请求对象上发射 错误代码(例如4xx)不会定义errvariables。 我试图嘲笑这一点,无论错误,定义errvariables和评估为真

与Sinon一起testingaxios调用,还有redux和Karma

你好在testing的redux文档中,他们有这个例子来testingAPI调用: import configureMockStore from 'redux-mock-store' import thunk from 'redux-thunk' import * as actions from '../../actions/counter' import * as types from '../../constants/ActionTypes' import nock from 'nock' const middlewares = [ thunk ] const mockStore = configureMockStore(middlewares) describe('async actions', () => { afterEach(() => { nock.cleanAll() }) it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', (done) => […]

superagent和nock怎么能一起工作?

在node.js中,我很难使superagent和nock一起工作。 如果我使用请求,而不是superagent,它完美的作品。 这里是一个简单的例子,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对象没有“text”属性。 出了些问题。 现在,如果我使用请求做同样的事情: 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) […]