From: Tom Christie Date: Tue, 20 Aug 2019 11:33:28 +0000 (+0100) Subject: Test coverage for HTTPVersionConfig X-Git-Tag: 0.7.2~19^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc27fd253063fa54647823d78e9ea26fad3ba8e9;p=thirdparty%2Fhttpx.git Test coverage for HTTPVersionConfig --- diff --git a/httpx/config.py b/httpx/config.py index 3804effc..73a5eb34 100644 --- a/httpx/config.py +++ b/httpx/config.py @@ -236,15 +236,18 @@ class HTTPVersionConfig: http_versions = ['HTTP/1.1', 'HTTP/2'] if isinstance(http_versions, str): - self.http_versions = set([http_versions]) + self.http_versions = set([http_versions.upper()]) elif isinstance(http_versions, HTTPVersionConfig): self.http_versions = http_versions.http_versions else: - self.http_versions = set(sorted(http_versions)) + self.http_versions = set(sorted([version.upper() for version in http_versions])) for version in self.http_versions: if version not in ('HTTP/1.1', 'HTTP/2'): - raise ValueError(f"Unsupported protocol value {protocol!r}") + raise ValueError(f"Unsupported HTTP version {version!r}.") + + if not self.http_versions: + raise ValueError(f"HTTP versions cannot be an empty list.") @property def alpn_strings(self) -> typing.List[str]: @@ -256,7 +259,7 @@ class HTTPVersionConfig: def __repr__(self) -> str: class_name = self.__class__.__name__ - value = list(self.http_versions) + value = sorted(list(self.http_versions)) return f"{class_name}({value!r})" diff --git a/tests/test_config.py b/tests/test_config.py index 20befa7c..6a862fd1 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -90,6 +90,36 @@ def test_ssl_repr(): assert repr(ssl) == "SSLConfig(cert=None, verify=False)" +def test_http_versions_repr(): + http_versions = httpx.HTTPVersionConfig() + assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1', 'HTTP/2'])" + + +def test_http_versions_from_string(): + http_versions = httpx.HTTPVersionConfig("HTTP/1.1") + assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])" + + +def test_http_versions_from_list(): + http_versions = httpx.HTTPVersionConfig(["http/1.1"]) + assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])" + + +def test_http_versions_from_config(): + http_versions = httpx.HTTPVersionConfig(httpx.HTTPVersionConfig("HTTP/1.1")) + assert repr(http_versions) == "HTTPVersionConfig(['HTTP/1.1'])" + + +def test_invalid_http_version(): + with pytest.raises(ValueError): + httpx.HTTPVersionConfig("HTTP/9") + + +def test_empty_http_version(): + with pytest.raises(ValueError): + httpx.HTTPVersionConfig([]) + + def test_timeout_repr(): timeout = httpx.TimeoutConfig(timeout=5.0) assert repr(timeout) == "TimeoutConfig(timeout=5.0)"