|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const util = require('node:util') |
4 | 3 | const test = require('tap').test
|
5 | 4 | const FormData = require('form-data')
|
6 | 5 | const Fastify = require('fastify')
|
7 | 6 | const multipart = require('..')
|
8 | 7 | const http = require('node:http')
|
9 |
| -const sleep = util.promisify(setTimeout) |
| 8 | +const { setTimeout: sleep } = require('node:timers/promises') |
10 | 9 | const { writableNoopStream } = require('noop-stream')
|
11 |
| -const stream = require('node:stream') |
12 |
| -const pipeline = util.promisify(stream.pipeline) |
| 10 | +const { pipeline } = require('node:stream/promises') |
| 11 | +const { once } = require('node:events') |
| 12 | +const fs = require('node:fs/promises') |
13 | 13 |
|
14 |
| -test('should finish with error on partial upload', async function (t) { |
| 14 | +test('should finish with error on partial upload - files api', async function (t) { |
15 | 15 | t.plan(4)
|
16 | 16 |
|
17 | 17 | const fastify = Fastify()
|
@@ -60,3 +60,50 @@ test('should finish with error on partial upload', async function (t) {
|
60 | 60 | await sleep(100)
|
61 | 61 | t.end()
|
62 | 62 | })
|
| 63 | + |
| 64 | +test('should finish with error on partial upload - saveRequestFiles', async function (t) { |
| 65 | + t.plan(3) |
| 66 | + |
| 67 | + const fastify = Fastify() |
| 68 | + t.teardown(fastify.close.bind(fastify)) |
| 69 | + |
| 70 | + await fastify.register(multipart) |
| 71 | + |
| 72 | + let tmpUploads |
| 73 | + fastify.post('/', async function (req) { |
| 74 | + t.ok(req.isMultipart()) |
| 75 | + try { |
| 76 | + await req.saveRequestFiles() |
| 77 | + } finally { |
| 78 | + tmpUploads = req.tmpUploads |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + await fastify.listen({ port: 0 }) |
| 83 | + const dataSize = 1024 * 1024 * 1024 |
| 84 | + |
| 85 | + // request |
| 86 | + const form = new FormData() |
| 87 | + form.append('upload', Buffer.alloc(dataSize)) |
| 88 | + const opts = { |
| 89 | + protocol: 'http:', |
| 90 | + hostname: 'localhost', |
| 91 | + port: fastify.server.address().port, |
| 92 | + path: '/', |
| 93 | + headers: form.getHeaders(), |
| 94 | + method: 'POST' |
| 95 | + } |
| 96 | + |
| 97 | + const req = http.request(opts) |
| 98 | + const data = form.getBuffer() |
| 99 | + req.write(data.slice(0, dataSize / 4)) |
| 100 | + req.write(data.slice(dataSize / 4, dataSize / 2)) |
| 101 | + req.end() |
| 102 | + |
| 103 | + const [res] = await once(req, 'response') |
| 104 | + t.equal(res.statusCode, 500) |
| 105 | + |
| 106 | + for (const tmpUpload of tmpUploads) { |
| 107 | + await t.rejects(fs.access(tmpUpload)) |
| 108 | + } |
| 109 | +}) |
0 commit comments