summaryrefslogtreecommitdiffstats
path: root/test/fuzzing/client/client-fuzz-options.js
blob: 5be81b630c65de8c62576aa653abc84dd7999f40 (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
'use strict'

const { request, errors } = require('../../..')

const acceptableCodes = [
  'ERR_INVALID_URL',
  // These are included because '\\ABC' is interpreted as a Windows UNC path and can cause these errors.
  'ENOTFOUND',
  'EAI_AGAIN',
  'ECONNREFUSED'
  // ----
]

async function fuzz (netServer, results, buf) {
  const optionKeys = ['body', 'path', 'method', 'opaque', 'upgrade', buf]
  const options = {}
  for (const optionKey of optionKeys) {
    if (Math.random() < 0.5) {
      options[optionKey] = buf.toString()
    }
  }
  results.options = options
  try {
    const data = await request(`http://localhost:${netServer.address().port}`, options)
    data.body.destroy().on('error', () => {})
  } catch (err) {
    results.err = err
    // Handle any undici errors
    if (Object.values(errors).some(undiciError => err instanceof undiciError)) {
      // Okay error
    } else if (!acceptableCodes.includes(err.code)) {
      console.log(`=== Options: ${JSON.stringify(options)} ===`)
      throw err
    }
  }
}

module.exports = fuzz