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

Commit

Permalink
Add additional tests for image type detection (vercel#26832)
Browse files Browse the repository at this point in the history
Adding additional tests. Follow up to vercel#26705
  • Loading branch information
styfle committed Jul 1, 2021
1 parent b91d635 commit 4a5619d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ function parseCacheControl(str: string | null): Map<string, string> {
* it matches the "magic number" of known file signatures.
* https://en.wikipedia.org/wiki/List_of_file_signatures
*/
function detectContentType(buffer: Buffer) {
export function detectContentType(buffer: Buffer) {
if ([0xff, 0xd8, 0xff].every((b, i) => buffer[i] === b)) {
return JPEG
}
Expand Down
25 changes: 25 additions & 0 deletions test/integration/image-optimizer/test/detect-content-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-env jest */
import { detectContentType } from '../../../../packages/next/dist/server/image-optimizer.js'
import { readFile } from 'fs/promises'
import { join } from 'path'

const getImage = (filepath) => readFile(join(__dirname, filepath))

describe('detectContentType', () => {
it('should return jpg', async () => {
const buffer = await getImage('../public/test.jpg')
expect(detectContentType(buffer)).toBe('image/jpeg')
})
it('should return png', async () => {
const buffer = await getImage('../public/test.png')
expect(detectContentType(buffer)).toBe('image/png')
})
it('should return webp', async () => {
const buffer = await getImage('../public/animated.webp')
expect(detectContentType(buffer)).toBe('image/webp')
})
it('should return svg', async () => {
const buffer = await getImage('../public/test.svg')
expect(detectContentType(buffer)).toBe('image/svg+xml')
})
})

0 comments on commit 4a5619d

Please sign in to comment.