Repository navigation
fix(debug): bump pysignalr to 1.3.3 to keep negotiate cookies on the WebSocket upgrade #2016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+121
−8
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import asyncio | ||
| import json | ||
| from collections.abc import AsyncIterator | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from aiohttp import WSMsgType, web | ||
| from aiohttp.test_utils import TestServer | ||
| from pysignalr.client import SignalRClient | ||
|
|
||
| from uipath._cli._debug._bridge import SignalRDebugBridge | ||
|
|
||
| RECORD_SEPARATOR = "\x1e" | ||
|
|
||
|
|
||
| class _Hub: | ||
| def __init__(self) -> None: | ||
| self.upgrade_cookies: list[str | None] = [] | ||
| self.invocations: list[dict[str, Any]] = [] | ||
|
|
||
| async def negotiate(self, request: web.Request) -> web.Response: | ||
| response = web.json_response({"connectionId": "conn-1", "negotiateVersion": 0}) | ||
| response.set_cookie("affinity", "backend-1") | ||
| return response | ||
|
|
||
| async def websocket(self, request: web.Request) -> web.WebSocketResponse: | ||
| self.upgrade_cookies.append(request.headers.get("Cookie")) | ||
| ws = web.WebSocketResponse() | ||
| await ws.prepare(request) | ||
| async for message in ws: | ||
| if message.type != WSMsgType.TEXT: | ||
| continue | ||
| for record in filter(None, message.data.split(RECORD_SEPARATOR)): | ||
| frame = json.loads(record) | ||
| if "protocol" in frame: | ||
| await ws.send_str("{}" + RECORD_SEPARATOR) | ||
| elif frame.get("type") == 1: | ||
| self.invocations.append(frame) | ||
| if "invocationId" in frame: | ||
| completion = { | ||
| "type": 3, | ||
| "invocationId": frame["invocationId"], | ||
| "error": "command rejected", | ||
| } | ||
| await ws.send_str(json.dumps(completion) + RECORD_SEPARATOR) | ||
| return ws | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def hub() -> AsyncIterator[tuple[_Hub, str]]: | ||
| state = _Hub() | ||
| app = web.Application() | ||
| app.router.add_post("/hub/negotiate", state.negotiate) | ||
| app.router.add_get("/hub", state.websocket) | ||
| server = TestServer(app) | ||
| await server.start_server() | ||
| # aiohttp's default cookie jar ignores cookies from IP-address hosts. | ||
| yield state, str(server.make_url("/hub")).replace("127.0.0.1", "localhost") | ||
| await server.close() | ||
|
|
||
|
|
||
| async def test_websocket_upgrade_sends_the_negotiate_cookie( | ||
| hub: tuple[_Hub, str], | ||
| ) -> None: | ||
| state, url = hub | ||
| client = SignalRClient(url, connection_timeout=1) | ||
| opened = asyncio.Event() | ||
|
|
||
| async def on_open() -> None: | ||
| opened.set() | ||
|
|
||
| client.on_open(on_open) | ||
| run_task = asyncio.create_task(client.run()) | ||
| try: | ||
| await asyncio.wait_for(opened.wait(), timeout=5) | ||
| finally: | ||
| run_task.cancel() | ||
| await asyncio.gather(run_task, return_exceptions=True) | ||
|
|
||
| assert state.upgrade_cookies == ["affinity=backend-1"] | ||
|
|
||
|
|
||
| async def test_hub_side_send_error_reaches_the_error_handler( | ||
| hub: tuple[_Hub, str], monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| state, url = hub | ||
| bridge = SignalRDebugBridge(hub_url=url) | ||
| errors: list[Any] = [] | ||
|
|
||
| async def record_error(error: Any) -> None: | ||
| errors.append(error) | ||
|
|
||
| monkeypatch.setattr(bridge, "_handle_error", record_error) | ||
| await bridge.connect() | ||
| try: | ||
| await bridge._send("OnExecutionStarted", {"job": "1"}) | ||
| for _ in range(50): | ||
| if errors: | ||
| break | ||
| await asyncio.sleep(0.05) | ||
| finally: | ||
| await bridge.disconnect() | ||
|
|
||
| assert state.invocations[0]["target"] == "SendCommand" | ||
| assert "invocationId" in state.invocations[0] | ||
| assert [error.error for error in errors] == ["command rejected"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.