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