で http(s) リクエストを初期化するとrequest(url, callback)、イベント エミッタ インスタンスが返されます (いくつかのカスタム プロパティ/メソッドとともに)。
このオブジェクトを手に入れることができる限り (これには何らかのリファクタリングが必要になるか、またはおそらく適切ではないかもしれません)、このエミッターにerrorイベントを発行させて、発行しerrたエラーでコールバックを起動することができます。
次のコード スニペットは、これを示しています。
'use strict';
// Just importing the module
var request = require('request')
// google is now an event emitter that we can emit from!
, google = request('http://google.com', function (err, res) {
console.log(err) // Guess what this will be...?
})
// In the next tick, make the emitter emit an error event
// which will trigger the above callback with err being
// our Error object.
process.nextTick(function () {
google.emit('error', new Error('test'))
})
編集
このアプローチの問題点は、ほとんどの場合、多少のリファクタリングが必要になることです。別のアプローチでは、Node のネイティブ モジュールがキャッシュされ、アプリケーション全体で再利用されるという事実を利用します。したがって、httpモジュールを変更すると、Requestは変更内容を確認できます。秘訣は、メソッドにモンキー パッチを適用し、http.request()独自のロジックを挿入することです。
次のコード スニペットは、これを示しています。
'use strict';
// Just importing the module
var request = require('request')
, http = require('http')
, httpRequest = http.request
// Monkey-patch the http.request method with
// our implementation
http.request = function (opts, cb) {
console.log('ping');
// Call the original implementation of http.request()
var req = httpRequest(opts, cb)
// In next tick, simulate an error in the http module
process.nextTick(function () {
req.emit('error', new Error('you shall not pass!'))
// Prevent Request from waiting for
// this request to finish
req.removeAllListeners('response')
// Properly close the current request
req.end()
})
// We must return this value to keep it
// consistent with original implementation
return req
}
request('http://google.com', function (err) {
console.log(err) // Guess what this will be...?
})
Nockも似たようなこと ( httpモジュールのメソッドの置き換え) を行っているのではないかと思うので、Nockを必要とした (そしておそらく構成した?)後に、このモンキー パッチを適用することをお勧めします。
正しい URL が要求された場合 (オブジェクトを調べたopts場合) にのみエラーを発行することを確認し、元の実装を復元しhttp.request()て将来のテストが変更の影響を受けないようにすることは、ユーザーのタスクになることに注意してください。