rsnext/test/development/api-cors-with-rewrite/index.test.ts
Severin Ibarluzea dce8c0ce9d
fix(#11930): rewritten api routes can correctly handle cors in dev mode (#38937)
* fix(#11930): rewritten api routes can correctly handle cors in dev mode

* test that request matches hot reloader middleware instead of api https://github.com/vercel/next.js/pull/38937#pullrequestreview-1055957698

* remove unnecessary imports

* remove unused constant

* remove api route import
2022-07-29 21:39:43 -05:00

45 lines
1.2 KiB
TypeScript

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!')
})
})