Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit d0e2e5e

Browse files
Forward SSL status of upstream image URL to Photon (#1080)
1 parent f4e1f66 commit d0e2e5e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

api/catalog/api/utils/photon.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ def get(
5252
# request URL.
5353
params["q"] = parsed_image_url.query
5454

55+
if parsed_image_url.scheme == "https":
56+
# Photon defaults to HTTP without this parameter
57+
# which will cause some providers to fail (if they
58+
# do not serve over HTTP and do not have a redirect)
59+
params["ssl"] = "true"
60+
5561
# Photon excludes the protocol so we need to reconstruct the url + port + path
5662
# to send as the "path" of the Photon request
5763
domain = parsed_image_url.netloc

api/test/unit/utils/photon_test.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
PHOTON_URL_FOR_TEST_IMAGE = f"{settings.PHOTON_ENDPOINT}subdomain.example.com/path_part1/part2/image_dot_jpg.jpg"
14-
TEST_IMAGE_URL = PHOTON_URL_FOR_TEST_IMAGE.replace(settings.PHOTON_ENDPOINT, "https://")
14+
TEST_IMAGE_URL = PHOTON_URL_FOR_TEST_IMAGE.replace(settings.PHOTON_ENDPOINT, "http://")
1515

1616
UA_HEADER = HEADERS["User-Agent"]
1717

@@ -261,3 +261,29 @@ def test_get_generic_exception(capture_exception, setup_requests_get_exception):
261261
photon_get(TEST_IMAGE_URL)
262262

263263
capture_exception.assert_called_once_with(exc)
264+
265+
266+
@pook.on
267+
def test_get_successful_https_image_url_sends_ssl_parameter(mock_image_data):
268+
https_url = TEST_IMAGE_URL.replace("http://", "https://")
269+
mock_get: pook.Mock = (
270+
pook.get(PHOTON_URL_FOR_TEST_IMAGE)
271+
.params(
272+
{
273+
"w": settings.THUMBNAIL_WIDTH_PX,
274+
"quality": settings.THUMBNAIL_QUALITY,
275+
"ssl": "true",
276+
}
277+
)
278+
.header("User-Agent", UA_HEADER)
279+
.header("Accept", "image/*")
280+
.reply(200)
281+
.body(MOCK_BODY)
282+
.mock
283+
)
284+
285+
res = photon_get(https_url)
286+
287+
assert res.content == MOCK_BODY.encode()
288+
assert res.status_code == 200
289+
assert mock_get.matched

0 commit comments

Comments
 (0)