Skip to content

Commit

Permalink
Add additional tests for image type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Jul 1, 2021
1 parent 93f6254 commit a42ce34
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 @@ -497,7 +497,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 a42ce34

Please sign in to comment.