Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 4a5619d

Browse files
authored
Add additional tests for image type detection (vercel#26832)
Adding additional tests. Follow up to vercel#26705
1 parent b91d635 commit 4a5619d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/next/server/image-optimizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ function parseCacheControl(str: string | null): Map<string, string> {
498498
* it matches the "magic number" of known file signatures.
499499
* https://en.wikipedia.org/wiki/List_of_file_signatures
500500
*/
501-
function detectContentType(buffer: Buffer) {
501+
export function detectContentType(buffer: Buffer) {
502502
if ([0xff, 0xd8, 0xff].every((b, i) => buffer[i] === b)) {
503503
return JPEG
504504
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-env jest */
2+
import { detectContentType } from '../../../../packages/next/dist/server/image-optimizer.js'
3+
import { readFile } from 'fs/promises'
4+
import { join } from 'path'
5+
6+
const getImage = (filepath) => readFile(join(__dirname, filepath))
7+
8+
describe('detectContentType', () => {
9+
it('should return jpg', async () => {
10+
const buffer = await getImage('../public/test.jpg')
11+
expect(detectContentType(buffer)).toBe('image/jpeg')
12+
})
13+
it('should return png', async () => {
14+
const buffer = await getImage('../public/test.png')
15+
expect(detectContentType(buffer)).toBe('image/png')
16+
})
17+
it('should return webp', async () => {
18+
const buffer = await getImage('../public/animated.webp')
19+
expect(detectContentType(buffer)).toBe('image/webp')
20+
})
21+
it('should return svg', async () => {
22+
const buffer = await getImage('../public/test.svg')
23+
expect(detectContentType(buffer)).toBe('image/svg+xml')
24+
})
25+
})

0 commit comments

Comments
 (0)