Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/browsing_context/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ async def test_browsing_context_download_finished_complete(
@pytest.mark.asyncio
async def test_browsing_context_download_finished_canceled(
websocket, test_headless_mode, url_hang_forever_download, context_id,
html, get_cdp_session_id, filename):
html, get_cdp_session_id, filename, some_host):

page_url = html(
f"""<a id="download_link" href="{url_hang_forever_download()}" download="{filename}">Download</a>"""
f"""<a id="download_link" href="{url_hang_forever_download}" download="{filename}">Download</a>"""
)
await goto_url(websocket, context_id, page_url)

Expand Down Expand Up @@ -215,7 +215,7 @@ async def test_browsing_context_download_finished_canceled(
'navigation': navigation_id,
'status': 'canceled',
'timestamp': ANY_TIMESTAMP,
'url': url_hang_forever_download(),
'url': url_hang_forever_download,
},
'type': 'event',
}
10 changes: 6 additions & 4 deletions tests/browsing_context/test_navigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,8 @@ async def test_speculationrules_disable_prerender(websocket, context_id, html):

@pytest.mark.asyncio
async def test_browsing_context_navigate_ssl_bad(websocket, context_id,
local_server_bad_ssl):
local_server_bad_ssl,
some_host):
with pytest.raises(Exception,
match=str({
"error": "unknown error",
Expand All @@ -927,7 +928,7 @@ async def test_browsing_context_navigate_ssl_bad(websocket, context_id,
websocket, {
'method': "browsingContext.navigate",
'params': {
'url': local_server_bad_ssl.url_200(),
'url': local_server_bad_ssl.url_200(host=some_host),
'wait': 'complete',
'context': context_id
}
Expand All @@ -936,12 +937,13 @@ async def test_browsing_context_navigate_ssl_bad(websocket, context_id,

@pytest.mark.asyncio
async def test_browsing_context_navigate_ssl_good(websocket, context_id,
local_server_good_ssl):
local_server_good_ssl,
some_host):
await execute_command(
websocket, {
'method': "browsingContext.navigate",
'params': {
'url': local_server_good_ssl.url_200(),
'url': local_server_good_ssl.url_200(host=some_host),
'wait': 'complete',
'context': context_id
}
Expand Down
75 changes: 36 additions & 39 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,21 @@


@pytest_asyncio.fixture(scope='session')
def local_server_http() -> Generator[LocalHttpServer, None, None]:
"""
Returns an instance of a LocalHttpServer without SSL pointing to localhost.
"""
server = LocalHttpServer()
yield server
def some_host() -> str:
return "localhost"

server.clear()
if server.is_running():
server.stop()
return

@pytest_asyncio.fixture(scope='session')
def another_host() -> str:
return "another_host.test"


@pytest_asyncio.fixture(scope='session')
def local_server_http_another_host() -> Generator[LocalHttpServer, None, None]:
def local_server_http() -> Generator[LocalHttpServer, None, None]:
"""
Returns an instance of a LocalHttpServer without SSL pointing to `127.0.0.1`
Returns an instance of a LocalHttpServer without SSL pointing to localhost.
"""
server = LocalHttpServer('127.0.0.1')
server = LocalHttpServer()
yield server

server.clear()
Expand Down Expand Up @@ -138,7 +134,9 @@ async def create_session(connection):
# Required to prevent automatic switch to https.
"--disable-features=HttpsFirstBalancedModeAutoEnable,HttpsUpgrades,LocalNetworkAccessChecks",
# Required for bluetooth testing.
"--enable-features=WebBluetooth"
"--enable-features=WebBluetooth",
# Required for testing with `.test` domains.
"--host-resolver-rules=MAP *.test 127.0.0.1",
]
}
}
Expand Down Expand Up @@ -341,65 +339,65 @@ def url_all_origins(request, url_example, url_example_another_origin, html):


@pytest.fixture
def url_base(local_server_http):
def url_base(local_server_http, some_host):
"""Return a generic example URL with status code 200."""
return local_server_http.url_base()
return local_server_http.url_base(host=some_host)


@pytest.fixture
def url_example(local_server_http):
def url_example(local_server_http, some_host):
"""Return a generic example URL with status code 200."""
return local_server_http.url_200()
return local_server_http.url_200(host=some_host)


@pytest.fixture
def url_example_another_origin(local_server_http_another_host):
def url_example_another_origin(local_server_http, another_host):
"""Return a generic example URL with status code 200, in a domain other than
the example_url fixture."""
return local_server_http_another_host.url_200()
return local_server_http.url_200(host=another_host)


@pytest.fixture
def url_auth_required(local_server_http):
def url_auth_required(local_server_http, some_host):
"""Return a URL that requires authentication (status code 401).
Alternatively, any of the following URLs could also be used:
- "https://authenticationtest.com/HTTPAuth/"
- "http://the-internet.herokuapp.com/basic_auth"
- "http://httpstat.us/401"
"""
return local_server_http.url_basic_auth()
return local_server_http.url_basic_auth(host=some_host)


@pytest.fixture
def url_hang_forever(local_server_http):
def url_hang_forever(local_server_http, some_host):
"""Return a URL that hangs forever."""
try:
yield local_server_http.url_hang_forever()
yield local_server_http.url_hang_forever(host=some_host)
finally:
local_server_http.hang_forever_stop()


@pytest.fixture(scope="session")
def url_bad_ssl(local_server_bad_ssl):
def url_bad_ssl(local_server_bad_ssl, some_host):
"""
Return a URL with an invalid certificate authority from a SSL certificate.
In Chromium, this generates the following error:

> Your connection is not private
> NET::ERR_CERT_AUTHORITY_INVALID
"""
return local_server_bad_ssl.url_200()
return local_server_bad_ssl.url_200(host=some_host)


@pytest.fixture(scope="session")
def url_secure_context(local_server_good_ssl):
return local_server_good_ssl.url_200()
def url_secure_context(local_server_good_ssl, some_host):
return local_server_good_ssl.url_200(host=some_host)


@pytest.fixture
def url_cacheable(local_server_http):
def url_cacheable(local_server_http, some_host):
"""Return a generic example URL that can be cached."""
return local_server_http.url_cacheable()
return local_server_http.url_cacheable(host=some_host)


@pytest.fixture
Expand Down Expand Up @@ -588,11 +586,12 @@ async def activate_main_tab():


@pytest.fixture
def url_download(local_server_http):
def url_download(local_server_http, some_host):
"""Return a URL that triggers a download."""
def url_download(file_name="file-name.txt", content="download content"):
return local_server_http.url_200(
content,
host=some_host,
content=content,
content_type="application/octet-stream",
headers={
"Content-Disposition": f"attachment; filename=\"{file_name}\""
Expand All @@ -602,22 +601,20 @@ def url_download(file_name="file-name.txt", content="download content"):


@pytest.fixture
def url_hang_forever_download(local_server_http):
def url_hang_forever_download(local_server_http, some_host):
"""Return a URL that triggers a download which hangs forever."""
try:
yield local_server_http.url_hang_forever_download
yield local_server_http.url_hang_forever_download(host=some_host)
finally:
local_server_http.hang_forever_stop()


@pytest.fixture
def html(local_server_http, local_server_http_another_host):
def html(local_server_http, some_host, another_host):
"""Return a factory for URL with the given content."""
def html(content="", same_origin=True):
if same_origin:
return local_server_http.url_200(content=content)
else:
return local_server_http_another_host.url_200(content=content)
return local_server_http.url_200(
host=(some_host if same_origin else another_host), content=content)

return html

Expand Down
10 changes: 3 additions & 7 deletions tests/emulation/test_geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,12 @@ async def test_geolocation_emulate_unavailable(websocket, context_id,

@pytest.mark.asyncio
async def test_geolocation_per_user_context(websocket, url_example,
url_example_another_origin,
url_secure_context,
user_context_id, create_context,
snapshot):
# `url_example_another_origin` is required as `local_server_http` can
# be dead-locked in case of concurrent requests.

await set_permission(websocket, get_origin(url_example),
{'name': 'geolocation'}, 'granted', "default")
await set_permission(websocket, get_origin(url_example_another_origin),
await set_permission(websocket, get_origin(url_secure_context),
{'name': 'geolocation'}, 'granted', user_context_id)

# Set different geolocation overrides for different user contexts.
Expand Down Expand Up @@ -203,8 +200,7 @@ async def test_geolocation_per_user_context(websocket, url_example,
assert emulated_geolocation_1 == snapshot()

browsing_context_id_2 = await create_context(user_context_id)
await goto_url(websocket, browsing_context_id_2,
url_example_another_origin)
await goto_url(websocket, browsing_context_id_2, url_secure_context)
emulated_geolocation_2 = await get_geolocation(websocket,
browsing_context_id_2)
assert emulated_geolocation_2 == snapshot()
4 changes: 2 additions & 2 deletions tests/script/test_realm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import pytest
from anys import ANY_DICT, ANY_NUMBER, ANY_STR
from test_helpers import (AnyExtending, execute_command, goto_url,
from test_helpers import (AnyExtending, execute_command, get_origin, goto_url,
read_JSON_message, send_JSON_command, subscribe,
wait_for_event, wait_for_filtered_event)

Expand Down Expand Up @@ -44,7 +44,7 @@ async def test_realm_realmCreated(websocket, context_id, html,
"method": "script.realmCreated",
"params": {
"type": "window",
"origin": local_server_http.origin(),
"origin": get_origin(url),
"realm": ANY_STR,
"context": context_id,
}
Expand Down
7 changes: 4 additions & 3 deletions tests/service_worker/test_navigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
}],
indirect=True)
async def test_serviceWorker_acceptInsecureCertsCapability_respected(
websocket, context_id, local_server_bad_ssl):
websocket, context_id, local_server_bad_ssl, some_host):
service_worker_script = local_server_bad_ssl.url_200(
content='', content_type='text/javascript')
service_worker_page = local_server_bad_ssl.url_200(content=f"""<script>
host=some_host, content='', content_type='text/javascript')
service_worker_page = local_server_bad_ssl.url_200(host=some_host,
content=f"""<script>
window.registrationPromise = navigator.serviceWorker.register('{service_worker_script}');
</script>""")

Expand Down
Loading
Loading