summaryrefslogtreecommitdiffstats
path: root/test/issue-2349.js
blob: a82bb74a261cbb88f8f9fa1bf32562593272eec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict'

const { test, skip } = require('tap')
const { nodeMajor } = require('../lib/core/util')
const { Writable } = require('stream')
const { MockAgent, errors, stream } = require('..')

if (nodeMajor < 16) {
  skip('only for node 16')
  process.exit(0)
}

test('stream() does not fail after request has been aborted', async (t) => {
  t.plan(1)

  const mockAgent = new MockAgent()

  mockAgent.disableNetConnect()
  mockAgent
    .get('http://localhost:3333')
    .intercept({
      path: '/'
    })
    .reply(200, 'ok')
    .delay(10)

  const parts = []
  const ac = new AbortController()

  setTimeout(() => ac.abort('nevermind'), 5)

  try {
    await stream(
      'http://localhost:3333/',
      {
        opaque: { parts },
        signal: ac.signal,
        dispatcher: mockAgent
      },
      ({ opaque: { parts } }) => {
        return new Writable({
          write (chunk, _encoding, callback) {
            parts.push(chunk)
            callback()
          }
        })
      }
    )
  } catch (error) {
    console.log(error)
    t.equal(error instanceof errors.RequestAbortedError, true)
  }
})