Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive redirection occurs when redirecting within the handler #993

Closed
snaka opened this issue Feb 13, 2024 · 3 comments
Closed

Recursive redirection occurs when redirecting within the handler #993

snaka opened this issue Feb 13, 2024 · 3 comments
Labels

Comments

@snaka
Copy link

snaka commented Feb 13, 2024

Note:
This is not a problem caused by this library itself, but rather an issue that can arise when using the library. It is being documented as an issue with the intention of highlighting potential problems that may occur when using the library, and to request its inclusion in the documentation.

Overview of the Issue

When redirecting within an API handler, if you simply call fetch() in the test, it recursively triggers redirection. As a result, an error stating redirect count exceeded occurs.

Reproduction Test Code

  test('redirect', async () => {
    await testApiHandler({
      pagesHandler: (_, res) => res.redirect(302, '/'),
      test: async ({ fetch }) => {
        const res = await fetch({ method: 'GET' })
        expect(res.redirected).toBeTruthy()
      }
    })
  })

Executing this code results in the following error:

    TypeError: fetch failed

       8 |       pagesHandler: (_, res) => res.redirect(302, '/'),
       9 |       test: async ({ fetch }) => {
    > 10 |         const res = await fetch({ method: 'GET' })
         |                     ^
      11 |         expect(res.redirected).toBeTruthy()
      12 |       }
      13 |     })

(snip)

    Cause:
    redirect count exceeded

      at async node:internal/deps/undici/undici:10342:20
      at async node:internal/deps/undici/undici:10342:20
      at async node:internal/deps/undici/undici:10342:20
      (The above line repeats)

Solution Approach

By adding redirect: 'manual' when calling fetch(), it prevents automatic redirection to the redirect destination, allowing validation of the response.

Updated test code:

  test.skip('redirect', async () => {
    await testApiHandler({
      pagesHandler: (_, res) => res.redirect(302, '/'),
      test: async ({ fetch }) => {
        const res = await fetch({ method: 'GET', redirect: 'manual' })
        expect(res.status).toEqual(302)
        expect(res.headers.get('location')).toEqual('/')
      }
    })
  })

See also: https://developer.mozilla.org/en-US/docs/Web/API/fetch#redirect

Expected Fix

To prevent similar issues in the future, it would be beneficial to explicitly mention this in the documentation.

@Xunnamius
Copy link
Owner

Thanks for the heads up. That's interesting, it may be a undici thing since I've tested redirects with the pages router back when NTARH used the old node-fetch. I'll investigate and update the docs accordingly.

@snaka
Copy link
Author

snaka commented Feb 14, 2024

My next info outputs is as follows

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Fri Jan 27 02:56:13 UTC 2023
Binaries:
  Node: 20.11.0
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 14.0.4
  eslint-config-next: 14.0.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Xunnamius added a commit that referenced this issue Mar 2, 2024
### [4.0.4](v4.0.3...v4.0.4) (2024-03-02)

#### 🪄 Fixes

* **src:** allow relative url strings passed via url shorthand for App Router ([01b86b6](01b86b6)) <sup>closes [#1000](https://github.com/Xunnamius/next-test-api-route-handler/issues/1000)</sup>
* **src:** prevent recursive redirection with undici/whatwg fetch ([22bb716](22bb716)) <sup>closes [#993](https://github.com/Xunnamius/next-test-api-route-handler/issues/993)</sup>
* **src:** replace `AppRouteUserlandModule` with looser type ([502e666](502e666)) <sup>closes [#1006](#1006), [#1005](https://github.com/Xunnamius/next-test-api-route-handler/issues/1005)</sup>
@Xunnamius
Copy link
Owner

🎉 This issue has been resolved in version 4.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants