Skip to content

Commit d859d76

Browse files
committed
test: fix tests on channels
1 parent 8368a13 commit d859d76

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

tests/async/test_browsercontext.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
from .utils import Utils
3333

3434

35+
@pytest.fixture(scope="session")
36+
def fails_on_401(browser_name: str, is_headless_shell: bool) -> bool:
37+
return browser_name == "chromium" and not is_headless_shell
38+
39+
3540
async def test_page_event_should_create_new_context(browser: Browser) -> None:
3641
assert len(browser.contexts) == 0
3742
context = await browser.new_context()
@@ -472,13 +477,17 @@ def logme(t: JSHandle) -> int:
472477

473478

474479
async def test_auth_should_fail_without_credentials(
475-
context: BrowserContext, server: Server
480+
context: BrowserContext, server: Server, fails_on_401: bool
476481
) -> None:
477482
server.set_auth("/empty.html", "user", "pass")
478483
page = await context.new_page()
479-
response = await page.goto(server.EMPTY_PAGE)
480-
assert response
481-
assert response.status == 401
484+
try:
485+
response = await page.goto(server.EMPTY_PAGE)
486+
assert response
487+
assert response.status == 401
488+
except Error as exc:
489+
assert fails_on_401
490+
assert "net::ERR_INVALID_AUTH_CREDENTIALS" in exc.message
482491

483492

484493
async def test_auth_should_work_with_correct_credentials(
@@ -562,7 +571,7 @@ async def test_should_work_with_correct_credentials_and_matching_origin_case_ins
562571

563572

564573
async def test_should_fail_with_correct_credentials_and_mismatching_scheme(
565-
browser: Browser, server: Server
574+
browser: Browser, server: Server, fails_on_401: bool
566575
) -> None:
567576
server.set_auth("/empty.html", "user", "pass")
568577
context = await browser.new_context(
@@ -573,14 +582,18 @@ async def test_should_fail_with_correct_credentials_and_mismatching_scheme(
573582
}
574583
)
575584
page = await context.new_page()
576-
response = await page.goto(server.EMPTY_PAGE)
577-
assert response
578-
assert response.status == 401
585+
try:
586+
response = await page.goto(server.EMPTY_PAGE)
587+
assert response
588+
assert response.status == 401
589+
except Error as exc:
590+
assert fails_on_401
591+
assert "net::ERR_INVALID_AUTH_CREDENTIALS" in exc.message
579592
await context.close()
580593

581594

582595
async def test_should_fail_with_correct_credentials_and_mismatching_hostname(
583-
browser: Browser, server: Server
596+
browser: Browser, server: Server, fails_on_401: bool
584597
) -> None:
585598
server.set_auth("/empty.html", "user", "pass")
586599
hostname = urlparse(server.PREFIX).hostname
@@ -590,24 +603,32 @@ async def test_should_fail_with_correct_credentials_and_mismatching_hostname(
590603
http_credentials={"username": "user", "password": "pass", "origin": origin}
591604
)
592605
page = await context.new_page()
593-
response = await page.goto(server.EMPTY_PAGE)
594-
assert response
595-
assert response.status == 401
606+
try:
607+
response = await page.goto(server.EMPTY_PAGE)
608+
assert response
609+
assert response.status == 401
610+
except Error as exc:
611+
assert fails_on_401
612+
assert "net::ERR_INVALID_AUTH_CREDENTIALS" in exc.message
596613
await context.close()
597614

598615

599616
async def test_should_fail_with_correct_credentials_and_mismatching_port(
600-
browser: Browser, server: Server
617+
browser: Browser, server: Server, fails_on_401: bool
601618
) -> None:
602619
server.set_auth("/empty.html", "user", "pass")
603620
origin = server.PREFIX.replace(str(server.PORT), str(server.PORT + 1))
604621
context = await browser.new_context(
605622
http_credentials={"username": "user", "password": "pass", "origin": origin}
606623
)
607624
page = await context.new_page()
608-
response = await page.goto(server.EMPTY_PAGE)
609-
assert response
610-
assert response.status == 401
625+
try:
626+
response = await page.goto(server.EMPTY_PAGE)
627+
assert response
628+
assert response.status == 401
629+
except Error as exc:
630+
assert fails_on_401
631+
assert "net::ERR_INVALID_AUTH_CREDENTIALS" in exc.message
611632
await context.close()
612633

613634

tests/async/test_websocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ async def test_should_reject_wait_for_event_on_close_and_error(
172172

173173

174174
async def test_should_emit_error_event(
175-
page: Page, server: Server, browser_name: str
175+
page: Page, server: Server, browser_name: str, browser_channel: str
176176
) -> None:
177177
future: "asyncio.Future[str]" = asyncio.Future()
178178

@@ -194,4 +194,4 @@ def _on_websocket(websocket: WebSocket) -> None:
194194
if browser_name == "firefox":
195195
assert err == "CLOSE_ABNORMAL"
196196
else:
197-
assert ": 404" in err
197+
assert ("" if browser_channel == "msedge" else ": 404") in err

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ def browser_channel(pytestconfig: pytest.Config) -> Optional[str]:
9191
return cast(Optional[str], pytestconfig.getoption("--browser-channel"))
9292

9393

94+
@pytest.fixture(scope="session")
95+
def is_headless_shell(browser_name: str, browser_channel: str, headless: bool) -> bool:
96+
return browser_name == "chromium" and (
97+
browser_channel == "chromium-headless-shell"
98+
or (not browser_channel and headless)
99+
)
100+
101+
94102
@pytest.fixture(scope="session")
95103
def is_webkit(browser_name: str) -> bool:
96104
return browser_name == "webkit"

tests/server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def process(self) -> None:
110110
if not creds_correct:
111111
self.setHeader(b"www-authenticate", 'Basic realm="Secure Area"')
112112
self.setResponseCode(HTTPStatus.UNAUTHORIZED)
113+
self.write(b"HTTP Error 401 Unauthorized: Access is denied")
113114
self.finish()
114115
return
115116
if server.csp.get(path):
@@ -133,7 +134,10 @@ def process(self) -> None:
133134
self.write(file_content)
134135
self.setResponseCode(HTTPStatus.OK)
135136
except (FileNotFoundError, IsADirectoryError, PermissionError):
137+
self.setHeader(b"Content-Type", "text/plain")
136138
self.setResponseCode(HTTPStatus.NOT_FOUND)
139+
if self.method != "HEAD":
140+
self.write(f"File not found: {path}".encode())
137141
self.finish()
138142

139143

0 commit comments

Comments
 (0)