Skip to content

Commit

Permalink
Add an UnsupportedProtocol exception (#128)
Browse files Browse the repository at this point in the history
* Add an UnsupportedProtocol exception

* Update httpcore/_async/connection_pool.py

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

* Update tests/async_tests/test_interfaces.py

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

* Update tests/async_tests/test_interfaces.py

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

* Unasync

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
  • Loading branch information
tomchristie and florimondmanca committed Aug 2, 2020
1 parent 40f9663 commit 4b0ee6f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions httpcore/__init__.py
Expand Up @@ -12,6 +12,7 @@
ReadError,
ReadTimeout,
TimeoutException,
UnsupportedProtocol,
WriteError,
WriteTimeout,
)
Expand All @@ -38,5 +39,6 @@
"ReadError",
"WriteError",
"CloseError",
"UnsupportedProtocol",
]
__version__ = "0.9.1"
7 changes: 5 additions & 2 deletions httpcore/_async/connection_pool.py
Expand Up @@ -2,7 +2,7 @@
from typing import AsyncIterator, Callable, Dict, List, Optional, Set, Tuple

from .._backends.auto import AsyncLock, AsyncSemaphore, AutoBackend
from .._exceptions import PoolTimeout
from .._exceptions import PoolTimeout, UnsupportedProtocol
from .._threadlock import ThreadLock
from .._types import URL, Headers, Origin, TimeoutDict
from .._utils import get_logger, origin_to_url_string, url_to_origin
Expand Down Expand Up @@ -124,7 +124,10 @@ async def request(
stream: AsyncByteStream = None,
timeout: TimeoutDict = None,
) -> Tuple[bytes, int, bytes, Headers, AsyncByteStream]:
assert url[0] in (b"http", b"https")
if url[0] not in (b"http", b"https"):
scheme = url[0].decode("latin-1")
raise UnsupportedProtocol(f"Unsupported URL protocol {scheme!r}")

origin = url_to_origin(url)

if self._keepalive_expiry is not None:
Expand Down
4 changes: 4 additions & 0 deletions httpcore/_exceptions.py
Expand Up @@ -13,6 +13,10 @@ def map_exceptions(map: Dict[Type[Exception], Type[Exception]]) -> Iterator[None
raise


class UnsupportedProtocol(Exception):
pass


class ProtocolError(Exception):
pass

Expand Down
7 changes: 5 additions & 2 deletions httpcore/_sync/connection_pool.py
Expand Up @@ -2,7 +2,7 @@
from typing import Iterator, Callable, Dict, List, Optional, Set, Tuple

from .._backends.auto import SyncLock, SyncSemaphore, SyncBackend
from .._exceptions import PoolTimeout
from .._exceptions import PoolTimeout, UnsupportedProtocol
from .._threadlock import ThreadLock
from .._types import URL, Headers, Origin, TimeoutDict
from .._utils import get_logger, origin_to_url_string, url_to_origin
Expand Down Expand Up @@ -124,7 +124,10 @@ def request(
stream: SyncByteStream = None,
timeout: TimeoutDict = None,
) -> Tuple[bytes, int, bytes, Headers, SyncByteStream]:
assert url[0] in (b"http", b"https")
if url[0] not in (b"http", b"https"):
scheme = url[0].decode("latin-1")
raise UnsupportedProtocol(f"Unsupported URL protocol {scheme!r}")

origin = url_to_origin(url)

if self._keepalive_expiry is not None:
Expand Down
10 changes: 10 additions & 0 deletions tests/async_tests/test_interfaces.py
Expand Up @@ -50,6 +50,16 @@ async def test_https_request() -> None:
assert len(http._connections[url[:3]]) == 1 # type: ignore


@pytest.mark.usefixtures("async_environment")
async def test_request_unsupported_protocol() -> None:
async with httpcore.AsyncConnectionPool() as http:
method = b"GET"
url = (b"ftp", b"example.org", 443, b"/")
headers = [(b"host", b"example.org")]
with pytest.raises(httpcore.UnsupportedProtocol):
await http.request(method, url, headers)


@pytest.mark.usefixtures("async_environment")
async def test_http2_request() -> None:
async with httpcore.AsyncConnectionPool(http2=True) as http:
Expand Down
10 changes: 10 additions & 0 deletions tests/sync_tests/test_interfaces.py
Expand Up @@ -51,6 +51,16 @@ def test_https_request() -> None:



def test_request_unsupported_protocol() -> None:
with httpcore.SyncConnectionPool() as http:
method = b"GET"
url = (b"ftp", b"example.org", 443, b"/")
headers = [(b"host", b"example.org")]
with pytest.raises(httpcore.UnsupportedProtocol):
http.request(method, url, headers)



def test_http2_request() -> None:
with httpcore.SyncConnectionPool(http2=True) as http:
method = b"GET"
Expand Down

0 comments on commit 4b0ee6f

Please sign in to comment.