From 4ea4af661d2b41feb3b2d602b297bc8784df3822 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Can=20Sar=C4=B1g=C3=B6l?= Date: Wed, 21 Aug 2019 01:39:05 +0300 Subject: [PATCH] Raise TypeError if invalid parameter for HTTPVersionConfig (#253) --- httpx/config.py | 10 +++++++++- tests/test_config.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/httpx/config.py b/httpx/config.py index b8544453..4082a9f7 100644 --- a/httpx/config.py +++ b/httpx/config.py @@ -251,8 +251,16 @@ class HTTPVersionConfig: self.http_versions = {http_versions.upper()} elif isinstance(http_versions, HTTPVersionConfig): self.http_versions = http_versions.http_versions + elif isinstance(http_versions, typing.Iterable): + self.http_versions = { + version.upper() if isinstance(version, str) else version + for version in http_versions + } else: - self.http_versions = {version.upper() for version in http_versions} + raise TypeError( + f"HTTP version should be a string or list of strings, " + "but got {type(http_versions)}" + ) for version in self.http_versions: if version not in ("HTTP/1.1", "HTTP/2"): diff --git a/tests/test_config.py b/tests/test_config.py index 6a862fd1..0df1029b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -115,6 +115,16 @@ def test_invalid_http_version(): httpx.HTTPVersionConfig("HTTP/9") +def test_invalid_http_version_type(): + with pytest.raises(TypeError): + httpx.HTTPVersionConfig(123) + + +def test_invalid_http_version_list_type(): + with pytest.raises(ValueError): + httpx.HTTPVersionConfig([123]) + + def test_empty_http_version(): with pytest.raises(ValueError): httpx.HTTPVersionConfig([]) -- 2.47.3