Skip to content
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

APIRequestor: don't mutate incoming multipart headers #1091

Merged
merged 4 commits into from
Oct 18, 2023
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
16 changes: 10 additions & 6 deletions stripe/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import platform
import time
from typing import Optional, Tuple
from typing import Dict, Optional, Tuple
import uuid
import warnings
from collections import OrderedDict
Expand Down Expand Up @@ -296,6 +296,10 @@ def request_raw(
Mechanism for issuing an API call
"""

supplied_headers_dict: Optional[Dict[str, str]] = (
dict(supplied_headers) if supplied_headers is not None else None
)

if self.api_key:
my_api_key = self.api_key
else:
Expand Down Expand Up @@ -327,14 +331,14 @@ def request_raw(
post_data = None
elif method == "post":
if (
supplied_headers is not None
and supplied_headers.get("Content-Type")
supplied_headers_dict is not None
and supplied_headers_dict.get("Content-Type")
== "multipart/form-data"
):
generator = MultipartDataGenerator()
generator.add_params(params or {})
post_data = generator.get_post_data()
supplied_headers[
supplied_headers_dict[
"Content-Type"
] = "multipart/form-data; boundary=%s" % (generator.boundary,)
else:
Expand All @@ -347,8 +351,8 @@ def request_raw(
)

headers = self.request_headers(my_api_key, method)
if supplied_headers is not None:
for key, value in supplied_headers.items():
if supplied_headers_dict is not None:
for key, value in supplied_headers_dict.items():
headers[key] = value

util.log_info("Request to Stripe api", method=method, path=abs_url)
Expand Down
25 changes: 17 additions & 8 deletions tests/api_resources/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def test_is_retrievable(self, request_mock):
request_mock.assert_requested("get", "/v1/files/%s" % TEST_RESOURCE_ID)
assert isinstance(resource, stripe.File)

def test_is_creatable(self, setup_upload_api_base, request_mock):
def test_is_creatable(self, mocker):
hc = mocker.Mock(stripe.http_client.HTTPClient)
richardm-stripe marked this conversation as resolved.
Show resolved Hide resolved
hc.name = "mockclient"
stripe.default_http_client = hc
hc.request_with_retries.return_value = ('{"object": "file"}', 200, {})
stripe.multipart_data_generator.MultipartDataGenerator._initialize_boundary = (
lambda self: 1234567890
)
Expand All @@ -38,14 +42,19 @@ def test_is_creatable(self, setup_upload_api_base, request_mock):
file=test_file,
file_link_data={"create": True},
)
request_mock.assert_api_base(stripe.upload_api_base)
request_mock.assert_requested(
"post",
"/v1/files",
headers={
"Content-Type": "multipart/form-data; boundary=1234567890"
},
method, url, headers, body = hc.request_with_retries.call_args[0]
assert method == "post"
assert url.endswith("/v1/files")
assert (
headers["Content-Type"]
== "multipart/form-data; boundary=1234567890"
)
parts = body.split(b"--1234567890")
assert len(parts) == 5
assert parts[0] == b""
assert b'name="purpose"' in parts[1]
assert b'name="file"' in parts[2]
assert b'name="file_link_data[create]"' in parts[3]
assert isinstance(resource, stripe.File)

def test_create_respects_stripe_version(
Expand Down
57 changes: 0 additions & 57 deletions tests/api_resources/test_file_upload.py

This file was deleted.

1 change: 1 addition & 0 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ def test_raw_request_with_file_param(self, requestor, mock_response):
supplied_headers = {"Content-Type": "multipart/form-data"}
mock_response("{}", 200)
requestor.request("post", "/v1/files", params, supplied_headers)
assert supplied_headers["Content-Type"] == "multipart/form-data"


class TestDefaultClient(object):
Expand Down