-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathinflight-and-close.js
37 lines (34 loc) · 1.05 KB
/
inflight-and-close.js
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
'use strict'
const { tspl } = require('@matteo.collina/tspl')
const { test } = require('node:test')
const { request } = require('..')
const http = require('node:http')
test('inflight and close', async (t) => {
t = tspl(t, { plan: 3 })
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.ok(true, 'first response')
body.resume()
body.on('close', function () {
t.ok(true, 'first body closed')
})
return request(url)
.then(({ statusCode, headers, body }) => {
t.ok(true, 'second response')
body.resume()
body.on('close', function () {
server.close()
})
})
}).catch((err) => {
t.ifError(err)
})
})
await t.completed
})