1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
Description: update test for formidable 3
Author: Yadd <yadd@debian.org>
Forwarded: not-needed
Last-Update: 2022-07-17
--- a/test/unit/adapters/http.js
+++ b/test/unit/adapters/http.js
@@ -13,20 +13,14 @@
let server, proxy;
import AxiosError from '../../../lib/core/AxiosError.js';
import FormDataLegacy from 'form-data';
-import formidable from 'formidable';
+import {IncomingForm} from 'formidable';
import express from 'express';
-import multer from 'multer';
import bodyParser from 'body-parser';
const isBlobSupported = typeof Blob !== 'undefined';
import {Throttle} from 'stream-throttle';
import devNull from 'dev-null';
import {AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill.js';
import {__setProxy} from "../../../lib/adapters/http.js";
-import {FormData as FormDataPolyfill, Blob as BlobPolyfill, File as FilePolyfill} from 'formdata-node';
-
-const FormDataSpecCompliant = typeof FormData !== 'undefined' ? FormData : FormDataPolyfill;
-const BlobSpecCompliant = typeof Blob !== 'undefined' ? Blob : BlobPolyfill;
-const FileSpecCompliant = typeof File !== 'undefined' ? File : FilePolyfill;
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -113,20 +107,6 @@
}
}
-const handleFormData = (req) => {
- return new Promise((resolve, reject) => {
- const form = new formidable.IncomingForm();
-
- form.parse(req, (err, fields, files) => {
- if (err) {
- return reject(err);
- }
-
- resolve({fields, files});
- });
- });
-}
-
function generateReadableStream(length = 1024 * 1024, chunkSize = 10 * 1024, sleep = 50) {
return stream.Readable.from(async function* (){
let dataLength = 0;
@@ -1696,7 +1676,7 @@
form.append('fileStream', fileStream);
server = http.createServer(function (req, res) {
- var receivedForm = new formidable.IncomingForm();
+ var receivedForm = new IncomingForm();
assert.ok(req.rawHeaders.find(header => header.toLowerCase() === 'content-length'));
@@ -1716,116 +1696,21 @@
'Content-Type': 'multipart/form-data'
}
}).then(function (res) {
- assert.deepStrictEqual(res.data.fields, {foo: 'bar'});
+ assert.deepStrictEqual(res.data.fields, {foo: ['bar']});
- assert.strictEqual(res.data.files.file1.mimetype, 'image/jpeg');
- assert.strictEqual(res.data.files.file1.originalFilename, 'temp/bar.jpg');
- assert.strictEqual(res.data.files.file1.size, 3);
-
- assert.strictEqual(res.data.files.fileStream.mimetype, 'image/png');
- assert.strictEqual(res.data.files.fileStream.originalFilename, 'axios.png');
- assert.strictEqual(res.data.files.fileStream.size, stat.size);
+ assert.strictEqual(res.data.files.file1[0].mimetype, 'image/jpeg');
+ assert.strictEqual(res.data.files.file1[0].originalFilename, 'temp/bar.jpg');
+ assert.strictEqual(res.data.files.file1[0].size, 3);
+
+ assert.strictEqual(res.data.files.fileStream[0].mimetype, 'image/png');
+ assert.strictEqual(res.data.files.fileStream[0].originalFilename, 'axios.png');
+ assert.strictEqual(res.data.files.fileStream[0].size, stat.size);
done();
}).catch(done);
});
});
});
-
- describe('SpecCompliant FormData', (done) => {
- it('should allow passing FormData', async function () {
- server = await startHTTPServer(async (req, res) => {
- const {fields, files} = await handleFormData(req);
-
- res.end(JSON.stringify({
- fields,
- files
- }));
- });
-
- const form = new FormDataSpecCompliant();
-
- const blobContent = 'blob-content';
-
- const blob = new BlobSpecCompliant([blobContent], {type: 'image/jpeg'})
-
- form.append('foo1', 'bar1');
- form.append('foo2', 'bar2');
-
- form.append('file1', blob);
-
- const {data} = await axios.post(LOCAL_SERVER_URL, form, {
- maxRedirects: 0
- });
-
- assert.deepStrictEqual(data.fields, {foo1: 'bar1' ,'foo2': 'bar2'});
-
- assert.deepStrictEqual(typeof data.files.file1, 'object');
-
- const {size, mimetype, originalFilename} = data.files.file1;
-
- assert.deepStrictEqual(
- {size, mimetype, originalFilename},
- {mimetype: 'image/jpeg', originalFilename: 'blob', size: Buffer.from(blobContent).byteLength}
- );
- });
- });
-
- describe('toFormData helper', function () {
- it('should properly serialize nested objects for parsing with multer.js (express.js)', function (done) {
- var app = express();
-
- var obj = {
- arr1: ['1', '2', '3'],
- arr2: ['1', ['2'], '3'],
- obj: {x: '1', y: {z: '1'}},
- users: [{name: 'Peter', surname: 'griffin'}, {name: 'Thomas', surname: 'Anderson'}]
- };
-
- app.post('/', multer().none(), function (req, res, next) {
- res.send(JSON.stringify(req.body));
- });
-
- server = app.listen(3001, function () {
- // multer can parse the following key/value pairs to an array (indexes: null, false, true):
- // arr: '1'
- // arr: '2'
- // -------------
- // arr[]: '1'
- // arr[]: '2'
- // -------------
- // arr[0]: '1'
- // arr[1]: '2'
- // -------------
- Promise.all([null, false, true].map(function (mode) {
- return axios.postForm('http://localhost:3001/', obj, {formSerializer: {indexes: mode}})
- .then(function (res) {
- assert.deepStrictEqual(res.data, obj, 'Index mode ' + mode);
- });
- })).then(function (){
- done();
- }, done)
- });
- });
- });
- });
-
- describe('Blob', function () {
- it('should support Blob', async () => {
- server = await startHTTPServer(async (req, res) => {
- res.end(await getStream(req));
- });
-
- const blobContent = 'blob-content';
-
- const blob = new BlobSpecCompliant([blobContent], {type: 'image/jpeg'})
-
- const {data} = await axios.post(LOCAL_SERVER_URL, blob, {
- maxRedirects: 0
- });
-
- assert.deepStrictEqual(data, blobContent);
- });
});
describe('URLEncoded Form', function () {
|