From 60869c2598321386f57e55537e8c99ed011fbb95 Mon Sep 17 00:00:00 2001 From: Mark Ladyshau <47859603+mrkldshv@users.noreply.github.com> Date: Sun, 19 Jun 2022 23:33:24 +0200 Subject: [PATCH] fix(ext/fetch): add `accept-language` default header to fetch (#14882) --- cli/tests/unit/fetch_test.ts | 36 ++++++++++++++++++++++++++++++++++++ ext/fetch/26_fetch.js | 4 ++++ tools/wpt/expectation.json | 10 +++------- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 0720a05318..7adbb77216 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -643,6 +643,39 @@ Deno.test( "hello: World\r\n", "foo: Bar\r\n", "accept: */*\r\n", + "accept-language: *\r\n", + `user-agent: Deno/${Deno.version.deno}\r\n`, + "accept-encoding: gzip, br\r\n", + `host: ${addr}\r\n\r\n`, + ].join(""); + assertEquals(actual, expected); + }, +); + +Deno.test( + { + permissions: { net: true }, + }, + async function fetchRequestAcceptHeaders() { + const addr = "127.0.0.1:4501"; + const bufPromise = bufferServer(addr); + const response = await fetch(`http://${addr}/blah`, { + method: "POST", + headers: [ + ["Accept", "text/html"], + ["Accept-Language", "en-US"], + ], + }); + await response.arrayBuffer(); + assertEquals(response.status, 404); + assertEquals(response.headers.get("Content-Length"), "2"); + + const actual = new TextDecoder().decode((await bufPromise).bytes()); + const expected = [ + "POST /blah HTTP/1.1\r\n", + "content-length: 0\r\n", + "accept: text/html\r\n", + "accept-language: en-US\r\n", `user-agent: Deno/${Deno.version.deno}\r\n`, "accept-encoding: gzip, br\r\n", `host: ${addr}\r\n\r\n`, @@ -678,6 +711,7 @@ Deno.test( "foo: Bar\r\n", "content-type: text/plain;charset=UTF-8\r\n", "accept: */*\r\n", + "accept-language: *\r\n", `user-agent: Deno/${Deno.version.deno}\r\n`, "accept-encoding: gzip, br\r\n", `host: ${addr}\r\n`, @@ -715,6 +749,7 @@ Deno.test( "hello: World\r\n", "foo: Bar\r\n", "accept: */*\r\n", + "accept-language: *\r\n", `user-agent: Deno/${Deno.version.deno}\r\n`, "accept-encoding: gzip, br\r\n", `host: ${addr}\r\n`, @@ -1072,6 +1107,7 @@ Deno.test( "hello: World\r\n", "foo: Bar\r\n", "accept: */*\r\n", + "accept-language: *\r\n", `user-agent: Deno/${Deno.version.deno}\r\n`, "accept-encoding: gzip, br\r\n", `host: ${addr}\r\n`, diff --git a/ext/fetch/26_fetch.js b/ext/fetch/26_fetch.js index 7ef9bdaf70..9a4916e21f 100644 --- a/ext/fetch/26_fetch.js +++ b/ext/fetch/26_fetch.js @@ -452,6 +452,10 @@ ArrayPrototypePush(request.headerList, ["Accept", "*/*"]); } + if (!requestObject.headers.has("Accept-Language")) { + ArrayPrototypePush(request.headerList, ["Accept-Language", "*"]); + } + // 12. opPromise = PromisePrototypeCatch( PromisePrototypeThen( diff --git a/tools/wpt/expectation.json b/tools/wpt/expectation.json index 3b25e5fbb7..df2f246551 100644 --- a/tools/wpt/expectation.json +++ b/tools/wpt/expectation.json @@ -3018,12 +3018,8 @@ ], "text-utf8.any.html": true, "text-utf8.any.worker.html": true, - "accept-header.any.html": [ - "Request through fetch should have a 'accept-language' header" - ], - "accept-header.any.worker.html": [ - "Request through fetch should have a 'accept-language' header" - ], + "accept-header.any.html": true, + "accept-header.any.worker.html": true, "conditional-get.any.html": false, "conditional-get.any.worker.html": false, "error-after-response.any.html": false, @@ -4613,4 +4609,4 @@ "idlharness.https.any.worker.html": true, "idlharness-shadowrealm.window.html": false } -} +} \ No newline at end of file