-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Edge Functions to receive body (#37822)
This PR enables Edge API endpoints to receive a body. This wasn't in the original PR, as thankfully pointed out by @zaiste in [this comment](#37481 (comment)) 🙏 ## Related - Fixes #37821 ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md`
- Loading branch information
Showing
4 changed files
with
55 additions
and
4 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
11 changes: 11 additions & 0 deletions
11
test/e2e/edge-api-endpoints-can-receive-body/app/pages/api/edge.js
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,11 @@ | ||
export default async (req) => { | ||
if (!req.body) { | ||
return new Response('Body is required', { status: 400 }) | ||
} | ||
|
||
return new Response(`got: ${await req.text()}`) | ||
} | ||
|
||
export const config = { | ||
runtime: 'experimental-edge', | ||
} |
35 changes: 35 additions & 0 deletions
35
test/e2e/edge-api-endpoints-can-receive-body/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,35 @@ | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
import path from 'path' | ||
|
||
describe('Edge API endpoints can receive body', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'pages/api/edge.js': new FileRef( | ||
path.resolve(__dirname, './app/pages/api/edge.js') | ||
), | ||
}, | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('reads the body as text', async () => { | ||
const res = await fetchViaHTTP( | ||
next.url, | ||
'/api/edge', | ||
{}, | ||
{ | ||
body: 'hello, world.', | ||
method: 'POST', | ||
} | ||
) | ||
|
||
expect(res.status).toBe(200) | ||
expect(await res.text()).toBe('got: hello, world.') | ||
}) | ||
}) |