Skip to content

Commit

Permalink
tests: rewrite test for Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
GrosSacASac committed Jun 28, 2023
1 parent 0179abf commit d90b69d
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions test/standalone/keep-alive-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import formidable from '../../src/index.js';
let ok = 0;
let errors = 0;

const PORT = 0;
const PORT = 89;

test('keep alive error', (done) => {
const server = createServer((req, res) => {
const server = createServer(async (req, res) => {
const form = formidable();
form.on('error', () => {
errors += 1;
Expand All @@ -23,13 +23,35 @@ test('keep alive error', (done) => {
res.writeHead(200);
res.end();
});
form.parse(req);
try {
await form.parse(req);
// for client two
strictEqual(ok, 1, `should "ok" count === 1, has: ${ok}`);

server.close();
done();
} catch (formidableError) {
strictEqual(errors, 1, `should "errors" === 1, has: ${errors}`);

const clientTwo = createConnection(PORT);

// correct post upload (with hyphens)
clientTwo.write(
'POST /upload-test HTTP/1.1\r\n' +
'Host: localhost\r\n' +
'Connection: keep-alive\r\n' +
'Content-Type: multipart/form-data; boundary=----aaa\r\n' +
'Content-Length: 13\r\n\r\n' +
'------aaa--\r\n',
);
clientTwo.end();

}
});

server.listen(PORT, () => {
const chosenPort = server.address().port;

const client = createConnection(chosenPort);
const client = createConnection(PORT);

// first send malformed (boundary / hyphens) post upload
client.write(
Expand All @@ -47,29 +69,6 @@ test('keep alive error', (done) => {
client.write(buf);
client.end();

setTimeout(() => {
strictEqual(errors, 1, `should "errors" === 1, has: ${errors}`);

const clientTwo = createConnection(chosenPort);

// correct post upload (with hyphens)
clientTwo.write(
'POST /upload-test HTTP/1.1\r\n' +
'Host: localhost\r\n' +
'Connection: keep-alive\r\n' +
'Content-Type: multipart/form-data; boundary=----aaa\r\n' +
'Content-Length: 13\r\n\r\n' +
'------aaa--\r\n',
);
clientTwo.end();

setTimeout(() => {
strictEqual(ok, 1, `should "ok" count === 1, has: ${ok}`);

server.close();
done();
}, 300);
}, 200);
}, 150);
});
});

0 comments on commit d90b69d

Please sign in to comment.