-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: promise * doc: change example to use promise * test: add test for promise * chore: version and changelog * doc: add promise
- Loading branch information
1 parent
c249922
commit e4f29e7
Showing
5 changed files
with
182 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import {strictEqual, ok} from 'node:assert'; | ||
import { createServer, request } from 'node:http'; | ||
import formidable, {errors} from '../../src/index.js'; | ||
import test from 'node:test'; | ||
|
||
const PORT = 13539; | ||
|
||
const isPromise = (x) => { | ||
return x && typeof x === `object` && typeof x.then === `function`; | ||
}; | ||
|
||
test('parse returns promise if no callback is provided', (t,done) => { | ||
const server = createServer((req, res) => { | ||
const form = formidable(); | ||
|
||
const promise = form.parse(req); | ||
strictEqual(isPromise(promise), true); | ||
promise.then(([fields, files]) => { | ||
ok(typeof fields === 'object'); | ||
ok(typeof files === 'object'); | ||
res.writeHead(200); | ||
res.end("ok") | ||
}).catch(e => { | ||
done(e) | ||
}) | ||
}); | ||
|
||
server.listen(PORT, () => { | ||
const chosenPort = server.address().port; | ||
const body = `----13068458571765726332503797717\r | ||
Content-Disposition: form-data; name="title"\r | ||
\r | ||
a\r | ||
----13068458571765726332503797717\r | ||
Content-Disposition: form-data; name="multipleFiles"; filename="x.txt"\r | ||
Content-Type: application/x-javascript\r | ||
\r | ||
\r | ||
\r | ||
a\r | ||
b\r | ||
c\r | ||
d\r | ||
\r | ||
----13068458571765726332503797717--\r | ||
`; | ||
fetch(String(new URL(`http:localhost:${chosenPort}/`)), { | ||
method: 'POST', | ||
|
||
headers: { | ||
'Content-Length': body.length, | ||
Host: `localhost:${chosenPort}`, | ||
'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717', | ||
}, | ||
body | ||
}).then(res => { | ||
strictEqual(res.status, 200); | ||
server.close(); | ||
done(); | ||
}); | ||
|
||
}); | ||
}); | ||
|
||
test('parse rejects with promise if it fails', (t,done) => { | ||
const server = createServer((req, res) => { | ||
const form = formidable({minFileSize: 10 ** 6}); // create condition to fail | ||
|
||
const promise = form.parse(req); | ||
strictEqual(isPromise(promise), true); | ||
promise.then(() => { | ||
done('should have failed') | ||
}).catch(e => { | ||
res.writeHead(e.httpCode); | ||
strictEqual(e.code, errors.smallerThanMinFileSize); | ||
res.end(String(e)) | ||
}) | ||
}); | ||
|
||
server.listen(PORT, () => { | ||
const chosenPort = server.address().port; | ||
const body = `----13068458571765726332503797717\r | ||
Content-Disposition: form-data; name="title"\r | ||
\r | ||
a\r | ||
----13068458571765726332503797717\r | ||
Content-Disposition: form-data; name="multipleFiles"; filename="x.txt"\r | ||
Content-Type: application/x-javascript\r | ||
\r | ||
\r | ||
\r | ||
a\r | ||
b\r | ||
c\r | ||
d\r | ||
\r | ||
----13068458571765726332503797717--\r | ||
`; | ||
fetch(String(new URL(`http:localhost:${chosenPort}/`)), { | ||
method: 'POST', | ||
|
||
headers: { | ||
'Content-Length': body.length, | ||
Host: `localhost:${chosenPort}`, | ||
'Content-Type': 'multipart/form-data; boundary=--13068458571765726332503797717', | ||
}, | ||
body | ||
}).then(res => { | ||
strictEqual(res.status, 400); | ||
server.close(); | ||
done(); | ||
}); | ||
|
||
}); | ||
}); |