Unverified Commit 3dc2e672 authored by Steven's avatar Steven Committed by GitHub

fix(next/image): add missing svg test refactor missing types (#65345)

The tests added in https://github.com/vercel/next.js/pull/46219 were
never correctly testing the headers because `detectContentType()` is
called first and only when we can't detect the type from the response
body do we fallback to the `Content-Type` header.

I also refactored some of the tests because the `ctx: any` type was
causing some tests to not run when testing different configuration
options.

Closes NEXT-3321
parent 180d4f5a
...@@ -138,6 +138,9 @@ export function detectContentType(buffer: Buffer) { ...@@ -138,6 +138,9 @@ export function detectContentType(buffer: Buffer) {
if ([0x3c, 0x3f, 0x78, 0x6d, 0x6c].every((b, i) => buffer[i] === b)) { if ([0x3c, 0x3f, 0x78, 0x6d, 0x6c].every((b, i) => buffer[i] === b)) {
return SVG return SVG
} }
if ([0x3c, 0x73, 0x76, 0x67].every((b, i) => buffer[i] === b)) {
return SVG
}
if ( if (
[0, 0, 0, 0, 0x66, 0x74, 0x79, 0x70, 0x61, 0x76, 0x69, 0x66].every( [0, 0, 0, 0, 0x66, 0x74, 0x79, 0x70, 0x61, 0x76, 0x69, 0x66].every(
(b, i) => !b || buffer[i] === b (b, i) => !b || buffer[i] === b
......
export default function handler(_req, res) { export default function handler(_req, res) {
res.setHeader('Content-Type', 'application/svg+xml') res.setHeader('Content-Type', 'application/svg+xml')
res.end( res.end('body is not svg to force fallback to Content-Type header')
`<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="60" height="60"><text x="20" y="30">hi</text></svg>`
)
} }
export default function handler(_req, res) { export default function handler(_req, res) {
res.setHeader('Content-Type', 'image/foo, text/html') res.setHeader('Content-Type', 'image/foo, text/html')
res.end( res.end('body is not svg to force fallback to Content-Type header')
`<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="60" height="60"><text x="20" y="30">hi</text></svg>`
)
} }
const images = [
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M/wHwAEBgIApD5fRAAAAABJRU5ErkJggg==',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPj/HwADBwIAMCbHYQAAAABJRU5ErkJggg==',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5/hPwAIAgL/4d1j8wAAAABJRU5ErkJggg==',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P//PwAGBAL/VJiKjgAAAABJRU5ErkJggg==',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=',
]
let state = 0
export default function handler(_req, res) {
state++
const index = state % images.length
const buffer = Buffer.from(images[index], 'base64')
res.setHeader('Content-Type', 'image/png')
return res.end(buffer)
}
export default function handler(_req, res) { export default function handler(_req, res) {
res.setHeader('Content-Type', 'image/SVG+XML') res.setHeader('Content-Type', 'image/SVG+XML')
res.end( res.end('body is not svg to force fallback to Content-Type header')
`<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="60" height="60"><text x="20" y="30">hi</text></svg>`
)
} }
export default function handler(_req, res) {
res.setHeader('Content-Type', 'image/png')
res.end(
`<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="60" height="60"><text x="20" y="30">hi</text></svg>`
)
}
import { join } from 'path'
import { setupTests } from './util'
const appDir = join(__dirname, '../app')
const imagesDir = join(appDir, '.next', 'cache', 'images')
describe('with dangerouslyAllowSVG config', () => {
setupTests({
nextConfigImages: { dangerouslyAllowSVG: true },
appDir,
imagesDir,
})
})
This diff is collapsed.
...@@ -22,6 +22,10 @@ describe('detectContentType', () => { ...@@ -22,6 +22,10 @@ describe('detectContentType', () => {
const buffer = await getImage('./images/test.svg') const buffer = await getImage('./images/test.svg')
expect(detectContentType(buffer)).toBe('image/svg+xml') expect(detectContentType(buffer)).toBe('image/svg+xml')
}) })
it('should return svg for inline svg', async () => {
const buffer = await getImage('./images/test-inline.svg')
expect(detectContentType(buffer)).toBe('image/svg+xml')
})
it('should return avif', async () => { it('should return avif', async () => {
const buffer = await getImage('./images/test.avif') const buffer = await getImage('./images/test.avif')
expect(detectContentType(buffer)).toBe('image/avif') expect(detectContentType(buffer)).toBe('image/avif')
......
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="400" height="400">
<path d="M0 200v200h400V0H0v200zm240.1-11.8 39.6 69.3-39.8.3c-22 .1-57.8.1-79.8 0l-39.8-.3 39.6-69.3c21.7-38 39.8-69.2 40.1-69.2.3 0 18.4 31.2 40.1 69.2z"/>
</svg>
\ No newline at end of file
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