3535 Union ,
3636 cast ,
3737)
38- from urllib .parse import urljoin , urlparse , urlunparse
38+ from urllib .parse import ParseResult , urljoin , urlparse , urlunparse
3939
4040from playwright ._impl ._api_structures import NameValue
4141from playwright ._impl ._errors import (
@@ -241,14 +241,9 @@ def resolve_base_url(
241241 base_url : Optional [str ], given_url : str
242242) -> Tuple [str , Optional [str ]]:
243243 try :
244- url = urlparse (urljoin (base_url if base_url is not None else "" , given_url ))
245-
246- # In Node.js, new URL('http://localhost') returns 'http://localhost/'.
247- if (
248- url .scheme .startswith ("http" ) or url .scheme .startswith ("ws" )
249- ) and url .path == "" :
250- url = url ._replace (path = "/" )
251-
244+ url = nodelike_urlparse (
245+ urljoin (base_url if base_url is not None else "" , given_url )
246+ )
252247 resolved = urlunparse (url )
253248 # Schema and domain are case-insensitive.
254249 hostname_port = (
@@ -262,6 +257,20 @@ def resolve_base_url(
262257 return given_url , None
263258
264259
260+ def nodelike_urlparse (url : str ) -> ParseResult :
261+ parsed = urlparse (url , allow_fragments = True )
262+
263+ # https://url.spec.whatwg.org/#special-scheme
264+ is_special_url = parsed .scheme in ["http" , "https" , "ws" , "wss" , "ftp" , "file" ]
265+ if is_special_url :
266+ # special urls have a list path, list paths are serialized as follows: https://url.spec.whatwg.org/#url-path-serializer
267+ # urllib diverges, so we patch it here
268+ if parsed .path == "" :
269+ parsed = parsed ._replace (path = "/" )
270+
271+ return parsed
272+
273+
265274class HarLookupResult (TypedDict , total = False ):
266275 action : Literal ["error" , "redirect" , "fulfill" , "noentry" ]
267276 message : Optional [str ]
0 commit comments