Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert https://github.com/nock/nock/pull/1270 #1336

Merged
merged 8 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const _ = require('lodash')
const debug = require('debug')('nock.intercept')
const EventEmitter = require('events').EventEmitter
const globalEmitter = require('./global_emitter')
const timers = require('timers')

/**
* @name NetConnectNotAllowedError
Expand Down Expand Up @@ -279,7 +280,7 @@ function overrideClientRequest() {
if (isOff() || isEnabledForNetConnect(options)) {
originalClientRequest.apply(this, arguments)
} else {
setImmediate(
timers.setImmediate(
function() {
const error = new NetConnectNotAllowedError(
options.host,
Expand Down
11 changes: 6 additions & 5 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const debug = require('debug')('nock.request_overrider')
const ReadableStream = require('stream').Readable
const globalEmitter = require('./global_emitter')
const zlib = require('zlib')
const timers = require('timers')

function getHeader(request, name) {
if (!request._headers) {
Expand Down Expand Up @@ -41,7 +42,7 @@ function setHeader(request, name, value) {
}

if (name == 'expect' && value == '100-continue') {
setImmediate(function() {
timers.setImmediate(function() {
debug('continue')
request.emit('continue')
})
Expand Down Expand Up @@ -142,7 +143,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
emitError(new Error('Request aborted'))
}

setImmediate(function() {
timers.setImmediate(function() {
req.emit('drain')
})

Expand Down Expand Up @@ -301,7 +302,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
} else {
error = new Error(interceptor.errorMessage)
}
setTimeout(emitError, interceptor.getTotalDelay(), error)
timers.setTimeout(emitError, interceptor.getTotalDelay(), error)
return
}
response.statusCode = Number(interceptor.statusCode) || 200
Expand Down Expand Up @@ -579,13 +580,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}

// Stream the response chunks one at a time.
setImmediate(function emitChunk() {
timers.setImmediate(function emitChunk() {
const chunk = responseBuffers.shift()

if (chunk) {
debug('emitting response chunk')
response.push(chunk)
setImmediate(emitChunk)
timers.setImmediate(emitChunk)
} else {
debug('ending response stream')
response.push(null)
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"got": "^9.4.0",
"hyperquest": "^2.1.3",
"isomorphic-fetch": "^2.2.0",
"lolex": "^3.0.0",
"markdown-toc": "^1.2.0",
"needle": "^2.2.2",
"nyc": "^12.0.1",
Expand Down
23 changes: 23 additions & 0 deletions tests/test_fake_timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const nock = require('../.')
const test = require('tap').test
const request = require('request')
const lolex = require('lolex')

// https://github.com/nock/nock/issues/1334
test('one function returns successfully when fake timer is enabled', function(t) {
const clock = lolex.install()
nock('http://www.google.com')
.get('/')
.reply(200)

request.get('http://www.google.com', function(err, resp) {
clock.uninstall()
if (err) {
throw err
}
t.equal(resp.statusCode, 200)
t.end()
})
})