Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xavdid-stripe committed Dec 16, 2024
1 parent e83c6e4 commit 3bf6446
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
19 changes: 13 additions & 6 deletions stripe/_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Unpack,
)
import uuid
from urllib.parse import urlsplit, urlunsplit
from urllib.parse import urlsplit, urlunsplit, parse_qs

# breaking circular dependency
import stripe # noqa: IMP101
Expand Down Expand Up @@ -561,12 +561,19 @@ def _args_for_request_with_retries(
# if we're sending params in the querystring, then we have to make sure we're not
# duplicating anything we got back from the server already (like in a list iterator)
# so, we parse the querystring the server sends back so we can merge with what we (or the user) are trying to send
# note: server sends back "expand[]" but users supply "expand", so we have to match them up
existing_params = {
"expand" if k == "expand[]" else k: v
for k, v in parse_qs(urlsplit(url).query).items()
}
existing_params = {}
for k, v in parse_qs(urlsplit(url).query).items():
# note: server sends back "expand[]" but users supply "expand", so we strip the brackets from the key name
if k.endswith("[]"):
existing_params[k[:-2]] = v
else:
# all querystrings are pulled out as lists.
# We want to keep the querystrings that actually are lists, but flatten the ones that are single values
existing_params[k] = v[0] if len(v) == 1 else v

# if a user is expanding something that wasn't expanded before, add (and deduplicate) it
# this could theoretically work for other lists that we want to merge too, but that doesn't seem to be a use case
# it never would have worked before, so I think we can start with `expand` and go from there
if "expand" in existing_params and "expand" in params:
params["expand"] = list( # type:ignore - this is a dict
set([*existing_params["expand"], *params["expand"]])
Expand Down
9 changes: 5 additions & 4 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,17 @@ def test_ordereddict_encoding(self):

def test_url_construction(self, requestor, http_client_mock):
CASES = (
("%s?foo=bar" % stripe.api_base, "", {"foo": "bar"}),
("%s?foo=bar" % stripe.api_base, "?", {"foo": "bar"}),
(f"{stripe.api_base}?foo=bar", "", {"foo": "bar"}),
(f"{stripe.api_base}?foo=bar", "?", {"foo": "bar"}),
(stripe.api_base, "", {}),
(
"%s/%%20spaced?foo=bar%%24&baz=5" % stripe.api_base,
f"{stripe.api_base}/%20spaced?baz=5&foo=bar%24",
"/%20spaced?foo=bar%24",
{"baz": "5"},
),
# duplicate query params keys should be deduped
(
"%s?foo=bar&foo=bar" % stripe.api_base,
f"{stripe.api_base}?foo=bar",
"?foo=bar",
{"foo": "bar"},
),
Expand Down

0 comments on commit 3bf6446

Please sign in to comment.