Skip to content

Commit 6fb5fec

Browse files
authored
Enable inlining of images with query string (#439)
1 parent 2e84d15 commit 6fb5fec

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

opwen_email_server/utils/email_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def ensure_has_sent_at(email: dict):
145145
def _get_image_type(response: Response, url: str) -> Optional[str]:
146146
content_type = response.headers.get('Content-Type')
147147
if not content_type:
148-
content_type = guess_type(url)[0]
148+
content_type = guess_type(url.split('?')[0])[0]
149149
return content_type
150150

151151

tests/opwen_email_server/utils/test_email_parser.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ def test_format_inline_images_with_img_tag(self):
139139

140140
self.assertStartsWith(output_email['body'], '<div><h3>test image</h3><img src="data:image/png;')
141141

142+
@mock_responses.activate
143+
def test_format_inline_images_with_query_string(self):
144+
url = 'http://test-url.png?foo=bar&baz=qux'
145+
self.givenTestImage(url=url, content_type='')
146+
input_email = {'body': f'<div><h3>test image</h3><img src="{url}"/></div>'}
147+
148+
output_email = email_parser.format_inline_images(input_email, self.fail_if_called)
149+
150+
self.assertStartsWith(output_email['body'], '<div><h3>test image</h3><img src="data:image/png;')
151+
142152
@mock_responses.activate
143153
@patch.object(email_parser, 'Image')
144154
def test_handles_exceptions_when_processing_image(self, mock_pil):
@@ -213,13 +223,13 @@ def assertHasCount(self, data, snippet, expected_count):
213223
f'times but got {actual_count}')
214224

215225
@classmethod
216-
def givenTestImage(cls, content_type='image/png', status=200):
226+
def givenTestImage(cls, content_type='image/png', status=200, url='http://test-url.png'):
217227
with open(join(TEST_DATA_DIRECTORY, 'test_image.png'), 'rb') as image:
218228
image_bytes = image.read()
219229

220230
mock_responses.add(
221231
mock_responses.GET,
222-
'http://test-url.png',
232+
url,
223233
content_type=content_type,
224234
body=image_bytes,
225235
status=status,

0 commit comments

Comments
 (0)