Repository navigation
Encode query keys and values before building request URLs #514
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||
| import copy | ||||||
| import hashlib | ||||||
| import hmac | ||||||
| from urllib.parse import urlencode | ||||||
|
|
||||||
|
|
||||||
| # generates HMAC signature for the NotificationRequest object | ||||||
|
|
@@ -94,4 +95,4 @@ def is_valid_hmac_payload(hmac_signature, hmac_key, payload): | |||||
|
|
||||||
|
|
||||||
| def get_query(query_parameters): | ||||||
| return "?" + "&".join([f"{k}={v}" for k, v in query_parameters.items()]) | ||||||
| return "?" + urlencode(query_parameters) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By default, Additionally, using
Suggested change
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import unittest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from unittest.mock import Mock | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from urllib.parse import parse_qs, urlsplit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import Adyen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from Adyen.util import get_query | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TestQueryEncoding(unittest.TestCase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_reserved_characters_round_trip(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "reference": "Invoice A&B + 10% #café", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "filter[key]&name": "x=y?z/1", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "empty": "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "pageNumber": 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encoded = get_query(parameters) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parse_qs(urlsplit("https://example.test/" + encoded).query, keep_blank_values=True), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {key: [str(value)] for key, value in parameters.items()}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(urlsplit("https://example.test/" + encoded).fragment, "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the test to verify that spaces are encoded as
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_public_service_preserves_query_values(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| adyen = Adyen.Adyen() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| adyen.client.platform = "test" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| adyen.client.xapikey = "test-api-key" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| adyen.client._init_http_client() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request = Mock(return_value=["{}", {}, 200, {}]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| adyen.client.http_client.request = request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters = {"reference": "A&B + # café", "pageNumber": 3} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| adyen.checkout.payments_api.payment_methods( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"merchantAccount": "test-merchant"}, query_parameters=parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url = request.call_args.args[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parse_qs(urlsplit(url).query), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"reference": ["A&B + # café"], "pageNumber": ["3"]}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(parameters, {"reference": "A&B + # café", "pageNumber": 3}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_empty_query_keeps_existing_output(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(get_query({}), "?") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
quotefromurllib.parseto support encoding spaces as%20instead of+in query parameters, ensuring compliance with RFC 3986.