Skip to content

Commit

Permalink
bump httpx dependency version to 0.11.1 (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
syfluqs authored Mar 1, 2020
1 parent 7833d70 commit ce71514
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
- sphinx==1.8.3
- sphinx_rtd_theme==0.4.2
- recommonmark==0.5.0
- httpx==0.9.3
- httpx==0.11.1
- sphinxcontrib-asyncio>=0.2.0
- docutils==0.14
- pygments==2.3.1
18 changes: 10 additions & 8 deletions sanic/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, app, port=PORT, host=HOST):
self.host = host

def get_new_session(self):
return httpx.Client()
return httpx.AsyncClient(verify=False)

async def _local_request(self, method, url, *args, **kwargs):
logger.info(url)
Expand All @@ -38,20 +38,22 @@ async def _local_request(self, method, url, *args, **kwargs):

try:
response = await getattr(session, method.lower())(
url, verify=False, *args, **kwargs
url, *args, **kwargs
)
except NameError:
raise Exception(response.status_code)

response.body = await response.aread()
response.status = response.status_code
response.content_type = response.headers.get("content-type")

# response can be decoded as json after response._content
# is set by response.aread()
try:
response.json = response.json()
except (JSONDecodeError, UnicodeDecodeError):
response.json = None

response.body = await response.read()
response.status = response.status_code
response.content_type = response.headers.get("content-type")

if raw_cookies:
response.raw_cookies = {}

Expand Down Expand Up @@ -185,11 +187,11 @@ async def app_call_with_return(self, scope, receive, send):
return await asgi_app()


class SanicASGIDispatch(httpx.dispatch.ASGIDispatch):
class SanicASGIDispatch(httpx.dispatch.asgi.ASGIDispatch):
pass


class SanicASGITestClient(httpx.Client):
class SanicASGITestClient(httpx.AsyncClient):
def __init__(
self,
app,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def open_local(paths, mode="r", encoding="utf8"):
"aiofiles>=0.3.0",
"websockets>=7.0,<9.0",
"multidict>=4.0,<5.0",
"httpx==0.9.3",
"httpx==0.11.1",
]

tests_require = [
Expand Down
35 changes: 27 additions & 8 deletions tests/test_keep_alive_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
class ReusableSanicConnectionPool(
httpx.dispatch.connection_pool.ConnectionPool
):
@property
def cert(self):
return self.ssl.cert

@property
def verify(self):
return self.ssl.verify

@property
def trust_env(self):
return self.ssl.trust_env

@property
def http2(self):
return self.ssl.http2

async def acquire_connection(self, origin, timeout):
global old_conn
connection = self.pop_connection(origin)
Expand All @@ -26,14 +42,17 @@ async def acquire_connection(self, origin, timeout):
pool_timeout = None if timeout is None else timeout.pool_timeout

await self.max_connections.acquire(timeout=pool_timeout)
ssl_config = httpx.config.SSLConfig(
cert=self.cert,
verify=self.verify,
trust_env=self.trust_env,
http2=self.http2
)
connection = httpx.dispatch.connection.HTTPConnection(
origin,
verify=self.verify,
cert=self.cert,
http2=self.http2,
ssl=ssl_config,
backend=self.backend,
release_func=self.release_connection,
trust_env=self.trust_env,
uds=self.uds,
)

Expand All @@ -49,7 +68,7 @@ async def acquire_connection(self, origin, timeout):
return connection


class ResusableSanicSession(httpx.Client):
class ResusableSanicSession(httpx.AsyncClient):
def __init__(self, *args, **kwargs) -> None:
dispatch = ReusableSanicConnectionPool()
super().__init__(dispatch=dispatch, *args, **kwargs)
Expand Down Expand Up @@ -159,7 +178,7 @@ def kill_server(self):
self._server = None

if self._session:
self._loop.run_until_complete(self._session.close())
self._loop.run_until_complete(self._session.aclose())
self._session = None

except Exception as e3:
Expand All @@ -178,7 +197,7 @@ async def _local_request(self, method, url, *args, **kwargs):
self._session = self.get_new_session()
try:
response = await getattr(self._session, method.lower())(
url, verify=False, timeout=request_keepalive, *args, **kwargs
url, timeout=request_keepalive, *args, **kwargs
)
except NameError:
raise Exception(response.status_code)
Expand All @@ -188,7 +207,7 @@ async def _local_request(self, method, url, *args, **kwargs):
except (JSONDecodeError, UnicodeDecodeError):
response.json = None

response.body = await response.read()
response.body = await response.aread()
response.status = response.status_code
response.content_type = response.headers.get("content-type")

Expand Down
20 changes: 7 additions & 13 deletions tests/test_request_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ def __init__(self, *args, **kwargs):
self._request_delay = kwargs.pop("request_delay")
super().__init__(*args, **kwargs)

async def send(self, request, verify=None, cert=None, timeout=None):
if self.h11_connection is None and self.h2_connection is None:
await self.connect(verify=verify, cert=cert, timeout=timeout)
async def send(self, request, timeout=None):

if self.connection is None:
self.connection = (await self.connect(timeout=timeout))

if self._request_delay:
await asyncio.sleep(self._request_delay)

if self.h2_connection is not None:
response = await self.h2_connection.send(request, timeout=timeout)
else:
assert self.h11_connection is not None
response = await self.h11_connection.send(request, timeout=timeout)
response = await self.connection.send(request, timeout=timeout)

return response

Expand All @@ -46,12 +43,9 @@ async def acquire_connection(self, origin, timeout=None):
await self.max_connections.acquire(timeout=pool_timeout)
connection = DelayableHTTPConnection(
origin,
verify=self.verify,
cert=self.cert,
http2=self.http2,
ssl=self.ssl,
backend=self.backend,
release_func=self.release_connection,
trust_env=self.trust_env,
uds=self.uds,
request_delay=self._request_delay,
)
Expand All @@ -61,7 +55,7 @@ async def acquire_connection(self, origin, timeout=None):
return connection


class DelayableSanicSession(httpx.Client):
class DelayableSanicSession(httpx.AsyncClient):
def __init__(self, request_delay=None, *args, **kwargs) -> None:
dispatch = DelayableSanicConnectionPool(request_delay=request_delay)
super().__init__(dispatch=dispatch, *args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ deps =
pytest-sanic
pytest-sugar
httpcore==0.3.0
httpx==0.9.3
httpx==0.11.1
chardet<=2.3.0
beautifulsoup4
gunicorn
Expand Down

0 comments on commit ce71514

Please sign in to comment.