|
| 1 | +import { plainToInstance } from 'class-transformer'; |
| 2 | +import { validate } from 'class-validator'; |
| 3 | +import { IsMimeTypeField, normalizeMimeType } from './mime-type.validator'; |
| 4 | + |
| 5 | +class TestDto { |
| 6 | + @IsMimeTypeField() |
| 7 | + fileType!: string; |
| 8 | +} |
| 9 | + |
| 10 | +const expectValid = async (input: unknown, expectedNormalized: string) => { |
| 11 | + const instance = plainToInstance(TestDto, { fileType: input }); |
| 12 | + const errors = await validate(instance); |
| 13 | + expect(errors).toEqual([]); |
| 14 | + expect(instance.fileType).toBe(expectedNormalized); |
| 15 | +}; |
| 16 | + |
| 17 | +const expectInvalid = async (input: unknown) => { |
| 18 | + const instance = plainToInstance(TestDto, { fileType: input }); |
| 19 | + const errors = await validate(instance); |
| 20 | + expect(errors.length).toBeGreaterThan(0); |
| 21 | +}; |
| 22 | + |
| 23 | +describe('normalizeMimeType', () => { |
| 24 | + it('returns non-strings unchanged', () => { |
| 25 | + expect(normalizeMimeType(undefined)).toBe(undefined); |
| 26 | + expect(normalizeMimeType(null)).toBe(null); |
| 27 | + expect(normalizeMimeType(123)).toBe(123); |
| 28 | + }); |
| 29 | + |
| 30 | + it('strips parameters, whitespace, and lowercases', () => { |
| 31 | + expect(normalizeMimeType('application/pdf')).toBe('application/pdf'); |
| 32 | + expect(normalizeMimeType('application/pdf;charset=utf-8')).toBe( |
| 33 | + 'application/pdf', |
| 34 | + ); |
| 35 | + expect(normalizeMimeType('application/pdf; charset=utf-8')).toBe( |
| 36 | + 'application/pdf', |
| 37 | + ); |
| 38 | + expect(normalizeMimeType(' application/PDF ')).toBe('application/pdf'); |
| 39 | + expect(normalizeMimeType('application/pdf\n')).toBe('application/pdf'); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('IsMimeTypeField', () => { |
| 44 | + describe('valid inputs (CS-217 regression)', () => { |
| 45 | + it('accepts application/pdf', async () => { |
| 46 | + await expectValid('application/pdf', 'application/pdf'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('accepts the xlsx vendor type', async () => { |
| 50 | + await expectValid( |
| 51 | + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 52 | + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('accepts application/pdf with charset parameter', async () => { |
| 57 | + await expectValid( |
| 58 | + 'application/pdf;charset=utf-8', |
| 59 | + 'application/pdf', |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('accepts xlsx with charset parameter', async () => { |
| 64 | + await expectValid( |
| 65 | + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8', |
| 66 | + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('accepts trailing whitespace', async () => { |
| 71 | + await expectValid('application/pdf ', 'application/pdf'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('accepts trailing newline', async () => { |
| 75 | + await expectValid('application/pdf\n', 'application/pdf'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('accepts uppercase', async () => { |
| 79 | + await expectValid('APPLICATION/PDF', 'application/pdf'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('accepts structured syntax suffix (e.g. +json)', async () => { |
| 83 | + await expectValid( |
| 84 | + 'application/vnd.api+json', |
| 85 | + 'application/vnd.api+json', |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('accepts image/svg+xml', async () => { |
| 90 | + await expectValid('image/svg+xml', 'image/svg+xml'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('invalid inputs', () => { |
| 95 | + it('rejects empty string', async () => { |
| 96 | + await expectInvalid(''); |
| 97 | + }); |
| 98 | + |
| 99 | + it('rejects missing slash', async () => { |
| 100 | + await expectInvalid('applicationpdf'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('rejects leading slash', async () => { |
| 104 | + await expectInvalid('/pdf'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('rejects trailing slash', async () => { |
| 108 | + await expectInvalid('application/'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('rejects spaces inside type/subtype', async () => { |
| 112 | + await expectInvalid('application /pdf'); |
| 113 | + await expectInvalid('application/ pdf'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('rejects multiple slashes', async () => { |
| 117 | + await expectInvalid('application/foo/bar'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('rejects non-string values', async () => { |
| 121 | + await expectInvalid(123); |
| 122 | + await expectInvalid(null); |
| 123 | + await expectInvalid(undefined); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments