test(middleware-multipart): basic test case for in-memory parsing
MODIFY
packages/middleware-multipart/src/multipart.interface.ts
packages/middleware-multipart/src/multipart.interface.ts
@@ -6,7 +6,7 @@ export interface File {
size?: number;
encoding: string;
mimetype: string;
- filename: string;
+ filename?: string;
fieldname: string;
}

ADD
packages/middleware-multipart/src/specs/index.spec.ts
+import * as API from '../index';
+
+describe('@marblejs/middleware-multipart public API', () => {
+ test('apis are defined', () => {
+ expect(API.multipart$).toBeDefined();
+ });
+});

ADD
packages/middleware-multipart/src/specs/multipart.middleware.spec.ts
+import * as request from 'supertest';
+import { r, use, httpListener, createContext } from '@marblejs/core';
+import { multipart$ } from '../multipart.middleware';
+import { map } from 'rxjs/operators';
+
+const effect$ = r.pipe(
+ r.matchPath('/'),
+ r.matchType('POST'),
+ r.useEffect(req$ => req$.pipe(
+ use(multipart$()),
+ map(req => ({ body: {
+ file: req.file,
+ fields: req.body,
+ }}))
+ )));
+
+const app = httpListener({ effects: [effect$] }).run(createContext());
+
+describe('multipart$', () => {
+ test('parses multipart/form-data request and stores it in-memory', async () => {
+ const data = Buffer.from('test_buffer_1');
+
+ return request(app)
+ .post('/')
+ .field('field_1', 'test_1')
+ .field('field_2', 'test_2')
+ .attach('data_field', data)
+ .expect(200)
+ .then(({ body }) => {
+ // data_field
+ expect(body.file.data_field.size).toEqual(Buffer.byteLength(data));
+ expect(body.file.data_field.destination).toBeUndefined();
+ expect(body.file.data_field.filename).toBeUndefined();
+ expect(body.file.data_field.encoding).toEqual('7bit');
+ expect(body.file.data_field.mimetype).toEqual('application/octet-stream');
+ expect(body.file.data_field.fieldname).toEqual('data_field');
+ expect(data.equals(Buffer.from(body.file.data_field.buffer))).toBe(true);
+
+ // field_1, field_2
+ expect(body.fields.field_1).toEqual('test_1');
+ expect(body.fields.field_2).toEqual('test_2');
+ });
+ });
+});

test(middleware-multipart): basic test case for in-memory parsing