2017-10-28 22:16:34 +00:00
|
|
|
import fs from 'fs';
|
2017-10-11 21:43:21 +00:00
|
|
|
import {globalBeforeEach} from '../../__jest__/before-each';
|
2017-10-28 22:16:34 +00:00
|
|
|
import {buildMultipart, DEFAULT_BOUNDARY} from '../multipart';
|
2017-10-11 21:43:21 +00:00
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
describe('buildMultipart()', () => {
|
|
|
|
beforeEach(globalBeforeEach);
|
|
|
|
|
|
|
|
it('builds a simple request', async () => {
|
2017-10-28 22:16:34 +00:00
|
|
|
const {filePath, boundary, contentLength} = await buildMultipart([
|
2017-10-11 21:43:21 +00:00
|
|
|
{name: 'foo', value: 'bar'},
|
|
|
|
{name: 'multi-line', value: 'Hello\nWorld!'}
|
|
|
|
]);
|
|
|
|
|
2017-10-28 22:16:34 +00:00
|
|
|
expect(boundary).toBe(DEFAULT_BOUNDARY);
|
|
|
|
expect(contentLength).toBe(189);
|
|
|
|
expect(fs.readFileSync(filePath, 'utf8')).toBe([
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}`,
|
2017-10-11 21:43:21 +00:00
|
|
|
'Content-Disposition: form-data; name="foo"',
|
|
|
|
'',
|
|
|
|
'bar',
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}`,
|
2017-10-11 21:43:21 +00:00
|
|
|
'Content-Disposition: form-data; name="multi-line"',
|
|
|
|
'',
|
2017-10-12 11:34:51 +00:00
|
|
|
'Hello\nWorld!',
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}--`,
|
2017-10-12 11:34:51 +00:00
|
|
|
''
|
|
|
|
].join('\r\n'));
|
2017-10-11 21:43:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('builds with file', async () => {
|
|
|
|
const fileName = path.resolve(path.join(__dirname, './testfile.txt'));
|
2017-10-28 22:16:34 +00:00
|
|
|
const {filePath, boundary, contentLength} = await buildMultipart([
|
2017-10-11 21:43:21 +00:00
|
|
|
{name: 'foo', value: 'bar'},
|
|
|
|
{name: 'file', type: 'file', fileName: fileName},
|
|
|
|
{name: 'baz', value: 'qux'}
|
|
|
|
]);
|
|
|
|
|
2017-10-28 22:16:34 +00:00
|
|
|
expect(boundary).toBe(DEFAULT_BOUNDARY);
|
|
|
|
expect(contentLength).toBe(322);
|
|
|
|
expect(fs.readFileSync(filePath, 'utf8')).toBe([
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}`,
|
2017-10-11 21:43:21 +00:00
|
|
|
'Content-Disposition: form-data; name="foo"',
|
|
|
|
'',
|
|
|
|
'bar',
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}`,
|
2017-10-11 21:43:21 +00:00
|
|
|
'Content-Disposition: form-data; name="file"; filename="testfile.txt"',
|
|
|
|
'Content-Type: text/plain',
|
|
|
|
'',
|
2017-10-12 11:34:51 +00:00
|
|
|
'Hello World!\n\nHow are you?',
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}`,
|
2017-10-11 21:43:21 +00:00
|
|
|
'Content-Disposition: form-data; name="baz"',
|
|
|
|
'',
|
|
|
|
'qux',
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}--`,
|
2017-10-12 11:34:51 +00:00
|
|
|
''
|
|
|
|
].join('\r\n'));
|
2017-10-11 21:43:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('skips entries with no name or value', async () => {
|
2017-10-28 22:16:34 +00:00
|
|
|
const {filePath, boundary, contentLength} = await buildMultipart([
|
2017-10-11 21:43:21 +00:00
|
|
|
{value: 'bar'},
|
|
|
|
{name: 'foo'},
|
|
|
|
{name: '', value: ''},
|
|
|
|
{name: '', type: 'file', fileName: ''}
|
|
|
|
]);
|
|
|
|
|
2017-10-28 22:16:34 +00:00
|
|
|
expect(boundary).toBe(DEFAULT_BOUNDARY);
|
|
|
|
expect(contentLength).toBe(167);
|
|
|
|
expect(fs.readFileSync(filePath, 'utf8')).toBe([
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}`,
|
2017-10-11 21:43:21 +00:00
|
|
|
'Content-Disposition: form-data; name=""',
|
|
|
|
'',
|
|
|
|
'bar',
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}`,
|
2017-10-11 21:43:21 +00:00
|
|
|
'Content-Disposition: form-data; name="foo"',
|
|
|
|
'',
|
|
|
|
'',
|
2017-10-12 17:32:26 +00:00
|
|
|
`--${boundary}--`,
|
2017-10-12 11:34:51 +00:00
|
|
|
''
|
|
|
|
].join('\r\n'));
|
2017-10-11 21:43:21 +00:00
|
|
|
});
|
|
|
|
});
|