Skip to content

Commit

Permalink
Model.verify: add error message for non-2xx redirects
Browse files Browse the repository at this point in the history
for #337
  • Loading branch information
snarfed committed Dec 7, 2022
1 parent 6c1736f commit f72b3ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def verify(self):
elif resp.url:
diff = '\n'.join(difflib.Differ().compare([resp.url], [expected[0]]))
self.redirects_error = f'Current vs expected:<pre>{diff}</pre>'
else:
lines = [url, f' returned HTTP {resp.status_code}']
if resp.url != url:
lines[1:1] = [' redirected to:', resp.url]
self.redirects_error = '\n'.join(lines)
except requests.RequestException:
pass

Expand Down
11 changes: 11 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ def test_verify_multiple_redirects(self, mock_get):
mock_get.side_effect = [two_redirs, no_hcard]
self._test_verify(True, False, None)

@mock.patch('requests.get')
def test_verify_redirect_404(self, mock_get):
redir_404 = requests_response(status=404, redirected_url='http://this/404s')
no_hcard = requests_response('<html><body></body></html>')
mock_get.side_effect = [redir_404, no_hcard]
self._test_verify(False, False, None, """\
https://y.z/.well-known/webfinger?resource=acct:y.z@y.z
redirected to:
http://this/404s
returned HTTP 404""")

@mock.patch('requests.get')
def test_verify_non_representative_hcard(self, mock_get):
full_redir = requests_response(
Expand Down

0 comments on commit f72b3ab

Please sign in to comment.