Skip to content

Commit

Permalink
Add failing test for using FormData with searchParams (#227)
Browse files Browse the repository at this point in the history
* test: Use `busboy` for `FormData with searchParams` case.

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* Update test/browser.js

Co-Authored-By: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>

Co-authored-by: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>
  • Loading branch information
avocadowastaken and szmarczak committed Feb 3, 2020
1 parent 9f09e06 commit d5cb15a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -50,6 +50,7 @@
"abort-controller": "^3.0.0",
"ava": "^3.2.0",
"body": "^5.1.0",
"busboy": "^0.3.1",
"codecov": "^3.6.4",
"create-test-server": "2.1.1",
"delay": "^4.1.0",
Expand Down
55 changes: 55 additions & 0 deletions test/browser.js
Expand Up @@ -2,6 +2,7 @@ import util from 'util';
import body from 'body';
import {serial as test} from 'ava';
import createTestServer from 'create-test-server';
import Busboy from 'busboy';
import withPage from './helpers/with-page';

const pBody = util.promisify(body);
Expand Down Expand Up @@ -202,6 +203,60 @@ test('FormData with searchParams', withPage, async (t, page) => {
await server.close();
});

// FIXME: More detailed test that reproduces the bug described in https://github.com/sindresorhus/ky/issues/209
test.failing('FormData with searchParams ("multipart/form-data" parser)', withPage, async (t, page) => {
t.plan(3);
const server = await createTestServer();
server.get('/', (request, response) => {
response.end();
});
server.post('/', async (request, response) => {
const [body, error] = await new Promise(resolve => {
const busboy = new Busboy({headers: request.headers});
busboy.on('error', error => resolve(null, error));
busboy.on('file', async (fieldname, file, filename, encoding, mimetype) => {
let fileContent = '';
try {
for await (const chunk of file) {
fileContent += chunk;
}

resolve([{fieldname, filename, encoding, mimetype, fileContent}]);
} catch (error_) {
resolve([null, error_]);
}
});
setTimeout(() => resolve([null, new Error('Timeout')]), 3000);
request.pipe(busboy);
});

response.end();

t.falsy(error);
t.deepEqual(request.query, {foo: '1'});
t.deepEqual(body, {
fieldname: 'file',
filename: 'my-file',
encoding: '7bit',
mimetype: 'text/plain',
fileContent: 'bubblegum pie'
});
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.evaluate(url => {
const formData = new window.FormData();
formData.append('file', new window.File(['bubblegum pie'], 'my-file', {type: 'text/plain'}));
return window.ky(url, {
method: 'post',
searchParams: 'foo=1',
body: formData
});
}, server.url);
await server.close();
});

test('headers are preserved when input is a Request and there are searchParams in the options', withPage, async (t, page) => {
t.plan(2);

Expand Down

0 comments on commit d5cb15a

Please sign in to comment.