Repository navigation
Preserve host-provided Durable management URL query parameters #632
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
Open
Naiyuan Tian (nytian)
wants to merge
3
commits into
dev
Choose a base branch
from
nytian/management-url
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−10
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,139 @@ | ||
| import copy | ||
| import json | ||
| from unittest.mock import AsyncMock | ||
| from urllib.parse import urlsplit | ||
|
|
||
| import azure.functions as func | ||
| import pytest | ||
|
|
||
| from azure.durable_functions.models.DurableOrchestrationBindings import ( | ||
| DurableOrchestrationBindings) | ||
| from azure.durable_functions.models.DurableOrchestrationClient import ( | ||
| DurableOrchestrationClient) | ||
|
|
||
|
|
||
| BASE_URL = "https://notification.test/runtime/webhooks/durabletask" | ||
| QUERIES = [ | ||
| "taskHub=hub&connection=storage&code=synthetic-INSTANCEID%2Bkey%2F%3D", | ||
| "CoDe=synthetic-INSTANCEID%2Bkey%2F%3D&site=value%2Bone", | ||
| "%63ode=synthetic-INSTANCEID%2Bkey%2F%3D&CODE=duplicate", | ||
| "taskHub=hub&connection=storage", | ||
| "taskHub=hub&connection=storage&metadata=https://notification.test/info", | ||
| "code=synthetic-INSTANCEID%2Bkey%2F%3D&metadata=https://notification.test/INSTANCEID", | ||
| "", | ||
| ] | ||
|
|
||
|
|
||
| def binding(query): | ||
| suffix = "?" + query if query else "" | ||
| reason = "?reason={text}" + ("&" + query if query else "") | ||
| return { | ||
| "taskHubName": "hub", | ||
| "rpcBaseUrl": "http://rpc.test/durabletask/", | ||
| "creationUrls": { | ||
| "createNewInstancePostUri": | ||
| BASE_URL + "/orchestrators/{functionName}[/{instanceId}]" + suffix, | ||
| }, | ||
| "managementUrls": { | ||
| "id": "INSTANCEID", | ||
| "statusQueryGetUri": BASE_URL + "/instances/INSTANCEID" + suffix, | ||
| "sendEventPostUri": | ||
| BASE_URL + "/instances/INSTANCEID/raiseEvent/{eventName}" + suffix, | ||
| "terminatePostUri": BASE_URL + "/instances/INSTANCEID/terminate" + reason, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "value_url, expected_url", | ||
| [ | ||
| ( | ||
| "https://notification.test/https://notification.test/path" | ||
| "?metadata=https://notification.test/info#https://notification.test/fragment", | ||
| "http://frontend.test:8080/https://notification.test/path" | ||
| "?metadata=https://notification.test/info#https://notification.test/fragment", | ||
| ), | ||
| ( | ||
| "https://notification.test?metadata=https://notification.test/info", | ||
| "http://frontend.test:8080?metadata=https://notification.test/info", | ||
| ), | ||
| ("INSTANCEID", "INSTANCEID"), | ||
| ]) | ||
| def test_replace_url_origin_changes_only_scheme_and_authority(value_url, expected_url): | ||
| request_url = "http://frontend.test:8080/api/start?code=starter-key#request-fragment" | ||
|
|
||
| assert DurableOrchestrationClient._replace_url_origin(request_url, value_url) == expected_url | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("query", QUERIES) | ||
| def test_binding_preserves_host_urls_and_query_strings(query): | ||
| data = binding(query) | ||
| original = copy.deepcopy(data) | ||
| bindings = DurableOrchestrationBindings(**data) | ||
|
|
||
| assert bindings.creation_urls == data["creationUrls"] | ||
| assert bindings.management_urls == data["managementUrls"] | ||
| assert data == original | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("query", QUERIES) | ||
| def test_response_preserves_host_query_and_string_only_payload(query): | ||
| data = binding(query) | ||
| original = copy.deepcopy(data) | ||
| client = DurableOrchestrationClient(json.dumps(data)) | ||
| request = func.HttpRequest( | ||
| "GET", "https://frontend.test/api/start?code=starter-key", body=b"") | ||
|
|
||
| response = client.create_check_status_response(request, "instance") | ||
| payload = json.loads(response.get_body()) | ||
|
|
||
| notification_origin = "https://notification.test" | ||
| expected = { | ||
| key: ( | ||
| "https://frontend.test" + value[len(notification_origin):] | ||
| if value.startswith(notification_origin) else value | ||
| ).replace("INSTANCEID", "instance", 1) | ||
| for key, value in data["managementUrls"].items() | ||
| } | ||
| assert response.status_code == 202 | ||
| assert response.headers["Location"] == payload["statusQueryGetUri"] | ||
| assert response.headers["Retry-After"] == "10" | ||
| assert payload == expected | ||
| for key, original_url in data["managementUrls"].items(): | ||
| if key != "id": | ||
| assert urlsplit(payload[key]).query == urlsplit(original_url).query | ||
| assert all(isinstance(value, str) for value in payload.values()) | ||
| assert "requiredHeaders" not in payload | ||
| assert "starter-key" not in json.dumps(payload) | ||
| assert "{eventName}" in payload["sendEventPostUri"] | ||
| assert data == original | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("query", QUERIES) | ||
| def test_payload_instances_do_not_modify_the_binding(query): | ||
| data = binding(query) | ||
| original = copy.deepcopy(data) | ||
| client = DurableOrchestrationClient(json.dumps(data)) | ||
| first = client.create_http_management_payload("first") | ||
| first["statusQueryGetUri"] = "changed" | ||
| second = client.create_http_management_payload("second") | ||
|
|
||
| assert second["id"] == "second" | ||
| assert second["statusQueryGetUri"] == \ | ||
| data["managementUrls"]["statusQueryGetUri"].replace("INSTANCEID", "second", 1) | ||
| assert data == original | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("query", QUERIES) | ||
| @pytest.mark.asyncio | ||
| async def test_management_uses_rpc_without_public_key_credentials(query): | ||
| client = DurableOrchestrationClient(json.dumps(binding(query))) | ||
| client._post_async_request = AsyncMock(return_value=[202, {"id": "instance"}]) | ||
|
|
||
| assert await client.start_new("Allowed", "instance") == "instance" | ||
|
|
||
| args, kwargs = client._post_async_request.await_args | ||
| assert args[0] == "http://rpc.test/durabletask/orchestrators/Allowed/instance" | ||
| assert "headers" not in kwargs | ||
| assert "code" not in urlsplit(args[0]).query | ||
| assert "synthetic" not in str((args, kwargs)) |
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.