-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[middleware] add tests for body reading methods
- Loading branch information
Showing
7 changed files
with
179 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
.vercel |
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,49 @@ | ||
// @ts-check | ||
|
||
import { NextResponse } from 'next/server' | ||
|
||
/** | ||
* @param {NextRequest} req | ||
*/ | ||
export default async function middleware(req) { | ||
const res = NextResponse.next() | ||
res.headers.set('x-incoming-content-type', req.headers.get('content-type')) | ||
|
||
const handler = | ||
bodyHandlers[req.nextUrl.searchParams.get('middleware-handler')] | ||
const headers = await handler?.(req) | ||
for (const [key, value] of headers ?? []) { | ||
res.headers.set(key, value) | ||
} | ||
|
||
return res | ||
} | ||
|
||
/** | ||
* @typedef {import('next/server').NextRequest} NextRequest | ||
* @typedef {(req: NextRequest) => Promise<[string, string][]>} Handler | ||
* @type {Record<string, Handler>} | ||
*/ | ||
const bodyHandlers = { | ||
json: async (req) => { | ||
const json = await req.json() | ||
return [ | ||
['x-req-type', 'json'], | ||
['x-serialized', JSON.stringify(json)], | ||
] | ||
}, | ||
text: async (req) => { | ||
const text = await req.text() | ||
return [ | ||
['x-req-type', 'text'], | ||
['x-serialized', text], | ||
] | ||
}, | ||
formData: async (req) => { | ||
const formData = await req.formData() | ||
return [ | ||
['x-req-type', 'formData'], | ||
['x-serialized', JSON.stringify(Object.fromEntries(formData))], | ||
] | ||
}, | ||
} |
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,7 @@ | ||
{ | ||
"dependencies": { | ||
"next": "canary", | ||
"react": "latest", | ||
"react-dom": "latest" | ||
} | ||
} |
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,3 @@ | ||
export default (_req, res) => { | ||
res.send('ok') | ||
} |
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,109 @@ | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { renderViaHTTP } from 'next-test-utils' | ||
import { FormData, fetch } from '@edge-runtime/primitives' | ||
import path from 'path' | ||
|
||
async function serialize(response: Response) { | ||
return { | ||
text: await response.text(), | ||
headers: Object.fromEntries(response.headers), | ||
status: response.status, | ||
} | ||
} | ||
|
||
describe('Edge can read request body', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: new FileRef(path.resolve(__dirname, './app')), | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('renders the static page', async () => { | ||
const html = await renderViaHTTP(next.url, '/api/nothing') | ||
expect(html).toContain('ok') | ||
}) | ||
|
||
describe('middleware', () => { | ||
it('reads a JSON body', async () => { | ||
const response = await fetch( | ||
`${next.url}/api/nothing?middleware-handler=json`, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ hello: 'world' }), | ||
} | ||
) | ||
expect(await serialize(response)).toMatchObject({ | ||
text: expect.stringContaining('ok'), | ||
status: 200, | ||
headers: { | ||
'x-req-type': 'json', | ||
'x-serialized': '{"hello":"world"}', | ||
}, | ||
}) | ||
}) | ||
|
||
it('reads a text body', async () => { | ||
const response = await fetch( | ||
`${next.url}/api/nothing?middleware-handler=text`, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ hello: 'world' }), | ||
} | ||
) | ||
expect(await serialize(response)).toMatchObject({ | ||
text: expect.stringContaining('ok'), | ||
status: 200, | ||
headers: { | ||
'x-req-type': 'text', | ||
'x-serialized': '{"hello":"world"}', | ||
}, | ||
}) | ||
}) | ||
|
||
it('reads an URL encoded form data', async () => { | ||
const response = await fetch( | ||
`${next.url}/api/nothing?middleware-handler=formData`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
body: new URLSearchParams({ hello: 'world' }).toString(), | ||
} | ||
) | ||
expect(await serialize(response)).toMatchObject({ | ||
text: expect.stringContaining('ok'), | ||
status: 200, | ||
headers: { | ||
'x-req-type': 'formData', | ||
'x-serialized': '{"hello":"world"}', | ||
}, | ||
}) | ||
}) | ||
|
||
it('reads a multipart form data', async () => { | ||
const formData = new FormData() | ||
formData.set('hello', 'world') | ||
const response = await fetch( | ||
`${next.url}/api/nothing?middleware-handler=formData`, | ||
{ | ||
method: 'POST', | ||
body: formData, | ||
} | ||
) | ||
expect(await serialize(response)).toMatchObject({ | ||
text: expect.stringContaining('ok'), | ||
status: 200, | ||
headers: { | ||
'x-req-type': 'formData', | ||
'x-serialized': '{"hello":"world"}', | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |