rsnext/test/e2e/app-dir/next-image/next-image-proxy.test.ts
Tim Neutkens 59e042a9a7
Implement client_root for edge in Turbopack (#61024)
## What?

Implements https://github.com/vercel/turbo/pull/7081. Ensures image
imports in the edge runtime have the correct asset url. Previously it
would be `/assets/file.hash.png` but it should be
`/_next/static/media/file.hash.png`.

Updates the image tests to correctly compare the values in Turbopack and
the values in Webpack. Currently it only checks webpack values, causing
the tests to fail in Turbopack.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2194
2024-01-23 18:41:02 +01:00

120 lines
3.4 KiB
TypeScript

import { join } from 'path'
import { findPort, check } from 'next-test-utils'
import https from 'https'
import httpProxy from 'http-proxy'
import fs from 'fs'
import webdriver from 'next-webdriver'
import { createNextDescribe } from 'e2e-utils'
let proxyPort
let proxyServer: https.Server
createNextDescribe(
'next-image-proxy',
{
files: __dirname,
},
({ next }) => {
beforeAll(async () => {
proxyPort = await findPort()
const ssl = {
key: fs.readFileSync(
join(__dirname, 'certificates/localhost-key.pem'),
'utf8'
),
cert: fs.readFileSync(
join(__dirname, 'certificates/localhost.pem'),
'utf8'
),
}
const proxy = httpProxy.createProxyServer({
target: `http://localhost:${next.appPort}`,
ssl,
secure: false,
})
proxyServer = https.createServer(ssl, async (req, res) => {
proxy.web(req, res)
})
proxy.on('error', (err) => {
throw new Error('Failed to proxy: ' + err.message)
})
await new Promise<void>((resolve) => {
proxyServer.listen(proxyPort, () => resolve())
})
})
it('loads images without any errors', async () => {
let failCount = 0
let fulfilledCount = 0
const browser = await webdriver(`https://localhost:${proxyPort}`, '/', {
ignoreHTTPSErrors: true,
beforePageLoad(page) {
page.on('response', (response) => {
const url = response.url()
if (!url.includes('/_next/image')) return
const status = response.status()
console.log(`URL: ${url} Status: ${status}`)
if (!response.ok()) {
console.log(`Request failed: ${url}`)
failCount++
}
fulfilledCount++
})
},
})
const local = await browser.elementByCss('#app-page').getAttribute('src')
if (process.env.TURBOPACK) {
expect(local).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=90"`
)
} else {
expect(local).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=90"`
)
}
const remote = await browser
.elementByCss('#remote-app-page')
.getAttribute('src')
if (process.env.TURBOPACK) {
expect(remote).toMatchInlineSnapshot(
`"/_next/image?url=https%3A%2F%2Fimage-optimization-test.vercel.app%2Ftest.jpg&w=640&q=90"`
)
} else {
expect(remote).toMatchInlineSnapshot(
`"/_next/image?url=https%3A%2F%2Fimage-optimization-test.vercel.app%2Ftest.jpg&w=640&q=90"`
)
}
const expected = JSON.stringify({ fulfilledCount: 4, failCount: 0 })
await check(() => JSON.stringify({ fulfilledCount, failCount }), expected)
})
it('should work with connection upgrade by removing it via filterReqHeaders()', async () => {
const $ = await next.render$('/')
const url1 = $('#app-page').attr('src')
const opts = { headers: { connection: 'upgrade' } }
const res1 = await next.fetch(url1, opts)
expect(res1.status).toBe(200)
const url2 = $('#remote-app-page').attr('src')
const res2 = await next.fetch(url2, opts)
expect(res2.status).toBe(200)
})
afterAll(() => {
proxyServer.close()
})
}
)