Unverified Commit 778a2a74 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix app routes on deploy (#45931)

Follow-up to https://github.com/vercel/next.js/pull/45716 this ensures
we correctly construct the initial URL value as it must be a fully
qualified URL. Existing tests caught this failure when running in deploy
mode.
parent 4697b159
......@@ -2051,6 +2051,8 @@ export default class NextNodeServer extends BaseServer {
const initUrl =
this.hostname && this.port
? `${protocol}://${this.hostname}:${this.port}${req.url}`
: (this.nextConfig.experimental as any).trustHostHeader
? `https://${req.headers.host || 'localhost'}${req.url}`
: req.url
addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
......
......@@ -5,6 +5,7 @@ import {
cookieWithRequestMeta,
} from './helpers'
import { Readable } from 'stream'
import { check } from 'next-test-utils'
createNextDescribe(
'app-custom-routes',
......@@ -92,26 +93,29 @@ createNextDescribe(
})
describe('body', () => {
it('can handle handle a streaming request and streaming response', async () => {
const body = new Array(10).fill(JSON.stringify({ ping: 'pong' }))
let index = 0
const stream = new Readable({
read() {
if (index >= body.length) return this.push(null)
this.push(body[index] + '\n')
index++
},
})
// we can't stream a body to a function currently only stream response
if (!(global as any).isNextDeploy) {
it('can handle handle a streaming request and streaming response', async () => {
const body = new Array(10).fill(JSON.stringify({ ping: 'pong' }))
let index = 0
const stream = new Readable({
read() {
if (index >= body.length) return this.push(null)
this.push(body[index] + '\n')
index++
},
})
const res = await next.fetch('/advanced/body/streaming', {
method: 'POST',
body: stream,
})
const res = await next.fetch('/advanced/body/streaming', {
method: 'POST',
body: stream,
})
expect(res.status).toEqual(200)
expect(await res.text()).toEqual(body.join('\n') + '\n')
})
expect(res.status).toEqual(200)
expect(await res.text()).toEqual(body.join('\n') + '\n')
})
}
it('can read a JSON encoded body', async () => {
const body = { ping: 'pong' }
......@@ -125,27 +129,30 @@ createNextDescribe(
expect(meta.body).toEqual(body)
})
it('can read a streamed JSON encoded body', async () => {
const body = { ping: 'pong' }
const encoded = JSON.stringify(body)
let index = 0
const stream = new Readable({
async read() {
if (index >= encoded.length) return this.push(null)
this.push(encoded[index])
index++
},
})
const res = await next.fetch('/advanced/body/json', {
method: 'POST',
body: stream,
})
// we can't stream a body to a function currently only stream response
if (!(global as any).isNextDeploy) {
it('can read a streamed JSON encoded body', async () => {
const body = { ping: 'pong' }
const encoded = JSON.stringify(body)
let index = 0
const stream = new Readable({
async read() {
if (index >= encoded.length) return this.push(null)
this.push(encoded[index])
index++
},
})
const res = await next.fetch('/advanced/body/json', {
method: 'POST',
body: stream,
})
expect(res.status).toEqual(200)
const meta = getRequestMeta(res.headers)
expect(meta.body).toEqual(body)
})
expect(res.status).toEqual(200)
const meta = getRequestMeta(res.headers)
expect(meta.body).toEqual(body)
})
}
it('can read the text body', async () => {
const body = 'hello, world'
......@@ -275,7 +282,13 @@ createNextDescribe(
expect(res.status).toEqual(500)
expect(await res.text()).toBeEmpty()
expect(next.cliOutput).toContain(error)
if (!(global as any).isNextDeploy) {
await check(() => {
expect(next.cliOutput).toContain(error)
return 'yes'
}, 'yes')
}
})
})
......
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