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

Commit

Permalink
Forward SSL status of upstream image URL to Photon (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend authored Jan 12, 2023
1 parent f4e1f66 commit d0e2e5e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/catalog/api/utils/photon.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def get(
# request URL.
params["q"] = parsed_image_url.query

if parsed_image_url.scheme == "https":
# Photon defaults to HTTP without this parameter
# which will cause some providers to fail (if they
# do not serve over HTTP and do not have a redirect)
params["ssl"] = "true"

# Photon excludes the protocol so we need to reconstruct the url + port + path
# to send as the "path" of the Photon request
domain = parsed_image_url.netloc
Expand Down
28 changes: 27 additions & 1 deletion api/test/unit/utils/photon_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


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

UA_HEADER = HEADERS["User-Agent"]

Expand Down Expand Up @@ -261,3 +261,29 @@ def test_get_generic_exception(capture_exception, setup_requests_get_exception):
photon_get(TEST_IMAGE_URL)

capture_exception.assert_called_once_with(exc)


@pook.on
def test_get_successful_https_image_url_sends_ssl_parameter(mock_image_data):
https_url = TEST_IMAGE_URL.replace("http://", "https://")
mock_get: pook.Mock = (
pook.get(PHOTON_URL_FOR_TEST_IMAGE)
.params(
{
"w": settings.THUMBNAIL_WIDTH_PX,
"quality": settings.THUMBNAIL_QUALITY,
"ssl": "true",
}
)
.header("User-Agent", UA_HEADER)
.header("Accept", "image/*")
.reply(200)
.body(MOCK_BODY)
.mock
)

res = photon_get(https_url)

assert res.content == MOCK_BODY.encode()
assert res.status_code == 200
assert mock_get.matched

0 comments on commit d0e2e5e

Please sign in to comment.