forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(next/image): add missing svg test refactor missing types (vercel#…
…65345) The tests added in vercel#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
- Loading branch information
Showing
11 changed files
with
317 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
test/integration/image-optimizer/app/pages/api/application.svg.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
export default function handler(_req, res) { | ||
res.setHeader('Content-Type', 'application/svg+xml') | ||
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>` | ||
) | ||
res.end('body is not svg to force fallback to Content-Type header') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
export default function handler(_req, res) { | ||
res.setHeader('Content-Type', 'image/foo, text/html') | ||
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>` | ||
) | ||
res.end('body is not svg to force fallback to Content-Type header') | ||
} |
19 changes: 19 additions & 0 deletions
19
test/integration/image-optimizer/app/pages/api/stateful/test.png.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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) | ||
} |
4 changes: 1 addition & 3 deletions
4
test/integration/image-optimizer/app/pages/api/uppercase.svg.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
export default function handler(_req, res) { | ||
res.setHeader('Content-Type', 'image/SVG+XML') | ||
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>` | ||
) | ||
res.end('body is not svg to force fallback to Content-Type header') | ||
} |
6 changes: 6 additions & 0 deletions
6
test/integration/image-optimizer/app/pages/api/wrong-header.svg.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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>` | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
test/integration/image-optimizer/test/dangerously-allow-svg.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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, | ||
}) | ||
}) |
Oops, something went wrong.