-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow reading request bodies in middlewares
- Loading branch information
Showing
6 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
test/production/reading-request-body-in-middleware/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
|
||
describe('reading request body in middleware', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'src/readBody.js': ` | ||
export async function readBody(reader, input = reader.read(), body = "") { | ||
const { value, done } = await input; | ||
const inputText = new TextDecoder().decode(value); | ||
body += inputText; | ||
if (done) { | ||
return body; | ||
} | ||
const next = await reader.read(); | ||
return readBody(reader, next, body); | ||
} | ||
`, | ||
|
||
'pages/_middleware.js': ` | ||
const { NextResponse } = require('next/server'); | ||
import { readBody } from '../src/readBody'; | ||
export default async function middleware(request) { | ||
if (!request.body) { | ||
return new Response('No body', { status: 400 }); | ||
} | ||
const reader = await request.body.getReader(); | ||
const body = await readBody(reader); | ||
const json = JSON.parse(body); | ||
if (request.nextUrl.searchParams.has("next")) { | ||
return NextResponse.next(); | ||
} | ||
return new Response(JSON.stringify({ | ||
root: true, | ||
...json, | ||
}), { | ||
status: 200, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}) | ||
} | ||
`, | ||
|
||
'pages/nested/_middleware.js': ` | ||
const { NextResponse } = require('next/server'); | ||
import { readBody } from '../../src/readBody'; | ||
export default async function middleware(request) { | ||
if (!request.body) { | ||
return new Response('No body', { status: 400 }); | ||
} | ||
const reader = await request.body.getReader(); | ||
const body = await readBody(reader); | ||
const json = JSON.parse(body); | ||
return new Response(JSON.stringify({ | ||
root: false, | ||
...json, | ||
}), { | ||
status: 200, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}) | ||
} | ||
`, | ||
}, | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('rejects with 400 for get requests', async () => { | ||
const response = await fetchViaHTTP(next.url, '/') | ||
expect(response.status).toEqual(400) | ||
}) | ||
|
||
it('returns root: true for root calls', async () => { | ||
const response = await fetchViaHTTP( | ||
next.url, | ||
'/', | ||
{}, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
} | ||
) | ||
expect(response.status).toEqual(200) | ||
expect(await response.json()).toEqual({ | ||
foo: 'bar', | ||
root: true, | ||
}) | ||
}) | ||
|
||
it('reads the same body on both middlewares', async () => { | ||
const response = await fetchViaHTTP( | ||
next.url, | ||
'/nested/hello', | ||
{ | ||
next: '1', | ||
}, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
} | ||
) | ||
expect(response.status).toEqual(200) | ||
expect(await response.json()).toEqual({ | ||
foo: 'bar', | ||
root: false, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20812,8 +20812,7 @@ [email protected]: | |
source-list-map "^2.0.0" | ||
source-map "~0.6.1" | ||
|
||
"webpack-sources3@npm:[email protected]", webpack-sources@^3.2.3: | ||
name webpack-sources3 | ||
"webpack-sources3@npm:[email protected]", webpack-sources@^3.2.2, webpack-sources@^3.2.3: | ||
version "3.2.3" | ||
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde" | ||
integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w== | ||
|