Skip to content

Commit

Permalink
refactor: Remove depr. request in tests (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting authored Jan 2, 2022
1 parent b627d79 commit 1618ea0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 287 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"nyc": "15.1.0",
"prettier": "2.0.5",
"prettier-plugin-pkgjson": "0.2.8",
"request": "2.88.2",
"supertest": "6.1.6"
},
"jest": {
Expand Down
59 changes: 21 additions & 38 deletions test/standalone/issue-46.test.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,39 @@
import { createServer } from 'node:http';
import { ok, strictEqual } from 'node:assert';
import request from 'request';
import formidable from '../../src/index.js';
import { createServer, request } from "node:http";
import { ok, strictEqual } from "node:assert";
import { Buffer } from 'node:buffer';
import formidable from "../../src/index.js";

// OS choosing port
const PORT = 13531;
const type = "multipart/related; boundary=a7a65b99-8a61-4e2c-b149-f73a3b35f923"
const body = "LS1hN2E2NWI5OS04YTYxLTRlMmMtYjE0OS1mNzNhM2IzNWY5MjMNCmNvbnRlbnQtZGlzcG9zaXRpb246IGZvcm0tZGF0YTsgbmFtZT0iZm9vIg0KDQpiYXJyeQ0KLS1hN2E2NWI5OS04YTYxLTRlMmMtYjE0OS1mNzNhM2IzNWY5MjMtLQ";
const buffer = Buffer.from(body, 'base64url');

const indexForm = `
<form action="/" method="post" enctype="multipart/form-data">
<input type="text" name="foo" />
<input type="submit" />
</form>
`;

test('issue 46', (done) => {
test("issue 46", (done) => {
const server = createServer((req, res) => {
// Show a form for testing purposes.
if (req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(indexForm);
return;
}

// Parse form and write results to response.
const form = formidable();
form.parse(req, (err, fields, files) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
// ? old, makes more sense to be passed to `.end()`?
// res.write(JSON.stringify({ err, fields, files }));
res.end(JSON.stringify({ err, fields, files }));
ok(fields.foo, 'should have fields.foo === barry');
strictEqual(fields.foo[0], 'barry');
server.close();
done();
});
});

server.listen(PORT, () => {
const choosenPort = server.address().port;
const url = `http://localhost:${choosenPort}`;

const parts = [
{
'content-disposition': 'form-data; name="foo"',
body: 'barry',
},
];

request({ method: 'POST', url, multipart: parts }, (e, res, body) => {
const obj = JSON.parse(body);

ok(obj.fields.foo, 'should have fields.foo === barry');
strictEqual(obj.fields.foo[0], 'barry');

server.close();
done();
const req = request(url, {
method: "POST",
headers: {
"Content-Type": type,
"Content-Length": buffer.byteLength
}
});

req.write(buffer);
req.end();
});
});
Loading

0 comments on commit 1618ea0

Please sign in to comment.