File size: 1,259 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { createNext } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
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!')
  })
})