From a0bf87d7e397f65a1b6ca856bfc887239cbc3830 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 2 Dec 2020 15:00:09 -0800 Subject: improve Accept-Language header parsing --- fatcat_scholar/hacks.py | 3 ++- tests/test_web.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/fatcat_scholar/hacks.py b/fatcat_scholar/hacks.py index 96226ff..5cb8572 100644 --- a/fatcat_scholar/hacks.py +++ b/fatcat_scholar/hacks.py @@ -60,7 +60,7 @@ def parse_accept_lang(header: str, options: typing.List[str]) -> typing.Optional """ if not header: return None - chunks = [v.split(";")[0].split("-")[0] for v in header.split(",")] + chunks = [v.split(";")[0].split("-")[0].split("_")[0] for v in header.split(",")] for c in chunks: if len(c) == 2 and c in options: return c @@ -72,6 +72,7 @@ def test_parse_accept_lang() -> None: assert parse_accept_lang("en,de", []) == None assert parse_accept_lang("en,de", ["en"]) == "en" assert parse_accept_lang("en-GB,de", ["en"]) == "en" + assert parse_accept_lang("zh_Hans_CN", ["en", "zh"]) == "zh" assert parse_accept_lang("en,de", ["de"]) == "de" assert ( parse_accept_lang("en-ca,en;q=0.8,en-us;q=0.6,de-de;q=0.4,de;q=0.2", ["de"]) diff --git a/tests/test_web.py b/tests/test_web.py index b3e99a1..a5629e0 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -25,6 +25,10 @@ def test_main_view(client: Any) -> None: assert resp.status_code == 200 assert "معلومات عن" in resp.content.decode("utf-8") + resp = client.get("/", headers={"Accept-Language": "zh_Hans_CN"}) + assert resp.status_code == 200 + assert "我们是" in resp.content.decode("utf-8") + def test_basic_api(client: Any) -> None: """ -- cgit v1.2.3