Skip to content

Commit

Permalink
Add test for invalid image
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Oct 15, 2020
1 parent c53aec6 commit 17cddcb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/next/next-server/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ export async function imageOptimizer(
const upstreamRes = await fetch(href)

if (!upstreamRes.ok) {
server.logError(
new Error(`Unexpected status ${upstreamRes.status} from upstream ${href}`)
)
res.statusCode = upstreamRes.status
res.end('"url" parameter is valid but upstream response is invalid')
return { finished: true }
}

Expand Down
10 changes: 10 additions & 0 deletions test/integration/image-optimizer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ function runTests({ w, isDev }) {
expect(await res.text()).toBe(`"url" parameter is invalid`)
})

it('should fail when url fails to load an image', async () => {
const url = `http://localhost:${appPort}/not-an-image`
const query = { w, url, q: 100 }
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})
expect(res.status).toBe(404)
expect(await res.text()).toBe(
`"url" parameter is valid but upstream response is invalid`
)
})

it('should use cached image file when parameters are the same', async () => {
await fs.remove(imagesDir)

Expand Down

0 comments on commit 17cddcb

Please sign in to comment.