Skip to content

Commit cd47945

Browse files
Fix PURL resolution for redirected URLs
1 parent ffbd683 commit cd47945

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎src/fetchcode/utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def get_text_response(url, headers=None):
187187

188188
def make_head_request(url, headers=None):
189189
try:
190-
resp = requests.head(url, headers=headers)
190+
resp = requests.head(url, headers=headers, allow_redirects=True)
191191
return resp
192192
except requests.RequestException:
193193
raise Exception(f"Failed to fetch: {url}")

‎tests/test_fetch.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from fetchcode import fetch
2222
from fetchcode import resolve_purl
2323
from fetchcode import resolve_url_from_purl
24+
from fetchcode import utils
2425

2526

2627
@mock.patch("fetchcode.requests.get")
@@ -177,3 +178,19 @@ def test_resolve_purl_using_fetchcode_url_does_not_exists(
177178
mock_http_exists.return_value = False
178179
url = resolve_purl("pkg:pypi/example@1.0.0")
179180
assert url is None
181+
182+
183+
@mock.patch("fetchcode.utils.requests.head")
184+
def test_make_head_requests_follow_redirects(mock_head):
185+
response = mock.Mock()
186+
mock_head.return_value = response
187+
188+
url = "https://github.com/Alamofire/Alamofire/archive/5.4.3.zip"
189+
headers = {"Range": "bytes=0-0"}
190+
191+
result = utils.make_head_request(url, headers=headers)
192+
193+
assert result is response
194+
mock_head.assert_called_once_with(
195+
url, headers=headers, allow_redirects=True
196+
)

0 commit comments

Comments
 (0)