File tree Expand file tree Collapse file tree 3 files changed +69
-1
lines changed Expand file tree Collapse file tree 3 files changed +69
-1
lines changed Original file line number Diff line number Diff line change 34
34
Union ,
35
35
cast ,
36
36
)
37
- from urllib .parse import urljoin
37
+ from urllib .parse import urljoin , urlparse
38
38
39
39
from playwright ._impl ._api_structures import NameValue
40
40
from playwright ._impl ._errors import (
@@ -157,6 +157,10 @@ def url_matches(
157
157
base_url = re .sub (r"^http" , "ws" , base_url )
158
158
if base_url :
159
159
match = urljoin (base_url , match )
160
+ parsed = urlparse (match )
161
+ if parsed .path == "" :
162
+ parsed = parsed ._replace (path = "/" )
163
+ match = parsed .geturl ()
160
164
if isinstance (match , str ):
161
165
match = glob_to_regex (match )
162
166
if isinstance (match , Pattern ):
Original file line number Diff line number Diff line change @@ -346,3 +346,36 @@ async def _handle_ws(ws: WebSocketRoute) -> None:
346
346
f"message: data=echo origin=ws://localhost:{ server .PORT } lastEventId=" ,
347
347
],
348
348
)
349
+
350
+
351
+ async def test_should_work_with_no_trailing_slash (page : Page , server : Server ) -> None :
352
+ log : list [str ] = []
353
+
354
+ async def handle_ws (ws : WebSocketRoute ) -> None :
355
+ def on_message (message : Union [str , bytes ]) -> None :
356
+ assert isinstance (message , str )
357
+ log .append (message )
358
+ ws .send ("response" )
359
+
360
+ ws .on_message (on_message )
361
+
362
+ # No trailing slash in the route pattern
363
+ await page .route_web_socket (f"ws://localhost:{ server .PORT } " , handle_ws )
364
+
365
+ await page .goto ("about:blank" )
366
+ await page .evaluate (
367
+ """({ port }) => {
368
+ window.log = [];
369
+ // No trailing slash in WebSocket URL
370
+ window.ws = new WebSocket('ws://localhost:' + port);
371
+ window.ws.addEventListener('message', event => window.log.push(event.data));
372
+ }""" ,
373
+ {"port" : server .PORT },
374
+ )
375
+
376
+ await assert_equal (
377
+ lambda : page .evaluate ("window.ws.readyState" ), 1 # WebSocket.OPEN
378
+ )
379
+ await page .evaluate ("window.ws.send('query')" )
380
+ await assert_equal (lambda : log , ["query" ])
381
+ await assert_equal (lambda : page .evaluate ("window.log" ), ["response" ])
Original file line number Diff line number Diff line change @@ -340,3 +340,34 @@ def _handle_ws(ws: WebSocketRoute) -> None:
340
340
f"message: data=echo origin=ws://localhost:{ server .PORT } lastEventId=" ,
341
341
],
342
342
)
343
+
344
+
345
+ def test_should_work_with_no_trailing_slash (page : Page , server : Server ) -> None :
346
+ log : list [str ] = []
347
+
348
+ async def handle_ws (ws : WebSocketRoute ) -> None :
349
+ def on_message (message : Union [str , bytes ]) -> None :
350
+ assert isinstance (message , str )
351
+ log .append (message )
352
+ ws .send ("response" )
353
+
354
+ ws .on_message (on_message )
355
+
356
+ # No trailing slash in the route pattern
357
+ page .route_web_socket (f"ws://localhost:{ server .PORT } " , handle_ws )
358
+
359
+ page .goto ("about:blank" )
360
+ page .evaluate (
361
+ """({ port }) => {
362
+ window.log = [];
363
+ // No trailing slash in WebSocket URL
364
+ window.ws = new WebSocket('ws://localhost:' + port);
365
+ window.ws.addEventListener('message', event => window.log.push(event.data));
366
+ }""" ,
367
+ {"port" : server .PORT },
368
+ )
369
+
370
+ assert_equal (lambda : page .evaluate ("window.ws.readyState" ), 1 ) # WebSocket.OPEN
371
+ page .evaluate ("window.ws.send('query')" )
372
+ assert_equal (lambda : log , ["query" ])
373
+ assert_equal (lambda : page .evaluate ("window.log" ), ["response" ])
You can’t perform that action at this time.
0 commit comments