From 380311ced29e6ddbb749cc2bcfedb4bd4b1a4d67 Mon Sep 17 00:00:00 2001 From: Abdelhakim Qbaich Date: Tue, 2 May 2023 22:54:41 -0400 Subject: [PATCH 1/3] Pass SKIP_HEADER const when header is unset --- httpie/client.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/httpie/client.py b/httpie/client.py index 815ec559b2..a96b246701 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -10,6 +10,7 @@ import requests # noinspection PyPackageRequirements import urllib3 +from urllib3.util import SKIP_HEADER, SKIPPABLE_HEADERS from . import __version__ from .adapters import HTTPieHTTPAdapter @@ -200,6 +201,10 @@ def finalize_headers(headers: HTTPHeadersDict) -> HTTPHeadersDict: if isinstance(value, str): # See value = value.encode() + elif name.lower() in SKIPPABLE_HEADERS: + # Some headers get overwritten by urllib3 when set to `None` + # and should be replaced with the `SKIP_HEADER` constant. + value = SKIP_HEADER final_headers.add(name, value) return final_headers From 0b3d5656c733474c29e616b31a037b9a29d709a7 Mon Sep 17 00:00:00 2001 From: Abdelhakim Qbaich Date: Sun, 7 May 2023 11:14:30 -0400 Subject: [PATCH 2/3] Hide SKIP_HEADER constant when displaying headers --- httpie/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/httpie/models.py b/httpie/models.py index 6fe02d5fcf..a0a68c8ddc 100644 --- a/httpie/models.py +++ b/httpie/models.py @@ -1,6 +1,7 @@ from time import monotonic import requests +from urllib3.util import SKIP_HEADER, SKIPPABLE_HEADERS from enum import Enum, auto from typing import Iterable, Union, NamedTuple @@ -152,6 +153,7 @@ def headers(self): headers = [ f'{name}: {value if isinstance(value, str) else value.decode()}' for name, value in headers.items() + if not (name.lower() in SKIPPABLE_HEADERS and value == SKIP_HEADER) ] headers.insert(0, request_line) From a950ce364d59747aa62978b347e6fafd0df9f4b4 Mon Sep 17 00:00:00 2001 From: Abdelhakim Qbaich Date: Sun, 7 May 2023 12:20:49 -0400 Subject: [PATCH 3/3] Test that omits User-Agent --- tests/test_httpie.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_httpie.py b/tests/test_httpie.py index f07f34b9a5..281dc35600 100644 --- a/tests/test_httpie.py +++ b/tests/test_httpie.py @@ -196,6 +196,14 @@ def test_unset_host_header(httpbin_both): assert 'Host' not in r.json['headers'] # default Host unset +def test_unset_useragent_header(httpbin_both): + r = http('GET', httpbin_both + '/headers') + assert 'User-Agent' in r.json['headers'] # default User-Agent present + + r = http('GET', httpbin_both + '/headers', 'User-Agent:') + assert 'User-Agent' not in r.json['headers'] # default User-Agent unset + + def test_headers_empty_value(httpbin_both): r = http('GET', httpbin_both + '/headers') assert r.json['headers']['Accept'] # default Accept has value