summaryrefslogtreecommitdiffstats
path: root/test/issue-2065.js
blob: cc288c48a749e308fd40f919856684519786ac8f (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'

const { test, skip } = require('tap')
const { nodeMajor, nodeMinor } = require('../lib/core/util')
const { createServer } = require('http')
const { once } = require('events')
const { createReadStream } = require('fs')
const { File, FormData, request } = require('..')

if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 8)) {
  skip('FormData is not available in node < v16.8.0')
  process.exit()
}

test('undici.request with a FormData body should set content-length header', async (t) => {
  const server = createServer((req, res) => {
    t.ok(req.headers['content-length'])
    res.end()
  }).listen(0)

  t.teardown(server.close.bind(server))
  await once(server, 'listening')

  const body = new FormData()
  body.set('file', new File(['abc'], 'abc.txt'))

  await request(`http://localhost:${server.address().port}`, {
    method: 'POST',
    body
  })
})

test('undici.request with a FormData stream value should set transfer-encoding header', async (t) => {
  const server = createServer((req, res) => {
    t.equal(req.headers['transfer-encoding'], 'chunked')
    res.end()
  }).listen(0)

  t.teardown(server.close.bind(server))
  await once(server, 'listening')

  class BlobFromStream {
    #stream
    #type
    constructor (stream, type) {
      this.#stream = stream
      this.#type = type
    }

    stream () {
      return this.#stream
    }

    get type () {
      return this.#type
    }

    get [Symbol.toStringTag] () {
      return 'Blob'
    }
  }

  const body = new FormData()
  const fileReadable = createReadStream(__filename)
  body.set('file', new BlobFromStream(fileReadable, '.js'), 'streamfile')

  await request(`http://localhost:${server.address().port}`, {
    method: 'POST',
    body
  })
})