Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
210 additions
and
7 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
Large diffs are not rendered by default.
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
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,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,124 @@ | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils' | ||
import FormData from 'form-data' | ||
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 fetchViaHTTP( | ||
next.url, | ||
'/api/nothing?middleware-handler=json', | ||
null, | ||
{ | ||
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 () => { | ||
try { | ||
const response = await fetchViaHTTP( | ||
next.url, | ||
'/api/nothing?middleware-handler=text', | ||
null, | ||
{ | ||
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"}', | ||
}, | ||
}) | ||
} catch (err) { | ||
console.log('FAILED', err) | ||
} | ||
}) | ||
|
||
it('reads an URL encoded form data', async () => { | ||
const response = await fetchViaHTTP( | ||
next.url, | ||
'/api/nothing?middleware-handler=formData', | ||
null, | ||
{ | ||
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.append('hello', 'world') | ||
|
||
const response = await fetchViaHTTP( | ||
next.url, | ||
'/api/nothing?middleware-handler=formData', | ||
null, | ||
{ | ||
method: 'POST', | ||
body: formData, | ||
} | ||
) | ||
|
||
expect(await serialize(response)).toMatchObject({ | ||
text: expect.stringContaining('ok'), | ||
status: 200, | ||
headers: { | ||
'x-req-type': 'formData', | ||
'x-serialized': '{"hello":"world"}', | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |