summaryrefslogtreecommitdiffstats
path: root/test/http-req-destroy.js
blob: 29ec98e1a9c8a7f03d6a54837ac0d5cfdc1b22d3 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'

const { test } = require('tap')
const undici = require('..')
const { createServer } = require('http')
const { Readable } = require('stream')
const { maybeWrapStream, consts } = require('./utils/async-iterators')

function doNotKillReqSocket (bodyType) {
  test(`do not kill req socket ${bodyType}`, (t) => {
    t.plan(3)

    const server1 = createServer((req, res) => {
      const client = new undici.Client(`http://localhost:${server2.address().port}`)
      t.teardown(client.close.bind(client))
      client.request({
        path: '/',
        method: 'POST',
        body: req
      }, (err, response) => {
        t.error(err)
        setTimeout(() => {
          response.body.on('data', buf => {
            res.write(buf)
            setTimeout(() => {
              res.end()
            }, 100)
          })
        }, 100)
      })
    })
    t.teardown(server1.close.bind(server1))

    const server2 = createServer((req, res) => {
      setTimeout(() => {
        req.pipe(res)
      }, 100)
    })
    t.teardown(server2.close.bind(server2))

    server1.listen(0, () => {
      const client = new undici.Client(`http://localhost:${server1.address().port}`)
      t.teardown(client.close.bind(client))

      const r = new Readable({ read () {} })
      r.push('hello')
      client.request({
        path: '/',
        method: 'POST',
        body: maybeWrapStream(r, bodyType)
      }, (err, response) => {
        t.error(err)
        const bufs = []
        response.body.on('data', (buf) => {
          bufs.push(buf)
          r.push(null)
        })
        response.body.on('end', () => {
          t.equal('hello', Buffer.concat(bufs).toString('utf8'))
        })
      })
    })

    server2.listen(0)
  })
}

doNotKillReqSocket(consts.STREAM)
doNotKillReqSocket(consts.ASYNC_ITERATOR)