如何与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!') rp({ url: 'http://www.google.com', timeout: 5000 }) .then((data) => { console.log('then block should not execute: ', data) }) .catch((err) => { expect(err.cause.code).to.equal('ETIMEDOUT') return done() }) }) })
最后我解决了它:
nock('http://www.google.com') .get('/') .replyWithError({code: 'ETIMEDOUT'})
看起来干净,它不涉及delay
和东西,而且在我看来,它足以模拟ETIMEDOUT
有记住即时通讯使用request-promise
lib在哪里检查ETIMEDOUT
如: if (err.error && err.error.code === 'ETIMEDOUT')
这对我有用。 只有我不使用npm rp而是npm请求。