Also handle QtWebKit Accept-Language correctly
On QtWebKit, we only set Accept-Language in the request interceptor.
This commit is contained in:
parent
0144a314ad
commit
a397aa069e
|
|
@ -26,8 +26,15 @@ class CallSuper(Exception):
|
|||
"""Raised when the caller should call the superclass instead."""
|
||||
|
||||
|
||||
def custom_headers(url):
|
||||
"""Get the combined custom headers."""
|
||||
def custom_headers(
|
||||
url: QUrl, *, fallback_accept_language: bool = True
|
||||
) -> list[tuple[bytes, bytes]]:
|
||||
"""Get the combined custom headers.
|
||||
|
||||
Arguments:
|
||||
fallback_accept_language: Whether to include the global (rather than
|
||||
per-domain override) accept language header as well.
|
||||
"""
|
||||
headers = {}
|
||||
|
||||
dnt_config = config.instance.get('content.headers.do_not_track', url=url)
|
||||
|
|
@ -41,8 +48,9 @@ def custom_headers(url):
|
|||
encoded_value = b"" if value is None else value.encode('ascii')
|
||||
headers[encoded_header] = encoded_value
|
||||
|
||||
# note: Once we drop QtWebKit, we could hardcode fallback_accept_language to False.
|
||||
accept_language = config.instance.get('content.headers.accept_language',
|
||||
url=url, fallback=False)
|
||||
url=url, fallback=fallback_accept_language)
|
||||
if accept_language is not None and not isinstance(accept_language, usertypes.Unset):
|
||||
headers[b'Accept-Language'] = accept_language.encode('ascii')
|
||||
|
||||
|
|
|
|||
|
|
@ -187,7 +187,9 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor):
|
|||
if request.is_blocked:
|
||||
info.block(True)
|
||||
|
||||
for header, value in shared.custom_headers(url=url):
|
||||
for header, value in shared.custom_headers(
|
||||
url=url, fallback_accept_language=not is_xhr
|
||||
):
|
||||
if header.lower() == b'accept' and is_xhr:
|
||||
# https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
|
||||
# says: "If no Accept header has been set using this, an Accept header
|
||||
|
|
|
|||
|
|
@ -36,14 +36,17 @@ def test_custom_headers(config_stub, dnt, accept_language, custom_headers,
|
|||
assert shared.custom_headers(url=None) == expected_items
|
||||
|
||||
|
||||
@pytest.mark.parametrize("url, expected", [
|
||||
(None, True), # url is never None in the wild, mostly sanity check
|
||||
(QUrl("http://example.org"), False),
|
||||
@pytest.mark.parametrize("url, fallback, expected", [
|
||||
# url is never None in the wild, mostly sanity check
|
||||
(None, True, True),
|
||||
(None, False, True),
|
||||
(QUrl("http://example.org"), True, True),
|
||||
(QUrl("http://example.org"), False, False),
|
||||
])
|
||||
def test_accept_language_no_fallback(config_stub, url ,expected):
|
||||
def test_accept_language_no_fallback(config_stub, url, fallback, expected):
|
||||
config_stub.val.content.headers.accept_language = "de, en"
|
||||
has_header = b"Accept-Language" in dict(shared.custom_headers(url=url))
|
||||
assert has_header == expected
|
||||
headers = shared.custom_headers(url=url, fallback_accept_language=fallback)
|
||||
assert (b"Accept-Language" in dict(headers)) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
|
|||
Loading…
Reference in New Issue