Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(next): bind new stream functions with base incomingMessage object #37806

Merged
merged 9 commits into from
Jun 27, 2022
2 changes: 1 addition & 1 deletion packages/next/server/body-streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function replaceRequestBody<T extends IncomingMessage>(
for (const key in stream) {
let v = stream[key as keyof Readable] as any
if (typeof v === 'function') {
v = v.bind(stream)
v = v.bind(base)
}
base[key as keyof T] = v
}
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/proxy-request-with-middleware/app/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextResponse } from 'next/server'

export async function middleware() {
return NextResponse.next()
}
28 changes: 28 additions & 0 deletions test/e2e/proxy-request-with-middleware/app/pages/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import request from 'request'

export const config = {
api: {
bodyParser: false,
},
}

export default function handler(req, res) {
return req
.pipe(
request(
`http://${
// node v18 resolves to IPv6 by default so force IPv4
process.version.startsWith('v18.')
? `127.0.0.1:${req.headers.host.split(':').pop() || ''}`
: req.headers.host
}${req.url}/post`,
{
followAllRedirects: false,
followRedirect: false,
gzip: true,
json: false,
}
)
)
.pipe(res)
}
12 changes: 12 additions & 0 deletions test/e2e/proxy-request-with-middleware/app/pages/api/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const config = {
api: {
bodyParser: false,
},
}

export default function handler(req, res) {
return res.json({
method: req.method,
headers: req.headers,
})
}
53 changes: 53 additions & 0 deletions test/e2e/proxy-request-with-middleware/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-env jest */

import { join } from 'path'
import { fetchViaHTTP } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNext, FileRef } from 'e2e-utils'

describe('Requests not effected when middleware used', () => {
let next: NextInstance

afterAll(() => next.destroy())
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, '../app/pages')),
'middleware.js': new FileRef(join(__dirname, '../app/middleware.js')),
},
dependencies: {
request: '^2.88.2',
},
})
})

sendRequest('GET')
sendRequest('POST')

function sendRequest(method) {
const body = !['get', 'head'].includes(method.toLowerCase())
? JSON.stringify({
key: 'value',
})
: undefined
it(`should proxy ${method} request ${
body ? 'with body' : ''
}`, async () => {
const headers = {
'content-type': 'application/json',
'x-custom-header': 'some value',
}
const res = await fetchViaHTTP(next.url, `api`, '', {
method: method.toUpperCase(),
headers,
body: method.toLowerCase() !== 'get' ? body : undefined,
})
const data = await res.json()
expect(data.method).toEqual(method)
if (body) {
expect(data.headers['content-length']).toEqual(String(body.length))
}
expect(data.headers).toEqual(expect.objectContaining(headers))
})
}
})