Unverified Commit 0562cc77 authored by Steven's avatar Steven Committed by GitHub

Add additional tests for image type detection (#26832)

Adding additional tests. Follow up to #26705 
parent 748b005c
......@@ -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
}
......
/* 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')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment