|
| 1 | +import {strictEqual} from 'node:assert'; |
| 2 | +import { createServer } from 'node:http'; |
| 3 | +import { connect } from 'node:net'; |
| 4 | +import formidable from '../../src/index.js'; |
| 5 | + |
| 6 | +const PORT = 13539; |
| 7 | + |
| 8 | +test('end event emitted twice', (done) => { |
| 9 | + const server = createServer((req) => { |
| 10 | + const form = formidable(); |
| 11 | + |
| 12 | + let i = 0; |
| 13 | + form.on('end', () => { |
| 14 | + i += 1; |
| 15 | + strictEqual(i, 1, 'end should be emitted once'); |
| 16 | + }); |
| 17 | + form.parse(req, () => { |
| 18 | + |
| 19 | + server.close(); |
| 20 | + strictEqual(i, 1, 'end should be emitted once'); |
| 21 | + done(); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + server.listen(PORT, 'localhost', () => { |
| 26 | + const choosenPort = server.address().port; |
| 27 | + |
| 28 | + const client = connect(choosenPort); |
| 29 | + |
| 30 | + client.write( |
| 31 | +`POST /api/upload HTTP/1.1 |
| 32 | +Host: localhost:${choosenPort} |
| 33 | +User-Agent: N |
| 34 | +Content-Type: multipart/form-data; boundary=---------------------------13068458571765726332503797717 |
| 35 | +
|
| 36 | +
|
| 37 | +-----------------------------13068458571765726332503797717 |
| 38 | +Content-Disposition: form-data; name="title" |
| 39 | +
|
| 40 | +a |
| 41 | +-----------------------------13068458571765726332503797717 |
| 42 | +Content-Disposition: form-data; name="multipleFiles"; filename="x.txt" |
| 43 | +Content-Type: application/x-javascript |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | +a |
| 48 | +b |
| 49 | +c |
| 50 | +d |
| 51 | +
|
| 52 | +-----------------------------13068458571765726332503797717-- |
| 53 | +`, |
| 54 | + ); |
| 55 | + client.end(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments