Skip to content

Commit

Permalink
Replace remaining occurrences of dispatch with transport (#1010)
Browse files Browse the repository at this point in the history
* Replace remaining occurrences of dispatch with transport

* Remove unused AsyncDispatcher

Was removed in #804

* Remove hard_limit warning in test
  • Loading branch information
jcugat authored May 30, 2020
1 parent 8ed6904 commit 89a8100
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 80 deletions.
3 changes: 0 additions & 3 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@
warn_deprecated,
)

if typing.TYPE_CHECKING: # pragma: no cover
from ._dispatch.base import AsyncDispatcher # noqa: F401


class URL:
def __init__(
Expand Down
6 changes: 3 additions & 3 deletions httpx/_transports/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ class ASGITransport(httpcore.AsyncHTTPTransport):
client = httpx.AsyncClient(app=app)
```
Alternatively, you can setup the dispatch instance explicitly.
Alternatively, you can setup the transport instance explicitly.
This allows you to include any additional configuration arguments specific
to the ASGITransport class:
```
dispatch = httpx.ASGITransport(
transport = httpx.ASGITransport(
app=app,
root_path="/submount",
client=("1.2.3.4", 123)
)
client = httpx.AsyncClient(dispatch=dispatch)
client = httpx.AsyncClient(transport=transport)
```
Arguments:
Expand Down
6 changes: 3 additions & 3 deletions httpx/_transports/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class WSGITransport(httpcore.SyncHTTPTransport):
client = httpx.Client(app=app)
```
Alternatively, you can setup the dispatch instance explicitly.
Alternatively, you can setup the transport instance explicitly.
This allows you to include any additional configuration arguments specific
to the WSGITransport class:
```
dispatch = httpx.WSGITransport(
transport = httpx.WSGITransport(
app=app,
script_name="/submount",
remote_addr="1.2.3.4"
)
client = httpx.Client(dispatch=dispatch)
client = httpx.Client(transport=transport)
```
Arguments:
Expand Down
52 changes: 27 additions & 25 deletions tests/client/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_header_value(headers, key, default=None):
return default


class MockDispatch(httpcore.AsyncHTTPTransport):
class MockTransport(httpcore.AsyncHTTPTransport):
def __init__(self, auth_header: bytes = b"", status_code: int = 200) -> None:
self.auth_header = auth_header
self.status_code = status_code
Expand All @@ -50,7 +50,7 @@ async def request(
return b"HTTP/1.1", self.status_code, b"", response_headers, response_stream


class MockDigestAuthDispatch(httpcore.AsyncHTTPTransport):
class MockDigestAuthTransport(httpcore.AsyncHTTPTransport):
def __init__(
self,
algorithm: str = "SHA-256",
Expand Down Expand Up @@ -119,7 +119,7 @@ async def test_basic_auth() -> None:
url = "https://example.org/"
auth = ("tomchristie", "password123")

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, auth=auth)

assert response.status_code == 200
Expand All @@ -130,7 +130,7 @@ async def test_basic_auth() -> None:
async def test_basic_auth_in_url() -> None:
url = "https://tomchristie:password123@example.org/"

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url)

assert response.status_code == 200
Expand All @@ -142,7 +142,7 @@ async def test_basic_auth_on_session() -> None:
url = "https://example.org/"
auth = ("tomchristie", "password123")

client = AsyncClient(dispatch=MockDispatch(), auth=auth)
client = AsyncClient(transport=MockTransport(), auth=auth)
response = await client.get(url)

assert response.status_code == 200
Expand All @@ -157,7 +157,7 @@ def auth(request: Request) -> Request:
request.headers["Authorization"] = "Token 123"
return request

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, auth=auth)

assert response.status_code == 200
Expand All @@ -169,7 +169,7 @@ async def test_netrc_auth() -> None:
os.environ["NETRC"] = "tests/.netrc"
url = "http://netrcexample.org"

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url)

assert response.status_code == 200
Expand All @@ -183,7 +183,7 @@ async def test_auth_header_has_priority_over_netrc() -> None:
os.environ["NETRC"] = "tests/.netrc"
url = "http://netrcexample.org"

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, headers={"Authorization": "Override"})

assert response.status_code == 200
Expand All @@ -195,13 +195,13 @@ async def test_trust_env_auth() -> None:
os.environ["NETRC"] = "tests/.netrc"
url = "http://netrcexample.org"

client = AsyncClient(dispatch=MockDispatch(), trust_env=False)
client = AsyncClient(transport=MockTransport(), trust_env=False)
response = await client.get(url)

assert response.status_code == 200
assert response.json() == {"auth": None}

client = AsyncClient(dispatch=MockDispatch(), trust_env=True)
client = AsyncClient(transport=MockTransport(), trust_env=True)
response = await client.get(url)

assert response.status_code == 200
Expand All @@ -222,7 +222,7 @@ async def test_auth_hidden_header() -> None:
url = "https://example.org/"
auth = ("example-username", "example-password")

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, auth=auth)

assert "'authorization': '[secure]'" in str(response.request.headers)
Expand All @@ -232,7 +232,7 @@ async def test_auth_hidden_header() -> None:
async def test_auth_invalid_type() -> None:
url = "https://example.org/"
client = AsyncClient(
dispatch=MockDispatch(), auth="not a tuple, not a callable", # type: ignore
transport=MockTransport(), auth="not a tuple, not a callable", # type: ignore
)
with pytest.raises(TypeError):
await client.get(url)
Expand All @@ -243,7 +243,7 @@ async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() ->
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, auth=auth)

assert response.status_code == 200
Expand All @@ -258,7 +258,7 @@ async def test_digest_auth_200_response_including_digest_auth_header() -> None:
auth_header = b'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'

