|
| 1 | +'use strict' |
| 2 | +var requireInject = require('require-inject') |
| 3 | +var test = require('tap').test |
| 4 | +var EventEmitter = require('events').EventEmitter |
| 5 | +var PassThrough = require('readable-stream').PassThrough |
| 6 | + |
| 7 | +var content = [ |
| 8 | + 'first chunk', |
| 9 | + 'second chunk' |
| 10 | +] |
| 11 | +var requests = 0 |
| 12 | +var fetch = requireInject('../lib/fetch.js', { |
| 13 | + request: function (opts) { |
| 14 | + var req = new EventEmitter() |
| 15 | + ++requests |
| 16 | + setTimeout(function () { |
| 17 | + var res = new PassThrough() |
| 18 | + res.statusCode = 200 |
| 19 | + |
| 20 | + setTimeout(function () { |
| 21 | + res.write(content[0]) |
| 22 | + }, 50) |
| 23 | + |
| 24 | + if (requests === 1) { |
| 25 | + setTimeout(function () { |
| 26 | + var err = new Error('read ECONNRESET') |
| 27 | + err.code = 'ECONNRESET' |
| 28 | + req.emit('error', err) |
| 29 | + }, 100) |
| 30 | + } else { |
| 31 | + // we allow success on retries, though in practice we won't be |
| 32 | + // retrying. |
| 33 | + setTimeout(function () { |
| 34 | + res.end(content[1]) |
| 35 | + }, 100) |
| 36 | + } |
| 37 | + req.emit('response', res) |
| 38 | + }, 50) |
| 39 | + return req |
| 40 | + } |
| 41 | +}) |
| 42 | + |
| 43 | +function clientMock (t) { |
| 44 | + return { |
| 45 | + log: { |
| 46 | + info: function (section, message) { |
| 47 | + t.comment('[info] ' + section + ': ' + [].slice.call(arguments, 1).join(' ')) |
| 48 | + }, |
| 49 | + http: function (section, message) { |
| 50 | + t.comment('[http] ' + section + ': ' + [].slice.call(arguments, 1).join(' ')) |
| 51 | + } |
| 52 | + }, |
| 53 | + authify: function (alwaysAuth, parsed, headers, auth) { |
| 54 | + return |
| 55 | + }, |
| 56 | + initialize: function (parsed, method, accept, headers) { |
| 57 | + return {} |
| 58 | + }, |
| 59 | + attempt: require('../lib/attempt.js'), |
| 60 | + config: { |
| 61 | + retry: { |
| 62 | + retries: 2, |
| 63 | + factor: 10, |
| 64 | + minTimeout: 10000, |
| 65 | + maxTimeout: 60000 |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/* |
| 72 | +This tests that errors that occur in the REQUEST object AFTER a `response` |
| 73 | +event has been emitted will be passed through to the `response` stream. |
| 74 | +This means that we won't try to retry these sorts of errors ourselves. |
| 75 | +*/ |
| 76 | + |
| 77 | +test('econnreset', function (t) { |
| 78 | + var client = clientMock(t) |
| 79 | + fetch.call(client, 'http://example.com/', {}, function (err, res) { |
| 80 | + t.ifError(err, 'initial fetch ok') |
| 81 | + var data = '' |
| 82 | + res.on('data', function (chunk) { |
| 83 | + data += chunk |
| 84 | + }) |
| 85 | + res.on('error', function (err) { |
| 86 | + t.comment('ERROR:', err.stack) |
| 87 | + t.pass("errored and that's ok") |
| 88 | + t.done() |
| 89 | + }) |
| 90 | + res.on('end', function () { |
| 91 | + t.is(data, content.join(''), 'succeeded and got the right data') |
| 92 | + t.done() |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments