From 06684cd254eb76b54d41b57dbc50a3420c901568 Mon Sep 17 00:00:00 2001 From: Yifei Kong Date: Sun, 1 Oct 2023 16:31:02 +0800 Subject: [PATCH] Skip headers where value is None, mimics requests, fix #132 --- curl_cffi/requests/headers.py | 1 + tests/unittest/test_headers.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/unittest/test_headers.py diff --git a/curl_cffi/requests/headers.py b/curl_cffi/requests/headers.py index 7ddb8284..e049714a 100644 --- a/curl_cffi/requests/headers.py +++ b/curl_cffi/requests/headers.py @@ -85,6 +85,7 @@ def __init__( normalize_header_value(v, encoding), ) for k, v in headers.items() + if v is not None ] else: if isinstance(headers[0], (str, bytes)): diff --git a/tests/unittest/test_headers.py b/tests/unittest/test_headers.py new file mode 100644 index 00000000..9dc4f1f8 --- /dev/null +++ b/tests/unittest/test_headers.py @@ -0,0 +1,17 @@ +from curl_cffi.requests import Headers + + +def test_headers(): + headers = Headers() + headers['foo'] = 'bar' + headers['foo'] = 'baz' + assert headers['foo'] == 'baz' + assert headers.get('foo') == 'baz' + assert headers.get('bar') is None + assert headers + + +def test_headers_none_value(): + headers = Headers({"foo": None, "bar": ""}) + assert headers.get("foo") is None + assert headers["bar"] == ""