Skip to content

Commit

Permalink
Fix multipart encoding of nested parameters (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe authored Apr 8, 2019
1 parent c5fe81f commit fc8d4e5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
13 changes: 8 additions & 5 deletions stripe/multipart_data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import random
import io

from stripe import six
import stripe


class MultipartDataGenerator(object):
Expand All @@ -14,7 +14,10 @@ def __init__(self, chunk_size=1028):
self.chunk_size = chunk_size

def add_params(self, params):
for key, value in six.iteritems(params):
# Flatten parameters first
params = dict(stripe.api_requestor._api_encode(params))

for key, value in stripe.six.iteritems(params):
if value is None:
continue

Expand Down Expand Up @@ -45,7 +48,7 @@ def add_params(self, params):
self._write('"')
self._write(self.line_break)
self._write(self.line_break)
self._write(value)
self._write(str(value))

self._write(self.line_break)

Expand All @@ -58,9 +61,9 @@ def get_post_data(self):
return self.data.getvalue()

def _write(self, value):
if isinstance(value, six.binary_type):
if isinstance(value, stripe.six.binary_type):
array = bytearray(value)
elif isinstance(value, six.text_type):
elif isinstance(value, stripe.six.text_type):
array = bytearray(value, encoding="utf-8")
else:
raise TypeError(
Expand Down
4 changes: 3 additions & 1 deletion tests/api_resources/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def test_is_creatable(self, setup_upload_api_base, request_mock):
)
test_file = tempfile.TemporaryFile()
resource = stripe.File.create(
purpose="dispute_evidence", file=test_file
purpose="dispute_evidence",
file=test_file,
file_link_data={"create": True},
)
request_mock.assert_api_base(stripe.upload_api_base)
request_mock.assert_requested(
Expand Down
4 changes: 3 additions & 1 deletion tests/api_resources/test_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def test_is_creatable(self, setup_upload_api_base, request_mock):
)
test_file = tempfile.TemporaryFile()
resource = stripe.FileUpload.create(
purpose="dispute_evidence", file=test_file
purpose="dispute_evidence",
file=test_file,
file_link_data={"create": True},
)
request_mock.assert_api_base(stripe.upload_api_base)
request_mock.assert_requested(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_multipart_data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def run_test_multipart_data_with_file(self, test_file):
"key1": b"ASCII value",
"key2": u"Üñìçôdé value",
"key3": test_file,
"key4": {
"string": "Hello!",
"int": 234,
"float": 3.14159,
"bool": True,
"dict": {"foo": "bar"},
},
}
generator = MultipartDataGenerator()
generator.add_params(params)
Expand All @@ -36,6 +43,29 @@ def run_test_multipart_data_with_file(self, test_file):
http_body,
)
assert re.search(r"Content-Type: application/octet-stream", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[string\]\"",
http_body,
)
assert re.search(r"Hello!", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[int\]\"", http_body
)
assert re.search(r"234", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[float\]\"",
http_body,
)
assert re.search(r"3.14159", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[bool\]\"", http_body
)
assert re.search(r"True", http_body)
assert re.search(
r"Content-Disposition: form-data; name=\"key4\[dict\]\[foo\]\"",
http_body,
)
assert re.search(r"bar", http_body)

test_file.seek(0)
file_contents = test_file.read()
Expand Down

0 comments on commit fc8d4e5

Please sign in to comment.