SendGrid: convert long to str in headers, metadata

SendGrid requires extra headers and metadata values be strings.
Anymail has always coerced int and float; this treats Python 2's
`long` integer type the same.

Fixes #74
This commit is contained in:
medmunds
2017-09-14 11:45:17 -07:00
parent 168d46a254
commit 3b9cb963ef
5 changed files with 25 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ from decimal import Decimal
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
import six
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
from django.test import SimpleTestCase
@@ -19,6 +20,9 @@ from anymail.message import attach_inline_image_file
from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin
from .utils import sample_image_content, sample_image_path, SAMPLE_IMAGE_FILENAME, AnymailTestMixin
# noinspection PyUnresolvedReferences
longtype = int if six.PY3 else long
@override_settings(EMAIL_BACKEND='anymail.backends.sendgrid_v2.EmailBackend',
ANYMAIL={'SENDGRID_API_KEY': 'test_api_key'})
@@ -154,12 +158,13 @@ class SendGridBackendStandardEmailTests(SendGridBackendMockAPITestCase):
self.assertEqual(data['html'], html_content)
def test_extra_headers(self):
self.message.extra_headers = {'X-Custom': 'string', 'X-Num': 123}
self.message.extra_headers = {'X-Custom': 'string', 'X-Num': 123, 'X-Long': longtype(123)}
self.message.send()
data = self.get_api_call_data()
headers = json.loads(data['headers'])
self.assertEqual(headers['X-Custom'], 'string')
self.assertEqual(headers['X-Num'], '123') # number converted to string (per SendGrid requirement)
self.assertEqual(headers['X-Long'], '123') # number converted to string (per SendGrid requirement)
def test_extra_headers_serialization_error(self):
self.message.extra_headers = {'X-Custom': Decimal(12.5)}