chore: migrating to ruff and updating pre-commit hooks
This commit is contained in:
@@ -17,43 +17,51 @@ class TestStealthyFetcher:
|
||||
def urls(self, httpbin):
|
||||
url = httpbin.url
|
||||
return {
|
||||
'status_200': f'{url}/status/200',
|
||||
'status_404': f'{url}/status/404',
|
||||
'status_501': f'{url}/status/501',
|
||||
'basic_url': f'{url}/get',
|
||||
'html_url': f'{url}/html',
|
||||
'delayed_url': f'{url}/delay/10', # 10 Seconds delay response
|
||||
'cookies_url': f"{url}/cookies/set/test/value"
|
||||
"status_200": f"{url}/status/200",
|
||||
"status_404": f"{url}/status/404",
|
||||
"status_501": f"{url}/status/501",
|
||||
"basic_url": f"{url}/get",
|
||||
"html_url": f"{url}/html",
|
||||
"delayed_url": f"{url}/delay/10", # 10 Seconds delay response
|
||||
"cookies_url": f"{url}/cookies/set/test/value",
|
||||
}
|
||||
|
||||
async def test_basic_fetch(self, fetcher, urls):
|
||||
"""Test doing basic fetch request with multiple statuses"""
|
||||
assert (await fetcher.async_fetch(urls['status_200'])).status == 200
|
||||
assert (await fetcher.async_fetch(urls['status_404'])).status == 404
|
||||
assert (await fetcher.async_fetch(urls['status_501'])).status == 501
|
||||
assert (await fetcher.async_fetch(urls["status_200"])).status == 200
|
||||
assert (await fetcher.async_fetch(urls["status_404"])).status == 404
|
||||
assert (await fetcher.async_fetch(urls["status_501"])).status == 501
|
||||
|
||||
async def test_networkidle(self, fetcher, urls):
|
||||
"""Test if waiting for `networkidle` make page does not finish loading or not"""
|
||||
assert (await fetcher.async_fetch(urls['basic_url'], network_idle=True)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(urls["basic_url"], network_idle=True)
|
||||
).status == 200
|
||||
|
||||
async def test_blocking_resources(self, fetcher, urls):
|
||||
"""Test if blocking resources make page does not finish loading or not"""
|
||||
assert (await fetcher.async_fetch(urls['basic_url'], block_images=True)).status == 200
|
||||
assert (await fetcher.async_fetch(urls['basic_url'], disable_resources=True)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(urls["basic_url"], block_images=True)
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(urls["basic_url"], disable_resources=True)
|
||||
).status == 200
|
||||
|
||||
async def test_waiting_selector(self, fetcher, urls):
|
||||
"""Test if waiting for a selector make page does not finish loading or not"""
|
||||
assert (await fetcher.async_fetch(urls['html_url'], wait_selector='h1')).status == 200
|
||||
assert (await fetcher.async_fetch(
|
||||
urls['html_url'],
|
||||
wait_selector='h1',
|
||||
wait_selector_state='visible'
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(urls["html_url"], wait_selector="h1")
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(
|
||||
urls["html_url"], wait_selector="h1", wait_selector_state="visible"
|
||||
)
|
||||
).status == 200
|
||||
|
||||
async def test_cookies_loading(self, fetcher, urls):
|
||||
"""Test if cookies are set after the request"""
|
||||
response = await fetcher.async_fetch(urls['cookies_url'])
|
||||
assert response.cookies == {'test': 'value'}
|
||||
response = await fetcher.async_fetch(urls["cookies_url"])
|
||||
assert response.cookies == {"test": "value"}
|
||||
|
||||
async def test_automation(self, fetcher, urls):
|
||||
"""Test if automation break the code or not"""
|
||||
@@ -64,34 +72,38 @@ class TestStealthyFetcher:
|
||||
await page.mouse.up()
|
||||
return page
|
||||
|
||||
assert (await fetcher.async_fetch(urls['html_url'], page_action=scroll_page)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(urls["html_url"], page_action=scroll_page)
|
||||
).status == 200
|
||||
|
||||
async def test_properties(self, fetcher, urls):
|
||||
"""Test if different arguments breaks the code or not"""
|
||||
assert (await fetcher.async_fetch(
|
||||
urls['html_url'],
|
||||
block_webrtc=True,
|
||||
allow_webgl=True
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(
|
||||
urls["html_url"], block_webrtc=True, allow_webgl=True
|
||||
)
|
||||
).status == 200
|
||||
|
||||
assert (await fetcher.async_fetch(
|
||||
urls['html_url'],
|
||||
block_webrtc=False,
|
||||
allow_webgl=True
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(
|
||||
urls["html_url"], block_webrtc=False, allow_webgl=True
|
||||
)
|
||||
).status == 200
|
||||
|
||||
assert (await fetcher.async_fetch(
|
||||
urls['html_url'],
|
||||
block_webrtc=True,
|
||||
allow_webgl=False
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(
|
||||
urls["html_url"], block_webrtc=True, allow_webgl=False
|
||||
)
|
||||
).status == 200
|
||||
|
||||
assert (await fetcher.async_fetch(
|
||||
urls['html_url'],
|
||||
extra_headers={'ayo': ''},
|
||||
os_randomize=True
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(
|
||||
urls["html_url"], extra_headers={"ayo": ""}, os_randomize=True
|
||||
)
|
||||
).status == 200
|
||||
|
||||
async def test_infinite_timeout(self, fetcher, urls):
|
||||
"""Test if infinite timeout breaks the code or not"""
|
||||
assert (await fetcher.async_fetch(urls['delayed_url'], timeout=None)).status == 200
|
||||
assert (
|
||||
await fetcher.async_fetch(urls["delayed_url"], timeout=None)
|
||||
).status == 200
|
||||
|
||||
@@ -16,70 +16,111 @@ class TestAsyncFetcher:
|
||||
@pytest.fixture(scope="class")
|
||||
def urls(self, httpbin):
|
||||
return {
|
||||
'status_200': f'{httpbin.url}/status/200',
|
||||
'status_404': f'{httpbin.url}/status/404',
|
||||
'status_501': f'{httpbin.url}/status/501',
|
||||
'basic_url': f'{httpbin.url}/get',
|
||||
'post_url': f'{httpbin.url}/post',
|
||||
'put_url': f'{httpbin.url}/put',
|
||||
'delete_url': f'{httpbin.url}/delete',
|
||||
'html_url': f'{httpbin.url}/html'
|
||||
"status_200": f"{httpbin.url}/status/200",
|
||||
"status_404": f"{httpbin.url}/status/404",
|
||||
"status_501": f"{httpbin.url}/status/501",
|
||||
"basic_url": f"{httpbin.url}/get",
|
||||
"post_url": f"{httpbin.url}/post",
|
||||
"put_url": f"{httpbin.url}/put",
|
||||
"delete_url": f"{httpbin.url}/delete",
|
||||
"html_url": f"{httpbin.url}/html",
|
||||
}
|
||||
|
||||
async def test_basic_get(self, fetcher, urls):
|
||||
"""Test doing basic get request with multiple statuses"""
|
||||
assert (await fetcher.get(urls['status_200'])).status == 200
|
||||
assert (await fetcher.get(urls['status_404'])).status == 404
|
||||
assert (await fetcher.get(urls['status_501'])).status == 501
|
||||
assert (await fetcher.get(urls["status_200"])).status == 200
|
||||
assert (await fetcher.get(urls["status_404"])).status == 404
|
||||
assert (await fetcher.get(urls["status_501"])).status == 501
|
||||
|
||||
async def test_get_properties(self, fetcher, urls):
|
||||
"""Test if different arguments with GET request breaks the code or not"""
|
||||
assert (await fetcher.get(urls['status_200'], stealthy_headers=True)).status == 200
|
||||
assert (await fetcher.get(urls['status_200'], follow_redirects=True)).status == 200
|
||||
assert (await fetcher.get(urls['status_200'], timeout=None)).status == 200
|
||||
assert (await fetcher.get(
|
||||
urls['status_200'],
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.get(urls["status_200"], stealthy_headers=True)
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.get(urls["status_200"], follow_redirects=True)
|
||||
).status == 200
|
||||
assert (await fetcher.get(urls["status_200"], timeout=None)).status == 200
|
||||
assert (
|
||||
await fetcher.get(
|
||||
urls["status_200"],
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None,
|
||||
)
|
||||
).status == 200
|
||||
|
||||
async def test_post_properties(self, fetcher, urls):
|
||||
"""Test if different arguments with POST request breaks the code or not"""
|
||||
assert (await fetcher.post(urls['post_url'], data={'key': 'value'})).status == 200
|
||||
assert (await fetcher.post(urls['post_url'], data={'key': 'value'}, stealthy_headers=True)).status == 200
|
||||
assert (await fetcher.post(urls['post_url'], data={'key': 'value'}, follow_redirects=True)).status == 200
|
||||
assert (await fetcher.post(urls['post_url'], data={'key': 'value'}, timeout=None)).status == 200
|
||||
assert (await fetcher.post(
|
||||
urls['post_url'],
|
||||
data={'key': 'value'},
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.post(urls["post_url"], data={"key": "value"})
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.post(
|
||||
urls["post_url"], data={"key": "value"}, stealthy_headers=True
|
||||
)
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.post(
|
||||
urls["post_url"], data={"key": "value"}, follow_redirects=True
|
||||
)
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.post(urls["post_url"], data={"key": "value"}, timeout=None)
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.post(
|
||||
urls["post_url"],
|
||||
data={"key": "value"},
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None,
|
||||
)
|
||||
).status == 200
|
||||
|
||||
async def test_put_properties(self, fetcher, urls):
|
||||
"""Test if different arguments with PUT request breaks the code or not"""
|
||||
assert (await fetcher.put(urls['put_url'], data={'key': 'value'})).status in [200, 405]
|
||||
assert (await fetcher.put(urls['put_url'], data={'key': 'value'}, stealthy_headers=True)).status in [200, 405]
|
||||
assert (await fetcher.put(urls['put_url'], data={'key': 'value'}, follow_redirects=True)).status in [200, 405]
|
||||
assert (await fetcher.put(urls['put_url'], data={'key': 'value'}, timeout=None)).status in [200, 405]
|
||||
assert (await fetcher.put(
|
||||
urls['put_url'],
|
||||
data={'key': 'value'},
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None
|
||||
)).status in [200, 405]
|
||||
assert (await fetcher.put(urls["put_url"], data={"key": "value"})).status in [
|
||||
200,
|
||||
405,
|
||||
]
|
||||
assert (
|
||||
await fetcher.put(
|
||||
urls["put_url"], data={"key": "value"}, stealthy_headers=True
|
||||
)
|
||||
).status in [200, 405]
|
||||
assert (
|
||||
await fetcher.put(
|
||||
urls["put_url"], data={"key": "value"}, follow_redirects=True
|
||||
)
|
||||
).status in [200, 405]
|
||||
assert (
|
||||
await fetcher.put(urls["put_url"], data={"key": "value"}, timeout=None)
|
||||
).status in [200, 405]
|
||||
assert (
|
||||
await fetcher.put(
|
||||
urls["put_url"],
|
||||
data={"key": "value"},
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None,
|
||||
)
|
||||
).status in [200, 405]
|
||||
|
||||
async def test_delete_properties(self, fetcher, urls):
|
||||
"""Test if different arguments with DELETE request breaks the code or not"""
|
||||
assert (await fetcher.delete(urls['delete_url'], stealthy_headers=True)).status == 200
|
||||
assert (await fetcher.delete(urls['delete_url'], follow_redirects=True)).status == 200
|
||||
assert (await fetcher.delete(urls['delete_url'], timeout=None)).status == 200
|
||||
assert (await fetcher.delete(
|
||||
urls['delete_url'],
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None
|
||||
)).status == 200
|
||||
assert (
|
||||
await fetcher.delete(urls["delete_url"], stealthy_headers=True)
|
||||
).status == 200
|
||||
assert (
|
||||
await fetcher.delete(urls["delete_url"], follow_redirects=True)
|
||||
).status == 200
|
||||
assert (await fetcher.delete(urls["delete_url"], timeout=None)).status == 200
|
||||
assert (
|
||||
await fetcher.delete(
|
||||
urls["delete_url"],
|
||||
stealthy_headers=True,
|
||||
follow_redirects=True,
|
||||
timeout=None,
|
||||
)
|
||||
).status == 200
|
||||
|
||||
@@ -15,87 +15,97 @@ class TestPlayWrightFetcherAsync:
|
||||
@pytest.fixture
|
||||
def urls(self, httpbin):
|
||||
return {
|
||||
'status_200': f'{httpbin.url}/status/200',
|
||||
'status_404': f'{httpbin.url}/status/404',
|
||||
'status_501': f'{httpbin.url}/status/501',
|
||||
'basic_url': f'{httpbin.url}/get',
|
||||
'html_url': f'{httpbin.url}/html',
|
||||
'delayed_url': f'{httpbin.url}/delay/10',
|
||||
'cookies_url': f"{httpbin.url}/cookies/set/test/value"
|
||||
"status_200": f"{httpbin.url}/status/200",
|
||||
"status_404": f"{httpbin.url}/status/404",
|
||||
"status_501": f"{httpbin.url}/status/501",
|
||||
"basic_url": f"{httpbin.url}/get",
|
||||
"html_url": f"{httpbin.url}/html",
|
||||
"delayed_url": f"{httpbin.url}/delay/10",
|
||||
"cookies_url": f"{httpbin.url}/cookies/set/test/value",
|
||||
}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_basic_fetch(self, fetcher, urls):
|
||||
"""Test doing basic fetch request with multiple statuses"""
|
||||
response = await fetcher.async_fetch(urls['status_200'])
|
||||
response = await fetcher.async_fetch(urls["status_200"])
|
||||
assert response.status == 200
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_networkidle(self, fetcher, urls):
|
||||
"""Test if waiting for `networkidle` make page does not finish loading or not"""
|
||||
response = await fetcher.async_fetch(urls['basic_url'], network_idle=True)
|
||||
response = await fetcher.async_fetch(urls["basic_url"], network_idle=True)
|
||||
assert response.status == 200
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_blocking_resources(self, fetcher, urls):
|
||||
"""Test if blocking resources make page does not finish loading or not"""
|
||||
response = await fetcher.async_fetch(urls['basic_url'], disable_resources=True)
|
||||
response = await fetcher.async_fetch(urls["basic_url"], disable_resources=True)
|
||||
assert response.status == 200
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_waiting_selector(self, fetcher, urls):
|
||||
"""Test if waiting for a selector make page does not finish loading or not"""
|
||||
response1 = await fetcher.async_fetch(urls['html_url'], wait_selector='h1')
|
||||
response1 = await fetcher.async_fetch(urls["html_url"], wait_selector="h1")
|
||||
assert response1.status == 200
|
||||
|
||||
response2 = await fetcher.async_fetch(urls['html_url'], wait_selector='h1', wait_selector_state='visible')
|
||||
response2 = await fetcher.async_fetch(
|
||||
urls["html_url"], wait_selector="h1", wait_selector_state="visible"
|
||||
)
|
||||
assert response2.status == 200
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cookies_loading(self, fetcher, urls):
|
||||
"""Test if cookies are set after the request"""
|
||||
response = await fetcher.async_fetch(urls['cookies_url'])
|
||||
assert response.cookies == {'test': 'value'}
|
||||
response = await fetcher.async_fetch(urls["cookies_url"])
|
||||
assert response.cookies == {"test": "value"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_automation(self, fetcher, urls):
|
||||
"""Test if automation break the code or not"""
|
||||
|
||||
async def scroll_page(page):
|
||||
await page.mouse.wheel(10, 0)
|
||||
await page.mouse.move(100, 400)
|
||||
await page.mouse.up()
|
||||
return page
|
||||
|
||||
response = await fetcher.async_fetch(urls['html_url'], page_action=scroll_page)
|
||||
response = await fetcher.async_fetch(urls["html_url"], page_action=scroll_page)
|
||||
assert response.status == 200
|
||||
|
||||
@pytest.mark.parametrize("kwargs", [
|
||||
{"disable_webgl": True, "hide_canvas": False},
|
||||
{"disable_webgl": False, "hide_canvas": True},
|
||||
# {"stealth": True}, # causes issues with Github Actions
|
||||
{"useragent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0'},
|
||||
{"extra_headers": {'ayo': ''}}
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs",
|
||||
[
|
||||
{"disable_webgl": True, "hide_canvas": False},
|
||||
{"disable_webgl": False, "hide_canvas": True},
|
||||
# {"stealth": True}, # causes issues with Github Actions
|
||||
{
|
||||
"useragent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0"
|
||||
},
|
||||
{"extra_headers": {"ayo": ""}},
|
||||
],
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_properties(self, fetcher, urls, kwargs):
|
||||
"""Test if different arguments breaks the code or not"""
|
||||
response = await fetcher.async_fetch(urls['html_url'], **kwargs)
|
||||
response = await fetcher.async_fetch(urls["html_url"], **kwargs)
|
||||
assert response.status == 200
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cdp_url_invalid(self, fetcher, urls):
|
||||
"""Test if invalid CDP URLs raise appropriate exceptions"""
|
||||
with pytest.raises(ValueError):
|
||||
await fetcher.async_fetch(urls['html_url'], cdp_url='blahblah')
|
||||
await fetcher.async_fetch(urls["html_url"], cdp_url="blahblah")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
await fetcher.async_fetch(urls['html_url'], cdp_url='blahblah', nstbrowser_mode=True)
|
||||
await fetcher.async_fetch(
|
||||
urls["html_url"], cdp_url="blahblah", nstbrowser_mode=True
|
||||
)
|
||||
|
||||
with pytest.raises(Exception):
|
||||
await fetcher.async_fetch(urls['html_url'], cdp_url='ws://blahblah')
|
||||
await fetcher.async_fetch(urls["html_url"], cdp_url="ws://blahblah")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_infinite_timeout(self, fetcher, urls):
|
||||
"""Test if infinite timeout breaks the code or not"""
|
||||
response = await fetcher.async_fetch(urls['delayed_url'], timeout=None)
|
||||
response = await fetcher.async_fetch(urls["delayed_url"], timeout=None)
|
||||
assert response.status == 200
|
||||
|
||||
Reference in New Issue
Block a user