Skip to content

Commit 850db16

Browse files
committed
test: browser context tests, part 2
1 parent a1e3d58 commit 850db16

File tree

5 files changed

+551
-12
lines changed

5 files changed

+551
-12
lines changed

playwright/browser_context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ async def route(self, match: URLMatch, handler: RouteHandler) -> None:
170170
"setNetworkInterceptionEnabled", dict(enabled=True)
171171
)
172172

173-
async def unroute(self, match: URLMatch, handler: Optional[RouteHandler]) -> None:
173+
async def unroute(
174+
self, match: URLMatch, handler: Optional[RouteHandler] = None
175+
) -> None:
174176
self._routes = list(
175177
filter(
176178
lambda r: r.matcher.match != match

tests/assets/playground.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Playground</title>
5+
</head>
6+
<body>
7+
<button>A button</button>
8+
<textarea>A text area</textarea>
9+
<div id="first">First div</div>
10+
<div id="second">
11+
Second div
12+
<span class="inner">Inner span</span>
13+
</div>
14+
</body>
15+
</html>

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ async def context(browser):
6969
context = await browser.newContext()
7070
yield context
7171
await context.close()
72-
assert len(browser.contexts) == 0
72+
if len(browser.contexts):
73+
for context in browser.contexts:
74+
await context.close()
75+
assert False
7376

7477

7578
@pytest.fixture

tests/server.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ def __repr__(self) -> str:
5151
def start(self):
5252
request_subscribers = {}
5353
auth = {}
54+
csp = {}
5455
routes = {}
5556
gzip_routes = set()
5657
self.request_subscribers = request_subscribers
5758
self.auth = auth
59+
self.csp = csp
5860
self.routes = routes
5961
self.gzip_routes = gzip_routes
6062
static_path = os.path.join(os.path.dirname(__file__), "assets")
@@ -77,13 +79,15 @@ def process(self):
7779
request.getUser(),
7880
request.getPassword(),
7981
)
80-
if not (authorization_header or creds_correct):
82+
if not creds_correct:
8183
request.setHeader(
8284
b"www-authenticate", 'Basic realm="Secure Area"'
8385
)
8486
request.setResponseCode(HTTPStatus.UNAUTHORIZED)
8587
request.finish()
8688
return
89+
if csp.get(uri_path):
90+
request.setHeader(b"Content-Security-Policy", csp[uri_path])
8791
if routes.get(uri_path):
8892
routes[uri_path](request)
8993
return
@@ -123,15 +127,18 @@ def stop(self):
123127
async def wait_for_request(self, path):
124128
future = asyncio.Future()
125129
self.request_subscribers[path] = future
126-
future.add_done_callback(lambda f: self.request_subscribers.pop(path, None))
127130
return await future
128131

129132
def set_auth(self, path: str, username: str, password: str):
130133
self.auth[path] = (username, password)
131134

135+
def set_csp(self, path: str, value: str):
136+
self.csp[path] = value
137+
132138
def reset(self):
133139
self.request_subscribers.clear()
134140
self.auth.clear()
141+
self.csp.clear()
135142
self.gzip_routes.clear()
136143
self.routes.clear()
137144

0 commit comments

Comments
 (0)