summaryrefslogtreecommitdiffstats
path: root/test/request-timeout2.js
blob: 53943fbf5d3d94e087bd134c7dcbe4c8747defd4 (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
'use strict'

const { test } = require('tap')
const { Client } = require('..')
const { createServer } = require('http')
const { Readable } = require('stream')

test('request timeout with slow readable body', (t) => {
  t.plan(1)

  const server = createServer(async (req, res) => {
    let str = ''
    for await (const x of req) {
      str += x
    }
    res.end(str)
  })
  t.teardown(server.close.bind(server))

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

    const body = new Readable({
      read () {
        if (this._reading) {
          return
        }
        this._reading = true

        this.push('asd')
        setTimeout(() => {
          this.push('asd')
          this.push(null)
        }, 2e3)
      }
    })
    client.request({
      path: '/',
      method: 'POST',
      headersTimeout: 1e3,
      body
    }, async (err, response) => {
      t.error(err)
      await response.body.dump()
    })
  })
})