Skip to content
Merged
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
3 changes: 3 additions & 0 deletions newsfragments/1502.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Send cookies which the browser holds for the Vuforia developer portal
itself, and so have no domain attribute of their own, with requests to
the Vuforia credentials API.
10 changes: 8 additions & 2 deletions src/vws_web_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,11 @@ def _requests_session_from_driver(
if not _is_json_array(cookies):
return session

# A cookie set for the current host has no ``domain`` attribute of
# its own. Give those the browser's host, as a cookie with no domain
# is never sent.
current_host = urlparse(url=driver.current_url).hostname

for cookie in cookies:
if not _is_json_object(cookie):
continue
Expand All @@ -1356,12 +1361,13 @@ def _requests_session_from_driver(
if not isinstance(name, str) or not isinstance(value, str):
continue

domain = cookie.get("domain")
raw_domain = cookie.get("domain")
domain = raw_domain if isinstance(raw_domain, str) else current_host
path = cookie.get("path")
cookie_kwargs = {
"path": path if isinstance(path, str) else "/",
}
if isinstance(domain, str):
if domain:
cookie_kwargs["domain"] = domain
session.cookies.set(
name=name,
Expand Down
29 changes: 28 additions & 1 deletion tests/test_model_target_web_api_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import vws_web_tools

_PORTAL_URL = "https://developer.vuforia.com/develop/credentials"


class _BrowserStateDriver(WebDriver):
"""A WebDriver shell with controlled browser state."""
Expand All @@ -21,10 +23,17 @@ def __init__(
*,
user_agent: object,
cookies: object,
current_url: str,
) -> None:
"""Store controlled browser state."""
self._user_agent = user_agent
self._cookies = cookies
self._current_url = current_url

@property
def current_url(self) -> str:
"""Return the controlled browser URL."""
return self._current_url

def execute_script(self, script: str, *args: object) -> object:
"""Return the controlled user agent."""
Expand Down Expand Up @@ -52,16 +61,33 @@ def test_requests_session_from_driver_copies_browser_state() -> None:
{"name": "bad-value", "value": 123},
"not a cookie",
],
current_url=_PORTAL_URL,
)

session = vws_web_tools._requests_session_from_driver(driver=driver)

assert session.headers["User-Agent"] == "test browser"
assert session.cookies.get(name="session", domain="example.com") == "abc"
assert session.cookies.get(name="host") == "only"
assert (
session.cookies.get(name="host", domain="developer.vuforia.com")
== "only"
)
assert session.cookies.get(name="bad-value") is None


def test_requests_session_from_driver_without_a_browser_host() -> None:
"""A host-only cookie is still set when the URL names no host."""
driver = _BrowserStateDriver(
user_agent="test browser",
cookies=[{"name": "host", "value": "only"}],
current_url="about:blank",
)

session = vws_web_tools._requests_session_from_driver(driver=driver)

assert session.cookies.get(name="host") == "only"


@pytest.mark.parametrize(
argnames=("user_agent", "cookies"),
argvalues=[
Expand All @@ -78,6 +104,7 @@ def test_requests_session_from_driver_handles_unexpected_browser_state(
driver = _BrowserStateDriver(
user_agent=user_agent,
cookies=cookies,
current_url=_PORTAL_URL,
)

session = vws_web_tools._requests_session_from_driver(driver=driver)
Expand Down