-
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.
…37540) There was a bug that ignored `Request` options when one was given to the `fetch` function: ```ts const request = new Request("https://example.vercel.sh", { method: "POST" }); await fetch(request); ``` The code above was expected to make a `POST` request, but instead it made a `GET` request. This commit fixes it and adds some tests to verify that fetching with a `Request` object works as expected, and therefore resolves #37123. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md`
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> { | ||
const newObj = {} as Pick<T, K> | ||
for (const key of keys) { | ||
newObj[key] = obj[key] | ||
} | ||
return newObj | ||
} |
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
91 changes: 91 additions & 0 deletions
91
test/e2e/middleware-fetches-with-any-http-method/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,91 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
|
||
describe('Middleware fetches with any HTTP method', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'pages/api/ping.js': ` | ||
export default (req, res) => { | ||
res.send(JSON.stringify({ | ||
method: req.method, | ||
headers: {...req.headers}, | ||
})) | ||
} | ||
`, | ||
'middleware.js': ` | ||
import { NextResponse } from 'next/server'; | ||
const HTTP_ECHO_URL = 'https://http-echo-kou029w.vercel.app/'; | ||
export default async (req) => { | ||
const kind = req.nextUrl.searchParams.get('kind') | ||
const handler = handlers[kind] ?? handlers['normal-fetch']; | ||
const response = await handler({url: HTTP_ECHO_URL, method: req.method}); | ||
const json = await response.text() | ||
const res = NextResponse.next(); | ||
res.headers.set('x-resolved', json ?? '{}'); | ||
return res | ||
} | ||
const handlers = { | ||
'new-request': ({url, method}) => | ||
fetch(new Request(url, { method, headers: { 'x-kind': 'new-request' } })), | ||
'normal-fetch': ({url, method}) => | ||
fetch(url, { method, headers: { 'x-kind': 'normal-fetch' } }) | ||
} | ||
`, | ||
}, | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('passes the method on a direct fetch request', async () => { | ||
const response = await fetchViaHTTP( | ||
next.url, | ||
'/api/ping', | ||
{}, | ||
{ method: 'POST' } | ||
) | ||
const json = await response.json() | ||
expect(json).toMatchObject({ | ||
method: 'POST', | ||
}) | ||
|
||
const headerJson = JSON.parse(response.headers.get('x-resolved')) | ||
expect(headerJson).toMatchObject({ | ||
method: 'POST', | ||
headers: { | ||
'x-kind': 'normal-fetch', | ||
}, | ||
}) | ||
}) | ||
|
||
it('passes the method when providing a Request object', async () => { | ||
const response = await fetchViaHTTP( | ||
next.url, | ||
'/api/ping', | ||
{ kind: 'new-request' }, | ||
{ method: 'POST' } | ||
) | ||
const json = await response.json() | ||
expect(json).toMatchObject({ | ||
method: 'POST', | ||
}) | ||
|
||
const headerJson = JSON.parse(response.headers.get('x-resolved')) | ||
expect(headerJson).toMatchObject({ | ||
method: 'POST', | ||
headers: { | ||
'x-kind': 'new-request', | ||
}, | ||
}) | ||
}) | ||
}) |