diff --git a/docker/utils/decorators.py b/docker/utils/decorators.py index 7c41a5f8..46c28a80 100644 --- a/docker/utils/decorators.py +++ b/docker/utils/decorators.py @@ -40,7 +40,7 @@ def minimum_version(version): def update_headers(f): def inner(self, *args, **kwargs): if 'HttpHeaders' in self._auth_configs: - if 'headers' not in kwargs: + if not kwargs.get('headers'): kwargs['headers'] = self._auth_configs['HttpHeaders'] else: kwargs['headers'].update(self._auth_configs['HttpHeaders']) diff --git a/tests/unit/utils_test.py b/tests/unit/utils_test.py index 0f7a58c9..47ced433 100644 --- a/tests/unit/utils_test.py +++ b/tests/unit/utils_test.py @@ -20,9 +20,11 @@ from docker.utils import ( create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file, exclude_paths, convert_volume_binds, decode_json_header, tar, split_command, create_ipam_config, create_ipam_pool, parse_devices, + update_headers, ) -from docker.utils.utils import create_endpoint_config + from docker.utils.ports import build_port_bindings, split_port +from docker.utils.utils import create_endpoint_config from .. import base from ..helpers import make_tree @@ -34,6 +36,37 @@ TEST_CERT_DIR = os.path.join( ) +class DecoratorsTest(base.BaseTestCase): + def test_update_headers(self): + sample_headers = { + 'X-Docker-Locale': 'en-US', + } + + def f(self, headers=None): + return headers + + client = Client() + client._auth_configs = {} + + g = update_headers(f) + assert g(client, headers=None) is None + assert g(client, headers={}) == {} + assert g(client, headers={'Content-type': 'application/json'}) == { + 'Content-type': 'application/json', + } + + client._auth_configs = { + 'HttpHeaders': sample_headers + } + + assert g(client, headers=None) == sample_headers + assert g(client, headers={}) == sample_headers + assert g(client, headers={'Content-type': 'application/json'}) == { + 'Content-type': 'application/json', + 'X-Docker-Locale': 'en-US', + } + + class HostConfigTest(base.BaseTestCase): def test_create_host_config_no_options(self): config = create_host_config(version='1.19')