Get test suite back to ~100% line coverage (#406)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Seth Michael Larson · committed by GitHub · parent 5ced56b · Browse files · 10 files·+107 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+107 added−16 removed
- tests:+104 added−6 removed
- other:+3 added−10 removed
Provenance
- Commit
05f5dc2- Tree
c6bb4a3- Author
Seth Michael Larson <sethmichaellarson@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
5ced56b
10 files changed+107 added−16 removed
modified:httpx/concurrency/asyncio/backend.py-4 removed
52 unmodified lines53 if ssl_object is None:54 return "HTTP/1.1"55 56 ident = ssl_object.selected_alpn_protocol()57-58- if ident is None:59- return "HTTP/1.1"60-57 return "HTTP/2" if ident == "h2" else "HTTP/1.1"58 59 async def read(60 self, n: int, timeout: TimeoutConfig = None, flag: TimeoutFlag = None207 unmodified linesmodified:httpx/concurrency/trio.py+1 added-4 removed
37 unmodified lines38 if not isinstance(self.stream, trio.SSLStream):39 return "HTTP/1.1"40 41 ident = self.stream.selected_alpn_protocol()42- if ident is None:43- return "HTTP/1.1"44-42 return "HTTP/2" if ident == "h2" else "HTTP/1.1"43 44 async def read(45 self, n: int, timeout: TimeoutConfig = None, flag: TimeoutFlag = None28 unmodified lines74 # See: https://github.com/encode/httpx/pull/143#issuecomment-51518177875 return stream.socket.is_readable()76 77 def write_no_block(self, data: bytes) -> None:81- self.write_buffer += data78+ self.write_buffer += data # pragma: no cover79 80 async def write(81 self, data: bytes, timeout: TimeoutConfig = None, flag: TimeoutFlag = None82 ) -> None:170 unmodified linesmodified:httpx/dispatch/http2.py+1 added-1 removed
219 unmodified lines220 window_update_event.set()221 else:222 try:223 self.window_update_received[event_stream_id].set()224- except KeyError:224+ except KeyError: # pragma: no cover225 # the window_update_received dictionary is only relevant226 # when sending data, which should never raise a KeyError227 # here.228 pass23 unmodified linesmodified:httpx/models.py+1 added-1 removed
39 unmodified lines40 parse_header_links,41 str_query_param,42 )43 44-if typing.TYPE_CHECKING:44+if typing.TYPE_CHECKING: # pragma: no cover45 from .middleware.base import BaseMiddleware # noqa: F40146 from .dispatch.base import AsyncDispatcher # noqa: F40147 48 PrimitiveData = typing.Optional[typing.Union[str, int, float, bool]]1244 unmodified linesmodified:tests/client/test_auth.py-3 removed
89 unmodified lines90 ("www-authenticate", 'Digest realm="httpx@example.org", ' + challenge_str)91 ]92 return AsyncResponse(401, headers=headers, content=b"", request=request)93 94- def reset(self) -> None:95- self._response_count = 094 97-95 def test_basic_auth():96 url = "https://example.org/"97 auth = ("tomchristie", "password123")98 257 unmodified linesmodified:tests/client/test_proxies.py+71 added-1 removed
28 unmodified lines29 assert len(expected_proxies) == len(client.proxies)30 31 32 def test_proxies_has_same_properties_as_dispatch():33- client = httpx.AsyncClient(proxies="http://127.0.0.1")33+ client = httpx.AsyncClient(34+ proxies="http://127.0.0.1",35+ verify="/path/to/verify",36+ cert="/path/to/cert",37+ trust_env=False,38+ timeout=30,39+ http_versions=["HTTP/1.1"],40+ )41 pool = client.dispatch42 proxy = client.proxies["all"]43 44 assert isinstance(pool, httpx.ConnectionPool)7 unmodified lines52 "http_versions",53 "backend",54 ]:55 assert getattr(pool, prop) == getattr(proxy, prop)56+57+58+PROXY_URL = "http://[::1]"59+60+61+@pytest.mark.parametrize(62+ ["url", "proxies", "expected"],63+ [64+ ("http://example.com", None, None),65+ ("http://example.com", {}, None),66+ ("http://example.com", {"https": PROXY_URL}, None),67+ ("http://example.com", {"http://example.net": PROXY_URL}, None),68+ ("http://example.com:443", {"http://example.com": PROXY_URL}, None),69+ ("http://example.com", {"all": PROXY_URL}, PROXY_URL),70+ ("http://example.com", {"http": PROXY_URL}, PROXY_URL),71+ ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),72+ ("http://example.com", {"all://example.com:80": PROXY_URL}, PROXY_URL),73+ ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),74+ ("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),75+ ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),76+ ("http://example.com:8080", {"http://example.com": PROXY_URL}, None),77+ (78+ "http://example.com",79+ {80+ "all": PROXY_URL + ":1",81+ "http": PROXY_URL + ":2",82+ "all://example.com": PROXY_URL + ":3",83+ "http://example.com": PROXY_URL + ":4",84+ },85+ PROXY_URL + ":4",86+ ),87+ (88+ "http://example.com",89+ {90+ "all": PROXY_URL + ":1",91+ "http": PROXY_URL + ":2",92+ "all://example.com": PROXY_URL + ":3",93+ },94+ PROXY_URL + ":3",95+ ),96+ (97+ "http://example.com",98+ {"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},99+ PROXY_URL + ":2",100+ ),101+ ],102+)103+def test_dispatcher_for_request(url, proxies, expected):104+ client = httpx.AsyncClient(proxies=proxies)105+ request = httpx.AsyncRequest("GET", url)106+ dispatcher = client._dispatcher_for_request(request, client.proxies)107+108+ if expected is None:109+ assert isinstance(dispatcher, httpx.ConnectionPool)110+ assert dispatcher is client.dispatch111+ else:112+ assert isinstance(dispatcher, httpx.HTTPProxy)113+ assert dispatcher.proxy_url == expected114+115+116+def test_unsupported_proxy_scheme():117+ with pytest.raises(ValueError):118+ httpx.AsyncClient(proxies="ftp://127.0.0.1")modified:tests/client/test_queryparams.py+5 added
35 unmodified lines36 client = Client(params="a=b")37 assert isinstance(client.params, QueryParams)38 assert client.params["a"] == "b"39 40+ client = Client()41+ client.params = "a=b"42+ assert isinstance(client.params, QueryParams)43+ assert client.params["a"] == "b"44 45+46 def test_client_queryparams_echo():47 url = "http://example.org/echo_queryparams"48 client_queryparams = "first=str"49 request_queryparams = {"second": "dict"}7 unmodified linesmodified:tests/dispatch/test_http2.py+8 added
202 unmodified lines203 assert len(settings.changed_settings) == len(expected_settings)204 for setting_code, changed_setting in settings.changed_settings.items():205 assert isinstance(changed_setting, h2.settings.ChangedSetting)206 assert changed_setting.new_value == expected_settings[setting_code]207+208+209+async def test_http2_live_request(backend):210+ async with AsyncClient(backend=backend) as client:211+ resp = await client.get("https://nghttp2.org/httpbin/anything")212+213+ assert resp.status_code == 200214+ assert resp.http_version == "HTTP/2"modified:tests/dispatch/test_proxy_http.py+9 added-2 removed
164 unmodified lines165 ),166 backend=backend,167 )168 async with httpx.HTTPProxy(169- proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode=proxy_mode169+ proxy_url="http://127.0.0.1:8000",170+ backend=raw_io,171+ proxy_mode=proxy_mode,172+ proxy_headers={"Proxy-Authorization": "test", "Override": "2"},173 ) as proxy:171- response = await proxy.request("GET", f"http://example.com")174+ response = await proxy.request(175+ "GET", f"http://example.com", headers={"override": "1"}176+ )177 178 assert response.status_code == 200179 assert response.headers["Server"] == "origin-server"180 7 unmodified lines188 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"189 assert recv[1].startswith(190 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"191 )192+ assert b"proxy-authorization: test" in recv[1]193+ assert b"override: 1" in recv[1]194 195 196 def test_proxy_url_with_username_and_password():197 proxy = httpx.HTTPProxy("http://user:password@example.com:1080")17 unmodified linesmodified:tests/models/test_queryparams.py+11 added
49 unmodified lines50 assert str(q) == "a=1.23"51 52 q = QueryParams({"a": 123})53 assert str(q) == "a=123"54+55+56+def test_queryparam_setters():57+ q = QueryParams({"a": 1})58+ q.update([])59+60+ assert str(q) == "a=1"61+62+ q = QueryParams([("a", 1), ("a", 2)])63+ q["a"] = "3"64+ assert str(q) == "a=3"