test(middleware-multipart): `multipart.util.ts` test cases
MODIFY
packages/middleware-multipart/src/multipart.parser.file.ts
packages/middleware-multipart/src/multipart.parser.file.ts
@@ -5,7 +5,7 @@ import { fromEvent, Observable, of, throwError, merge, iif } from 'rxjs';
import { mapTo, map, mergeMap, takeUntil, toArray, tap, mergeMapTo, take, ignoreElements } from 'rxjs/operators';
import { Readable } from 'stream';
import { FileIncomingData, ParserOpts } from './multipart.interface';
-import { setRequestData, isFieldnameParseable } from './multipart.util';
+import { setRequestData, shouldParseFieldname } from './multipart.util';
type FileEvent = [string, NodeJS.ReadableStream, string, string, string];
@@ -23,7 +23,7 @@ export const parseFile = (req: HttpRequest) => (opts: ParserOpts) => (event$: Ob
takeUntil(finish$),
map(([ fieldname, file, filename, encoding, mimetype ]) => ({ fieldname, file, filename, encoding, mimetype })),
mergeMap(data => iif(
- () => !isFieldnameParseable(opts.files)(data.fieldname),
+ () => !shouldParseFieldname(opts.files)(data.fieldname),
throwError(new HttpError(`File "${data.fieldname}" is not acceptable`, HttpStatus.PRECONDITION_FAILED)),
of(data),
)),

MODIFY
packages/middleware-multipart/src/multipart.util.ts
packages/middleware-multipart/src/multipart.util.ts
@@ -4,13 +4,13 @@ import { FileIncomingData } from './multipart.interface';
type ComputedFileData = Partial<{ buffer: Buffer; destination: any; size: number }>;
-export const isProperMethod = (req: HttpRequest): boolean =>
+const isProperMethod = (req: HttpRequest): boolean =>
['POST', 'PUT'].includes(req.method);
-export const isMultipart = (req: HttpRequest): boolean =>
+const isMultipart = (req: HttpRequest): boolean =>
getContentType(req.headers).includes('multipart/');
-export const isFieldnameParseable = (files: string[] | undefined) => (fieldname: string) =>
+export const shouldParseFieldname = (files: string[] | undefined) => (fieldname: string) =>
!!files ? files.includes(fieldname) : true;
export const shouldParseMultipart = (req: HttpRequest) =>

ADD
packages/middleware-multipart/src/specs/multipart.util.spec.ts
+import { createHttpRequest } from '@marblejs/core/dist/+internal/testing';
+import { shouldParseFieldname, shouldParseMultipart, setRequestData } from '../multipart.util';
+import { FileIncomingData } from '../multipart.interface';
+
+test('#shouldParseFieldname checks if given fieldname can be parsed by middleware', () => {
+ expect(shouldParseFieldname(undefined)('test')).toEqual(true);
+ expect(shouldParseFieldname(['test_1'])('test_1')).toEqual(true);
+ expect(shouldParseFieldname(['test_1'])('test_2')).toEqual(false);
+ expect(shouldParseFieldname([])('test')).toEqual(false);
+});
+
+test('#shouldParseMultipart checks if incoming request can be parsed by middleware', () => {
+ // given
+ const multipartRequest = createHttpRequest({
+ method: 'POST',
+ headers: { 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'},
+ });
+
+ const nonMultipartRequest1 = createHttpRequest({
+ method: 'GET',
+ headers: { 'Content-Type': 'application/json'},
+ });
+
+ const nonMultipartRequest2 = createHttpRequest({
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json'},
+ });
+
+ // then
+ expect(shouldParseMultipart(multipartRequest)).toEqual(true);
+ expect(shouldParseMultipart(nonMultipartRequest1)).toEqual(false);
+ expect(shouldParseMultipart(nonMultipartRequest2)).toEqual(false);
+});
+
+test('#setRequestData sets file related request data', () => {
+ // given
+ const bufferedData = Buffer.from('test');
+ const bufferedDataLength = Buffer.byteLength(bufferedData);
+ const incomingHttpRequest = createHttpRequest();
+ const incomingFile: FileIncomingData = {
+ mimetype: 'image/svg+xml',
+ encoding: '7bit',
+ fieldname: 'image_1',
+ filename: 'shape.svg',
+ file: undefined as any as NodeJS.ReadableStream,
+ };
+ const computedFileData = {
+ buffer: bufferedData,
+ size: bufferedDataLength,
+ destination: 'test/path/to/file',
+ };
+
+ // when
+ setRequestData(incomingHttpRequest)(incomingFile)(computedFileData);
+
+ // then
+ expect(incomingHttpRequest.file).toEqual({
+ 'image_1': {
+ size: bufferedDataLength,
+ buffer: bufferedData,
+ destination: computedFileData.destination,
+ encoding: incomingFile.encoding,
+ mimetype: incomingFile.mimetype,
+ filename: incomingFile.filename,
+ fieldname: incomingFile.fieldname,
+ },
+ })
+});

test(middleware-multipart): `multipart.util.ts` test cases