Mailgun: disable non-ASCII attachment filename workaround when not needed

urllib3 v1.25 fixes non-ASCII filenames in multipart form data to be
RFC 5758 compliant by default, so our earlier workaround is no longer
needed. Disable the workaround if we detect that Requests is using a
fixed version of urllib3.

Closes #157
This commit is contained in:
Mike Edmunds
2019-09-03 18:04:27 -07:00
committed by GitHub
parent df29ee2da6
commit fd558e904e
6 changed files with 55 additions and 5 deletions

View File

@@ -115,6 +115,13 @@ class RequestsBackendMockAPITestCase(SimpleTestCase, AnymailTestMixin):
"""Returns the auth sent to the mock ESP API"""
return self.get_api_call_arg('auth', required)
def get_api_prepared_request(self):
"""Returns the PreparedRequest that would have been sent"""
(args, kwargs) = self.mock_request.call_args
kwargs.pop('timeout', None) # Session-only param
request = requests.Request(**kwargs)
return request.prepare()
def assert_esp_not_called(self, msg=None):
if self.mock_request.called:
raise AssertionError(msg or "ESP API was called and shouldn't have been")

View File

@@ -182,12 +182,20 @@ class MailgunBackendStandardEmailTests(MailgunBackendMockAPITestCase):
self.message.send()
# Verify the RFC 7578 compliance workaround has kicked in:
data = self.get_api_call_data().decode("utf-8")
data = self.get_api_call_data()
if isinstance(data, dict): # workaround not needed or used (but let's double check actual request)
workaround = False
prepared = self.get_api_prepared_request()
data = prepared.body
else:
workaround = True
data = data.decode("utf-8").replace("\r\n", "\n")
self.assertNotIn("filename*=", data) # No RFC 2231 encoding
self.assertIn(u'Content-Disposition: form-data; name="attachment"; filename="Une pièce jointe.html"', data)
files = self.get_api_call_files(required=False)
self.assertFalse(files) # files should have been moved to formdata body
if workaround:
files = self.get_api_call_files(required=False)
self.assertFalse(files) # files should have been moved to formdata body
def test_rfc_7578_compliance(self):
# Check some corner cases in the workaround that undoes RFC 2231 multipart/form-data encoding...
@@ -214,7 +222,12 @@ class MailgunBackendStandardEmailTests(MailgunBackendMockAPITestCase):
self.message.attach(u"besked med vedhæftede filer", forwarded_message, "message/rfc822")
self.message.send()
data = self.get_api_call_data().decode("utf-8").replace("\r\n", "\n")
data = self.get_api_call_data()
if isinstance(data, dict): # workaround not needed or used (but let's double check actual request)
prepared = self.get_api_prepared_request()
data = prepared.body
data = data.decode("utf-8").replace("\r\n", "\n")
# Top-level attachment (in form-data) should have RFC 7578 filename (raw Unicode):
self.assertIn(
u'Content-Disposition: form-data; name="attachment"; filename="besked med vedhæftede filer"', data)