Skip to content

Commit

Permalink
Add trio tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romis2012 committed Feb 6, 2022
1 parent 5ba3d13 commit 5d26ae4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ target-version = ['py37', 'py38', 'py39']
skip-string-normalization = true
experimental-string-processing = true
verbose = true

[tool.pytest.ini_options]
asyncio_mode = 'strict'
2 changes: 0 additions & 2 deletions pytest.ini

This file was deleted.

72 changes: 43 additions & 29 deletions tests/test_transport_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import httpcore
import httpx
import pytest # noqa
from yarl import URL # noqa
import pytest
from yarl import URL

from httpx_socks import (
ProxyType,
Expand All @@ -13,10 +13,24 @@
ProxyTimeoutError,
)

from httpx_socks._async_proxy import AsyncProxy

from tests.config import (
TEST_HOST_PEM_FILE, TEST_URL_IPV4, TEST_URL_IPV4_HTTPS, SOCKS5_IPV4_URL,
LOGIN, PASSWORD, PROXY_HOST_IPV4, SOCKS5_PROXY_PORT, TEST_URL_IPV4_DELAY,
SKIP_IPV6_TESTS, SOCKS5_IPV6_URL, SOCKS4_URL, HTTP_PROXY_URL, PROXY_HOST_PEM_FILE,
TEST_HOST_PEM_FILE,
TEST_URL_IPV4,
TEST_URL_IPV4_HTTPS,
SOCKS5_IPV4_URL,
LOGIN,
PASSWORD,
PROXY_HOST_IPV4,
SOCKS5_PROXY_PORT,
TEST_URL_IPV4_DELAY,
SKIP_IPV6_TESTS,
SOCKS5_IPV6_URL,
SOCKS4_URL,
HTTP_PROXY_URL,
SOCKS5_IPV4_HOSTNAME_URL,
PROXY_HOST_PEM_FILE,
HTTPS_PROXY_URL,
)

Expand All @@ -35,23 +49,33 @@ def create_ssl_context(url, http2=False):


async def fetch(
transport: AsyncProxyTransport,
url: str,
timeout: httpx.Timeout = None,
transport: AsyncProxyTransport,
url: str,
timeout: httpx.Timeout = None,
):
async with httpx.AsyncClient(transport=transport) as client:
res = await client.get(url=url, timeout=timeout)
return res


@pytest.mark.parametrize('proxy_url', (SOCKS5_IPV4_URL, SOCKS5_IPV4_HOSTNAME_URL, HTTP_PROXY_URL))
@pytest.mark.parametrize('target_url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS))
@pytest.mark.trio
async def test_proxy_direct(proxy_url, target_url):
ssl_context = create_ssl_context(target_url)
async with AsyncProxy.from_url(proxy_url, ssl_context=ssl_context) as proxy:
res = await proxy.request(method="GET", url=target_url)
assert res.status == 200
res = await proxy.request(method="GET", url=target_url)
assert res.status == 200


@pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS))
@pytest.mark.parametrize('rdns', (True, False))
@pytest.mark.trio
async def test_socks5_proxy_ipv4(url, rdns):
transport = AsyncProxyTransport.from_url(
SOCKS5_IPV4_URL,
rdns=rdns,
verify=create_ssl_context(url)
SOCKS5_IPV4_URL, rdns=rdns, verify=create_ssl_context(url)
)
res = await fetch(transport=transport, url=url)
assert res.status_code == 200
Expand All @@ -65,7 +89,7 @@ async def test_socks5_proxy_with_invalid_credentials(url=TEST_URL_IPV4):
proxy_port=SOCKS5_PROXY_PORT,
username=LOGIN,
password=PASSWORD + 'aaa',
verify=create_ssl_context(url)
verify=create_ssl_context(url),
)
with pytest.raises(ProxyError):
await fetch(transport=transport, url=url)
Expand All @@ -79,7 +103,7 @@ async def test_socks5_proxy_with_read_timeout(url=TEST_URL_IPV4_DELAY):
proxy_port=SOCKS5_PROXY_PORT,
username=LOGIN,
password=PASSWORD,
verify=create_ssl_context(url)
verify=create_ssl_context(url),
)
timeout = httpx.Timeout(2, connect=32)
with pytest.raises(httpcore.ReadTimeout):
Expand All @@ -94,23 +118,22 @@ async def test_socks5_proxy_with_connect_timeout(url=TEST_URL_IPV4):
proxy_port=SOCKS5_PROXY_PORT,
username=LOGIN,
password=PASSWORD,
verify=create_ssl_context(url)
verify=create_ssl_context(url),
)
timeout = httpx.Timeout(32, connect=0.001)
with pytest.raises(ProxyTimeoutError):
await fetch(transport=transport, url=url, timeout=timeout)


@pytest.mark.trio
async def test_socks5_proxy_with_invalid_proxy_port(unused_tcp_port,
url=TEST_URL_IPV4):
async def test_socks5_proxy_with_invalid_proxy_port(unused_tcp_port, url=TEST_URL_IPV4):
transport = AsyncProxyTransport(
proxy_type=ProxyType.SOCKS5,
proxy_host=PROXY_HOST_IPV4,
proxy_port=unused_tcp_port,
username=LOGIN,
password=PASSWORD,
verify=create_ssl_context(url)
verify=create_ssl_context(url),
)
with pytest.raises(ProxyConnectionError):
await fetch(transport=transport, url=url)
Expand All @@ -120,10 +143,7 @@ async def test_socks5_proxy_with_invalid_proxy_port(unused_tcp_port,
@pytest.mark.skipif(SKIP_IPV6_TESTS, reason="TravisCI doesn't support ipv6")
@pytest.mark.trio
async def test_socks5_proxy_ipv6(url):
transport = AsyncProxyTransport.from_url(
SOCKS5_IPV6_URL,
verify=create_ssl_context(url)
)
transport = AsyncProxyTransport.from_url(SOCKS5_IPV6_URL, verify=create_ssl_context(url))
res = await fetch(transport=transport, url=url)
assert res.status_code == 200

Expand All @@ -132,21 +152,15 @@ async def test_socks5_proxy_ipv6(url):
@pytest.mark.parametrize('rdns', (True, False))
@pytest.mark.trio
async def test_socks4_proxy(url, rdns):
transport = AsyncProxyTransport.from_url(
SOCKS4_URL, rdns=rdns,
verify=create_ssl_context(url)
)
transport = AsyncProxyTransport.from_url(SOCKS4_URL, rdns=rdns, verify=create_ssl_context(url))
res = await fetch(transport=transport, url=url)
assert res.status_code == 200


@pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS))
@pytest.mark.trio
async def test_http_proxy(url):
transport = AsyncProxyTransport.from_url(
HTTP_PROXY_URL,
verify=create_ssl_context(url)
)
transport = AsyncProxyTransport.from_url(HTTP_PROXY_URL, verify=create_ssl_context(url))
res = await fetch(transport=transport, url=url)
assert res.status_code == 200

Expand Down

0 comments on commit 5d26ae4

Please sign in to comment.