From 70a235b71c534556ff8b8974adb002a7327b1daa Mon Sep 17 00:00:00 2001 From: Hugo Tiburtino <45924645+hugotiburtino@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:53:20 +0100 Subject: [PATCH] fix(asset-proxy): be sure all headers are passed to the final endpoint --- __tests__/asset-proxy.ts | 17 ++++++++++++----- src/asset-proxy.ts | 10 ++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/__tests__/asset-proxy.ts b/__tests__/asset-proxy.ts index 09d55227..ec465cdd 100644 --- a/__tests__/asset-proxy.ts +++ b/__tests__/asset-proxy.ts @@ -47,11 +47,18 @@ test('request to https://asset-proxy.serlo.org/image?url=* gets asset from url q test('request to asset-proxy works also in case of other encodings', async () => { const env = currentTestEnvironment() - const response = await env.fetch({ - subdomain: 'asset-proxy', - pathname: - '/image?url=https://upload.wikimedia.org/wikipedia/commons/8/8b/Sinus_mit_y.svg', - }) + const response = await env.fetch( + { + subdomain: 'asset-proxy', + pathname: + '/image?url=https://upload.wikimedia.org/wikipedia/commons/8/8b/Sinus_mit_y.svg', + }, + { + headers: { + 'Accept-Encoding': '*', + }, + }, + ) expect(response.status).toBe(200) expect(response.headers.get('content-type')).toBe('image/svg+xml') expect(response.headers.get('Set-Cookie')).toBeNull() diff --git a/src/asset-proxy.ts b/src/asset-proxy.ts index 72427b3e..0e476b9a 100644 --- a/src/asset-proxy.ts +++ b/src/asset-proxy.ts @@ -18,10 +18,12 @@ export async function assetProxy(request: Request): Promise { return getPlaceholder() } - const originalResponse = await fetch(assetUrl, { - cf: { cacheTtl: 24 * 60 * 60 * 30 }, - headers: { 'Accept-Encoding': '*' }, - }) + const originalResponse = await fetch( + new Request(assetUrl.toString(), request), + { + cf: { cacheTtl: 24 * 60 * 60 * 30 }, + }, + ) if (originalResponse.ok && isImageResponse(originalResponse)) { const response = new Response(originalResponse.body, originalResponse)