|
| 1 | +import fetchProxy from "@opennextjs/aws/overrides/proxyExternalRequest/fetch.js"; |
| 2 | +import { vi } from "vitest"; |
| 3 | + |
| 4 | +describe("proxyExternalRequest/fetch", () => { |
| 5 | + // Note: if the url is hosted on the Cloudflare network we want to make sure that a `cf-connecting-ip` header is not being sent as that causes a DNS error |
| 6 | + // (see: https://developers.cloudflare.com/support/troubleshooting/cloudflare-errors/troubleshooting-cloudflare-1xxx-errors/#error-1000-dns-points-to-prohibited-ip) |
| 7 | + it("the proxy should remove any cf-connecting-ip headers (with any casing) before passing it to fetch", async () => { |
| 8 | + const fetchMock = vi.fn<typeof global.fetch>(async () => new Response()); |
| 9 | + globalThis.fetch = fetchMock; |
| 10 | + |
| 11 | + const { proxy } = fetchProxy; |
| 12 | + |
| 13 | + await proxy({ |
| 14 | + headers: { |
| 15 | + "header-1": "valid header 1", |
| 16 | + "header-2": "valid header 2", |
| 17 | + "cf-connecting-ip": "forbidden header 1", |
| 18 | + "header-3": "valid header 3", |
| 19 | + "CF-Connecting-IP": "forbidden header 2", |
| 20 | + "CF-CONNECTING-IP": "forbidden header 3", |
| 21 | + "header-4": "valid header 4", |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + expect(fetchMock.mock.calls.length).toEqual(1); |
| 26 | + |
| 27 | + const headersPassedToFetch = Object.keys( |
| 28 | + fetchMock.mock.calls[0][1]?.headers ?? {}, |
| 29 | + ); |
| 30 | + |
| 31 | + expect(headersPassedToFetch).toContain("header-1"); |
| 32 | + expect(headersPassedToFetch).toContain("header-2"); |
| 33 | + expect(headersPassedToFetch).not.toContain("cf-connecting-ip"); |
| 34 | + expect(headersPassedToFetch).toContain("header-3"); |
| 35 | + expect(headersPassedToFetch).not.toContain("CF-Connecting-IP"); |
| 36 | + expect(headersPassedToFetch).not.toContain("CF-CONNECTING-IP"); |
| 37 | + expect(headersPassedToFetch).toContain("header-4"); |
| 38 | + }); |
| 39 | +}); |
0 commit comments