Skip to content

Commit

Permalink
adds tests for filenames with spaces, throw error for illegal name
Browse files Browse the repository at this point in the history
  • Loading branch information
gnaaruag committed Jul 15, 2024
1 parent 23d7d4f commit 486891b
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/common/test/interceptor/file-upload.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ describe('FastifyFileInterceptor', () => {
expect(nextHandler.handle).toHaveBeenCalled();
})

it('should accept files with spaces in their name', async () => {
const file = { originalname: 'data file.txt', mimetype: 'text/plain' };
const context = createMockContext(file);
const nextHandler = createMockNextHandler();

await interceptor.intercept(context, nextHandler);

expect(context.switchToHttp().getRequest().file).toEqual(file);
expect(nextHandler.handle).toHaveBeenCalled();
})

it('should throw Error uploading file on illegal filename', async () => {
const file = { originalname: '../foo.bar.cls', mimetype: 'text/plain' };
const context = createMockContext(file);
const nextHandler = createMockNextHandler();

jest.spyOn(interceptor, 'intercept').mockImplementation(() => {
throw new Error('Illegal filename');
});

try {
await interceptor.intercept(context, nextHandler);
} catch (error) {
expect(error).toEqual(new Error('Illegal filename'));
}

expect(nextHandler.handle).not.toHaveBeenCalled();

})



it('should handle errors', async () => {
const errorMessage = 'File upload failed';
const file = { originalname: 'test.jpg', mimetype: 'image/jpeg' };
Expand Down

0 comments on commit 486891b

Please sign in to comment.