|
| 1 | +import {strictEqual, ok} from 'node:assert'; |
| 2 | +import { createServer, request } from 'node:http'; |
| 3 | +import formidable, {errors} from '../../src/index.js'; |
| 4 | +import test from 'node:test'; |
| 5 | + |
| 6 | +const PORT = 13539; |
| 7 | + |
| 8 | +const isPromise = (x) => { |
| 9 | + return x && typeof x === `object` && typeof x.then === `function`; |
| 10 | +}; |
| 11 | + |
| 12 | +test('parse returns promise if no callback is provided', (t,done) => { |
| 13 | + const server = createServer((req, res) => { |
| 14 | + const form = formidable(); |
| 15 | + |
| 16 | + const promise = form.parse(req); |
| 17 | + strictEqual(isPromise(promise), true); |
| 18 | + promise.then(([fields, files]) => { |
| 19 | + ok(typeof fields === 'object'); |
| 20 | + ok(typeof files === 'object'); |
| 21 | + res.writeHead(200); |
| 22 | + res.end("ok") |
| 23 | + }).catch(e => { |
| 24 | + done(e) |
| 25 | + }) |
| 26 | + }); |
| 27 | + |
| 28 | + server.listen(PORT, () => { |
| 29 | + const chosenPort = server.address().port; |
| 30 | + const body = `----13068458571765726332503797717\r |
| 31 | +Content-Disposition: form-data; name="title"\r |
| 32 | +\r |
| 33 | +a\r |
| 34 | +----13068458571765726332503797717\r |
| 35 | +Content-Disposition: form-data; name="multipleFiles"; filename="x.txt"\r |
| 36 | +Content-Type: application/x-javascript\r |
| 37 | +\r |
| 38 | +\r |
| 39 | +\r |
| 40 | +a\r |
| 41 | +b\r |
| 42 | +c\r |
| 43 | +d\r |
| 44 | +\r |
| 45 | +----13068458571765726332503797717--\r |
| 46 | +`; |
| 47 | + fetch(String(new URL(`http:localhost:${chosenPort}/`)), { |
| 48 | + method: 'POST', |
| 49 | + |
| 50 | + headers: { |
| 51 | + 'Content-Length': body.length, |
| 52 | + Host: `localhost:${chosenPort}`, |
| 53 | + 'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717', |
| 54 | + }, |
| 55 | + body |
| 56 | + }).then(res => { |
| 57 | + strictEqual(res.status, 200); |
| 58 | + server.close(); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +test('parse rejects with promise if it fails', (t,done) => { |
| 66 | + const server = createServer((req, res) => { |
| 67 | + const form = formidable({minFileSize: 10 ** 6}); // create condition to fail |
| 68 | + |
| 69 | + const promise = form.parse(req); |
| 70 | + strictEqual(isPromise(promise), true); |
| 71 | + promise.then(() => { |
| 72 | + done('should have failed') |
| 73 | + }).catch(e => { |
| 74 | + res.writeHead(e.httpCode); |
| 75 | + strictEqual(e.code, errors.smallerThanMinFileSize); |
| 76 | + res.end(String(e)) |
| 77 | + }) |
| 78 | + }); |
| 79 | + |
| 80 | + server.listen(PORT, () => { |
| 81 | + const chosenPort = server.address().port; |
| 82 | + const body = `----13068458571765726332503797717\r |
| 83 | +Content-Disposition: form-data; name="title"\r |
| 84 | +\r |
| 85 | +a\r |
| 86 | +----13068458571765726332503797717\r |
| 87 | +Content-Disposition: form-data; name="multipleFiles"; filename="x.txt"\r |
| 88 | +Content-Type: application/x-javascript\r |
| 89 | +\r |
| 90 | +\r |
| 91 | +\r |
| 92 | +a\r |
| 93 | +b\r |
| 94 | +c\r |
| 95 | +d\r |
| 96 | +\r |
| 97 | +----13068458571765726332503797717--\r |
| 98 | +`; |
| 99 | + fetch(String(new URL(`http:localhost:${chosenPort}/`)), { |
| 100 | + method: 'POST', |
| 101 | + |
| 102 | + headers: { |
| 103 | + 'Content-Length': body.length, |
| 104 | + Host: `localhost:${chosenPort}`, |
| 105 | + 'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717', |
| 106 | + }, |
| 107 | + body |
| 108 | + }).then(res => { |
| 109 | + strictEqual(res.status, 400); |
| 110 | + server.close(); |
| 111 | + done(); |
| 112 | + }); |
| 113 | + |
| 114 | + }); |
| 115 | +}); |
0 commit comments