summaryrefslogtreecommitdiffstats
path: root/test/inflight-and-close.js
blob: 49fbb10f7fc46e59e9b791873f83195980f60222 (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
'use strict'

const t = require('tap')
const { request } = require('..')
const http = require('http')

const server = http.createServer((req, res) => {
  res.writeHead(200)
  res.end('Response body')
  res.socket.end() // Close the connection immediately with every response
}).listen(0, '127.0.0.1', function () {
  const url = `http://127.0.0.1:${this.address().port}`
  request(url)
    .then(({ statusCode, headers, body }) => {
      t.pass('first response')
      body.resume()
      body.on('close', function () {
        t.pass('first body closed')
      })
      return request(url)
        .then(({ statusCode, headers, body }) => {
          t.pass('second response')
          body.resume()
          body.on('close', function () {
            server.close()
          })
        })
    }).catch((err) => {
      t.error(err)
    })
})