client = AsyncClient(
dispatch=MockDispatch(auth_header=auth_header, status_code=200)
transport=MockTransport(auth_header=auth_header, status_code=200)
)
response = await client.get(url, auth=auth)

Expand All @@ -272,7 +272,7 @@ async def test_digest_auth_401_response_without_digest_auth_header() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")

client = AsyncClient(dispatch=MockDispatch(auth_header=b"", status_code=401))
client = AsyncClient(transport=MockTransport(auth_header=b"", status_code=401))
response = await client.get(url, auth=auth)

assert response.status_code == 401
Expand Down Expand Up @@ -300,7 +300,7 @@ async def test_digest_auth(
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")

client = AsyncClient(dispatch=MockDigestAuthDispatch(algorithm=algorithm))
client = AsyncClient(transport=MockDigestAuthTransport(algorithm=algorithm))
response = await client.get(url, auth=auth)

assert response.status_code == 200
Expand Down Expand Up @@ -330,7 +330,7 @@ async def test_digest_auth_no_specified_qop() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")

client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=""))
client = AsyncClient(transport=MockDigestAuthTransport(qop=""))
response = await client.get(url, auth=auth)

assert response.status_code == 200
Expand Down Expand Up @@ -361,7 +361,7 @@ async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str)
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")

client = AsyncClient(dispatch=MockDigestAuthDispatch(qop=qop))
client = AsyncClient(transport=MockDigestAuthTransport(qop=qop))
response = await client.get(url, auth=auth)

assert response.status_code == 200
Expand All @@ -372,7 +372,7 @@ async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str)
async def test_digest_auth_qop_auth_int_not_implemented() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="auth-int"))
client = AsyncClient(transport=MockDigestAuthTransport(qop="auth-int"))

with pytest.raises(NotImplementedError):
await client.get(url, auth=auth)
Expand All @@ -382,7 +382,7 @@ async def test_digest_auth_qop_auth_int_not_implemented() -> None:
async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
client = AsyncClient(dispatch=MockDigestAuthDispatch(qop="not-auth"))
client = AsyncClient(transport=MockDigestAuthTransport(qop="not-auth"))

with pytest.raises(ProtocolError):
await client.get(url, auth=auth)
Expand All @@ -393,7 +393,9 @@ async def test_digest_auth_incorrect_credentials() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")

client = AsyncClient(dispatch=MockDigestAuthDispatch(send_response_after_attempt=2))
client = AsyncClient(
transport=MockDigestAuthTransport(send_response_after_attempt=2)
)
response = await client.get(url, auth=auth)

assert response.status_code == 401
Expand All @@ -417,7 +419,7 @@ async def test_digest_auth_raises_protocol_error_on_malformed_header(
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
client = AsyncClient(
dispatch=MockDispatch(auth_header=auth_header, status_code=401)
transport=MockTransport(auth_header=auth_header, status_code=401)
)

with pytest.raises(ProtocolError):
Expand Down Expand Up @@ -460,7 +462,7 @@ def auth_flow(

url = "https://example.org/"
auth = RepeatAuth(repeat=2)
client = AsyncClient(dispatch=MockDispatch(auth_header=b"abc"))
client = AsyncClient(transport=MockTransport(auth_header=b"abc"))

response = await client.get(url, auth=auth)
assert response.status_code == 200
Expand All @@ -481,7 +483,7 @@ def auth_flow(
async def test_digest_auth_unavailable_streaming_body():
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())

async def streaming_body():
yield b"Example request body" # pragma: nocover
Expand Down Expand Up @@ -520,7 +522,7 @@ def auth_flow(

url = "https://example.org/"
auth = ResponseBodyAuth("xyz")
client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())

response = await client.get(url, auth=auth)
assert response.status_code == 200
Expand Down
14 changes: 7 additions & 7 deletions tests/client/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_header_value(headers, key, default=None):
return default


class MockDispatch(httpcore.AsyncHTTPTransport):
class MockTransport(httpcore.AsyncHTTPTransport):
async def request(
self,
method: bytes,
Expand Down Expand Up @@ -48,7 +48,7 @@ async def test_set_cookie() -> None:
url = "http://example.org/echo_cookies"
cookies = {"example-name": "example-value"}

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, cookies=cookies)

assert response.status_code == 200
Expand Down Expand Up @@ -84,7 +84,7 @@ async def test_set_cookie_with_cookiejar() -> None:
)
cookies.set_cookie(cookie)

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, cookies=cookies)

assert response.status_code == 200
Expand Down Expand Up @@ -120,7 +120,7 @@ async def test_setting_client_cookies_to_cookiejar() -> None:
)
cookies.set_cookie(cookie)

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
client.cookies = cookies # type: ignore
response = await client.get(url)

Expand All @@ -138,7 +138,7 @@ async def test_set_cookie_with_cookies_model() -> None:
cookies = Cookies()
cookies["example-name"] = "example-value"

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url, cookies=cookies)

assert response.status_code == 200
Expand All @@ -149,7 +149,7 @@ async def test_set_cookie_with_cookies_model() -> None:
async def test_get_cookie() -> None:
url = "http://example.org/set_cookie"

client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())
response = await client.get(url)

assert response.status_code == 200
Expand All @@ -162,7 +162,7 @@ async def test_cookie_persistence() -> None:
"""
Ensure that Client instances persist cookies between requests.
"""
client = AsyncClient(dispatch=MockDispatch())
client = AsyncClient(transport=MockTransport())

response = await client.get("http://example.org/echo_cookies")
assert response.status_code == 200
Expand Down
Loading

0 comments on commit 89a8100

Please sign in to comment.