@@ -2,32 +2,49 @@ import { expect } from 'chai';
2
2
import { IFile } from '../../../../common/pipes/file/interfaces' ;
3
3
import { FileTypeValidator } from '../../../pipes' ;
4
4
5
+ const pngBuffer = Buffer . from ( [
6
+ 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a , 0x00 , 0x00 , 0x00 , 0x0d , 0x49 ,
7
+ 0x48 , 0x44 , 0x52 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 , 0x08 , 0x06 ,
8
+ 0x00 , 0x00 , 0x00 , 0x1f , 0x15 , 0xc4 , 0x89 , 0x00 , 0x00 , 0x00 , 0x0a , 0x49 , 0x44 ,
9
+ 0x41 , 0x54 , 0x78 , 0x9c , 0x63 , 0x00 , 0x01 , 0x00 , 0x00 , 0x05 , 0x00 , 0x01 , 0x0d ,
10
+ 0x0a , 0x2d , 0xb4 , 0x00 , 0x00 , 0x00 , 0x00 , 0x49 , 0x45 , 0x4e , 0x44 , 0xae , 0x42 ,
11
+ 0x60 , 0x82 ,
12
+ ] ) ;
13
+
14
+ const jpegBuffer = Buffer . from ( [
15
+ 0xff , 0xd8 , 0xff , 0xe0 , 0x00 , 0x10 , 0x4a , 0x46 , 0x49 , 0x46 ,
16
+ ] ) ;
17
+
5
18
describe ( 'FileTypeValidator' , ( ) => {
6
19
describe ( 'isValid' , ( ) => {
7
- it ( 'should return true when the file buffer matches the specified type' , async ( ) => {
8
- const fileTypeValidator = new FileTypeValidator ( {
9
- fileType : 'image/jpeg' ,
20
+ describe ( 'support file types' , ( ) => {
21
+ async function testFileByMimeType ( mimeType , fileData ) {
22
+ const fileTypeValidator = new FileTypeValidator ( {
23
+ fileType : mimeType ,
24
+ } ) ;
25
+
26
+ const requestFile = {
27
+ mimetype : mimeType ,
28
+ buffer : fileData ,
29
+ } as IFile ;
30
+
31
+ expect ( await fileTypeValidator . isValid ( requestFile ) ) . to . equal ( true ) ;
32
+ }
33
+
34
+ it ( 'should be able to validate a JPEG file' , ( ) => {
35
+ return testFileByMimeType ( 'image/jpeg' , jpegBuffer ) ;
10
36
} ) ;
11
37
12
- const jpegBuffer = Buffer . from ( [
13
- 0xff , 0xd8 , 0xff , 0xe0 , 0x00 , 0x10 , 0x4a , 0x46 , 0x49 , 0x46 ,
14
- ] ) ;
15
- const requestFile = {
16
- mimetype : 'image/jpeg' ,
17
- buffer : jpegBuffer ,
18
- } as IFile ;
19
-
20
- expect ( await fileTypeValidator . isValid ( requestFile ) ) . to . equal ( true ) ;
38
+ it ( 'should be able to validate a PNG file' , ( ) => {
39
+ return testFileByMimeType ( 'image/png' , pngBuffer ) ;
40
+ } ) ;
21
41
} ) ;
22
42
23
43
it ( 'should return true when the file buffer matches the specified file extension' , async ( ) => {
24
44
const fileTypeValidator = new FileTypeValidator ( {
25
45
fileType : 'jpeg' ,
26
46
} ) ;
27
47
28
- const jpegBuffer = Buffer . from ( [
29
- 0xff , 0xd8 , 0xff , 0xe0 , 0x00 , 0x10 , 0x4a , 0x46 , 0x49 , 0x46 ,
30
- ] ) ;
31
48
const requestFile = {
32
49
mimetype : 'image/jpeg' ,
33
50
buffer : jpegBuffer ,
0 commit comments