Get test suite back to ~100% line coverage (#406)

This commit is contained in:
Seth Michael Larson 2019-09-29 13:58:22 -05:00 committed by GitHub
parent 5ced56b5b5
commit 05f5dc26de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 107 additions and 16 deletions

View File

@ -54,10 +54,6 @@ class TCPStream(BaseTCPStream):
return "HTTP/1.1"
ident = ssl_object.selected_alpn_protocol()
if ident is None:
return "HTTP/1.1"
return "HTTP/2" if ident == "h2" else "HTTP/1.1"
async def read(

View File

@ -39,9 +39,6 @@ class TCPStream(BaseTCPStream):
return "HTTP/1.1"
ident = self.stream.selected_alpn_protocol()
if ident is None:
return "HTTP/1.1"
return "HTTP/2" if ident == "h2" else "HTTP/1.1"
async def read(
@ -78,7 +75,7 @@ class TCPStream(BaseTCPStream):
return stream.socket.is_readable()
def write_no_block(self, data: bytes) -> None:
self.write_buffer += data
self.write_buffer += data # pragma: no cover
async def write(
self, data: bytes, timeout: TimeoutConfig = None, flag: TimeoutFlag = None

View File

@ -221,7 +221,7 @@ class HTTP2Connection:
else:
try:
self.window_update_received[event_stream_id].set()
except KeyError:
except KeyError: # pragma: no cover
# the window_update_received dictionary is only relevant
# when sending data, which should never raise a KeyError
# here.

View File

@ -41,7 +41,7 @@ from .utils import (
str_query_param,
)
if typing.TYPE_CHECKING:
if typing.TYPE_CHECKING: # pragma: no cover
from .middleware.base import BaseMiddleware # noqa: F401
from .dispatch.base import AsyncDispatcher # noqa: F401

View File

@ -91,9 +91,6 @@ class MockDigestAuthDispatch(AsyncDispatcher):
]
return AsyncResponse(401, headers=headers, content=b"", request=request)
def reset(self) -> None:
self._response_count = 0
def test_basic_auth():
url = "https://example.org/"

View File

@ -30,7 +30,14 @@ def test_proxies_parameter(proxies, expected_proxies):
def test_proxies_has_same_properties_as_dispatch():
client = httpx.AsyncClient(proxies="http://127.0.0.1")
client = httpx.AsyncClient(
proxies="http://127.0.0.1",
verify="/path/to/verify",
cert="/path/to/cert",
trust_env=False,
timeout=30,
http_versions=["HTTP/1.1"],
)
pool = client.dispatch
proxy = client.proxies["all"]
@ -46,3 +53,66 @@ def test_proxies_has_same_properties_as_dispatch():
"backend",
]:
assert getattr(pool, prop) == getattr(proxy, prop)
PROXY_URL = "http://[::1]"
@pytest.mark.parametrize(
["url", "proxies", "expected"],
[
("http://example.com", None, None),
("http://example.com", {}, None),
("http://example.com", {"https": PROXY_URL}, None),
("http://example.com", {"http://example.net": PROXY_URL}, None),
("http://example.com:443", {"http://example.com": PROXY_URL}, None),
("http://example.com", {"all": PROXY_URL}, PROXY_URL),
("http://example.com", {"http": PROXY_URL}, PROXY_URL),
("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),
("http://example.com", {"all://example.com:80": PROXY_URL}, PROXY_URL),
("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),
("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),
("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),
("http://example.com:8080", {"http://example.com": PROXY_URL}, None),
(
"http://example.com",
{
"all": PROXY_URL + ":1",
"http": PROXY_URL + ":2",
"all://example.com": PROXY_URL + ":3",
"http://example.com": PROXY_URL + ":4",
},
PROXY_URL + ":4",
),
(
"http://example.com",
{
"all": PROXY_URL + ":1",
"http": PROXY_URL + ":2",
"all://example.com": PROXY_URL + ":3",
},
PROXY_URL + ":3",
),
(
"http://example.com",
{"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},
PROXY_URL + ":2",
),
],
)
def test_dispatcher_for_request(url, proxies, expected):
client = httpx.AsyncClient(proxies=proxies)
request = httpx.AsyncRequest("GET", url)
dispatcher = client._dispatcher_for_request(request, client.proxies)
if expected is None:
assert isinstance(dispatcher, httpx.ConnectionPool)
assert dispatcher is client.dispatch
else:
assert isinstance(dispatcher, httpx.HTTPProxy)
assert dispatcher.proxy_url == expected
def test_unsupported_proxy_scheme():
with pytest.raises(ValueError):
httpx.AsyncClient(proxies="ftp://127.0.0.1")

View File

@ -37,6 +37,11 @@ def test_client_queryparams_string():
assert isinstance(client.params, QueryParams)
assert client.params["a"] == "b"
client = Client()
client.params = "a=b"
assert isinstance(client.params, QueryParams)
assert client.params["a"] == "b"
def test_client_queryparams_echo():
url = "http://example.org/echo_queryparams"

View File

@ -204,3 +204,11 @@ async def test_http2_settings_in_handshake(backend):
for setting_code, changed_setting in settings.changed_settings.items():
assert isinstance(changed_setting, h2.settings.ChangedSetting)
assert changed_setting.new_value == expected_settings[setting_code]
async def test_http2_live_request(backend):
async with AsyncClient(backend=backend) as client:
resp = await client.get("https://nghttp2.org/httpbin/anything")
assert resp.status_code == 200
assert resp.http_version == "HTTP/2"

View File

@ -166,9 +166,14 @@ async def test_proxy_forwarding(backend, proxy_mode):
backend=backend,
)
async with httpx.HTTPProxy(
proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode=proxy_mode
proxy_url="http://127.0.0.1:8000",
backend=raw_io,
proxy_mode=proxy_mode,
proxy_headers={"Proxy-Authorization": "test", "Override": "2"},
) as proxy:
response = await proxy.request("GET", f"http://example.com")
response = await proxy.request(
"GET", f"http://example.com", headers={"override": "1"}
)
assert response.status_code == 200
assert response.headers["Server"] == "origin-server"
@ -184,6 +189,8 @@ async def test_proxy_forwarding(backend, proxy_mode):
assert recv[1].startswith(
b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"
)
assert b"proxy-authorization: test" in recv[1]
assert b"override: 1" in recv[1]
def test_proxy_url_with_username_and_password():

View File

@ -51,3 +51,14 @@ def test_queryparam_types():
q = QueryParams({"a": 123})
assert str(q) == "a=123"
def test_queryparam_setters():
q = QueryParams({"a": 1})
q.update([])
assert str(q) == "a=1"
q = QueryParams([("a", 1), ("a", 2)])
q["a"] = "3"
assert str(q) == "a=3"