summaryrefslogtreecommitdiffstats
path: root/test/diagnostics-channel/error.js
blob: 1f350b1dd4949bed5105904e09f26eedf444c410 (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
'use strict'

const t = require('tap')

let diagnosticsChannel

try {
  diagnosticsChannel = require('diagnostics_channel')
} catch {
  t.skip('missing diagnostics_channel')
  process.exit(0)
}

const { Client } = require('../..')
const { createServer } = require('http')

t.plan(3)

const server = createServer((req, res) => {
  res.destroy()
})
t.teardown(server.close.bind(server))

const reqHeaders = {
  foo: undefined,
  bar: 'bar'
}

let _req
diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
  _req = request
})

diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => {
  t.equal(_req, request)
  t.equal(error.code, 'UND_ERR_SOCKET')
})

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

  client.request({
    path: '/',
    method: 'GET',
    headers: reqHeaders
  }, (err, data) => {
    t.equal(err.code, 'UND_ERR_SOCKET')
  })
})