forked from vercel/next.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vercel#11930): rewritten api routes can correctly handle cors in …
…dev mode
- Loading branch information
Showing
2 changed files
with
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
|
||
describe('Rewritten API Requests should pass OPTIONS requests to the api function', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'pages/api/some-endpoint.js': ` | ||
export default (req, res) => { | ||
res.end("successfully hit some-endpoint!") | ||
} | ||
`, | ||
}, | ||
nextConfig: { | ||
rewrites: () => | ||
Promise.resolve({ | ||
beforeFiles: [ | ||
// Nextjs by default requires a /api prefix, let's remove that | ||
{ | ||
source: '/:path*', | ||
destination: '/api/:path*', | ||
}, | ||
], | ||
afterFiles: [], | ||
fallback: [], | ||
}), | ||
}, | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('should pass OPTIONS requests to the api function', async () => { | ||
const res = await fetchViaHTTP(next.url, '/some-endpoint', null, { | ||
method: 'OPTIONS', | ||
headers: { | ||
Origin: 'http://localhost:3000', | ||
}, | ||
}) | ||
expect(await res.text()).toContain('successfully hit some-endpoint!') | ||
}) | ||
}) |