Unverified Commit 99ee2225 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Normalize image optimizer error status codes (#34699)

parent b3f2b388
...@@ -259,7 +259,13 @@ export class ImageError extends Error { ...@@ -259,7 +259,13 @@ export class ImageError extends Error {
constructor(statusCode: number, message: string) { constructor(statusCode: number, message: string) {
super(message) super(message)
this.statusCode = statusCode
// ensure an error status is used > 400
if (statusCode >= 400) {
this.statusCode = statusCode
} else {
this.statusCode = 500
}
} }
} }
......
...@@ -25,11 +25,17 @@ export async function serveSlowImage() { ...@@ -25,11 +25,17 @@ export async function serveSlowImage() {
const server = http.createServer(async (req, res) => { const server = http.createServer(async (req, res) => {
const parsedUrl = new URL(req.url, 'http://localhost') const parsedUrl = new URL(req.url, 'http://localhost')
const delay = Number(parsedUrl.searchParams.get('delay')) || 500 const delay = Number(parsedUrl.searchParams.get('delay')) || 500
const status = Number(parsedUrl.searchParams.get('status')) || 200
console.log('delay image for', delay) console.log('delaying image for', delay)
await waitFor(delay) await waitFor(delay)
res.statusCode = 200 res.statusCode = status
if (status === 308) {
res.end('invalid status')
return
}
res.setHeader('content-type', 'image/png') res.setHeader('content-type', 'image/png')
res.end(await fs.readFile(join(__dirname, '../app/public/test.png'))) res.end(await fs.readFile(join(__dirname, '../app/public/test.png')))
}) })
...@@ -127,6 +133,19 @@ export function runTests(ctx) { ...@@ -127,6 +133,19 @@ export function runTests(ctx) {
slowImageServer.stop() slowImageServer.stop()
}) })
if (ctx.domains.includes('localhost')) {
it('should normalize invalid status codes', async () => {
const url = `http://localhost:${
slowImageServer.port
}/slow.png?delay=${1}&status=308`
const query = { url, w: ctx.w, q: 39 }
const opts = { headers: { accept: 'image/webp' }, redirect: 'manual' }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(500)
})
}
it('should return home page', async () => { it('should return home page', async () => {
const res = await fetchViaHTTP(ctx.appPort, '/', null, {}) const res = await fetchViaHTTP(ctx.appPort, '/', null, {})
expect(await res.text()).toMatch(/Image Optimizer Home/m) expect(await res.text()).toMatch(/Image Optimizer Home/m)
......
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