Skip to content

Commit

Permalink
Use requests_mock for punycode domain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timobrembeck authored and claudep committed Nov 17, 2023
1 parent 6a60a24 commit a322248
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
python -m pip install --upgrade pip
python -m pip install --upgrade django~=${{ matrix.django-version }}
python -m pip install --upgrade requests
python -m pip install --upgrade requests_mock
- name: Run tests
run: python runtests.py
52 changes: 34 additions & 18 deletions linkcheck/tests/test_linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from io import StringIO
from unittest.mock import patch

import requests_mock
from django.apps import apps
from django.conf import settings
from django.contrib.auth.models import User
Expand All @@ -11,6 +12,7 @@
from django.test import LiveServerTestCase, TestCase
from django.test.utils import override_settings
from django.urls import reverse
from requests.exceptions import ConnectionError

from linkcheck.linkcheck_settings import MAX_URL_LENGTH
from linkcheck.listeners import (
Expand Down Expand Up @@ -437,15 +439,25 @@ def test_external_check_404_incomplete_cert(self):
self.assertEqual(uv.redirect_to, '')
self.assertEqual(uv.type, 'external')

def test_external_check_unreachable(self):
uv = Url(url='https://invalid')
@requests_mock.Mocker()
def test_external_check_unreachable(self, mocker):
exc = ConnectionError(
"HTTPSConnectionPool(host='name-resolution-error.example.com', port=443): Max retries exceeded with url: / "
"(Caused by NameResolutionError(\"<urllib3.connection.HTTPSConnection object at 0xdeadbeef>: "
"Failed to resolve 'name-resolution-error.example.com' ([Errno -2] Name or service not known)\"))"
)
mocked_url = 'https://name-resolution-error.example.com/'
mocker.register_uri('HEAD', mocked_url, exc=exc),
uv = Url(url=mocked_url)
uv.check_url()
formatted_message = (
"Name Resolution Error: Failed to resolve 'name-resolution-error.example.com' "
"([Errno -2] Name or service not known)"
)
self.assertEqual(uv.message, formatted_message)
self.assertEqual(uv.get_message, formatted_message)
self.assertEqual(uv.error_message, formatted_message)
self.assertEqual(uv.status, False)
for attr in [uv.message, uv.get_message, uv.error_message]:
self.assertEqual(
attr,
"Name Resolution Error: Failed to resolve 'invalid' ([Errno -2] Name or service not known)",
)
self.assertEqual(uv.anchor_message, '')
self.assertEqual(uv.ssl_status, None)
self.assertEqual(uv.ssl_message, 'SSL certificate could not be checked')
Expand Down Expand Up @@ -484,27 +496,31 @@ def test_external_check_200_utf8_not_encoded(self):
self.assertEqual(uv.redirect_to, '')
self.assertEqual(uv.type, 'external')

def test_external_check_200_utf8_domain(self):
uv = Url(url='https://bafög.de/')
@requests_mock.Mocker()
def test_external_check_200_utf8_domain(self, mocker):
mocker.register_uri('HEAD', 'https://xn--utf8-test--z5a0txc.example.com/', reason='OK'),
uv = Url(url='https://utf8-test-äüö.example.com/')
uv.check_url()
self.assertEqual(uv.message, '200 OK')
self.assertEqual(uv.status, True)
self.assertEqual(uv.message, '302 Found')
self.assertEqual(uv.get_message, 'Working temporary redirect')
self.assertEqual(uv.get_message, 'Working external link')
self.assertEqual(uv.error_message, '')
self.assertEqual(uv.anchor_message, '')
self.assertEqual(uv.ssl_status, True)
self.assertEqual(uv.ssl_message, 'Valid SSL certificate')
self.assertEqual(uv.get_status_code_display(), '302 Found')
self.assertEqual(uv.get_redirect_status_code_display(), '200 OK')
self.assertEqual(uv.get_status_code_display(), '200 OK')
self.assertEqual(uv.get_redirect_status_code_display(), None)
self.assertEqual(uv.redirect_to, '')
self.assertEqual(uv.type, 'external')
# The actual redirect URL might be subject to change
self.assertNotEqual(uv.redirect_to, '')

def test_external_check_200_punycode_domain(self):
uv = Url(url='https://www.xn--jobbrse-stellenangebote-blc.de/')
@requests_mock.Mocker()
def test_external_check_200_punycode_domain(self, mocker):
punycode_domain = 'https://xn--utf8-test--z5a0txc.example.com/'
mocker.register_uri('HEAD', punycode_domain, reason='OK'),
uv = Url(url=punycode_domain)
uv.check_url()
self.assertEqual(uv.status, True)
self.assertEqual(uv.message, '200 OK')
self.assertEqual(uv.status, True)
self.assertEqual(uv.get_message, 'Working external link')
self.assertEqual(uv.error_message, '')
self.assertEqual(uv.anchor_message, '')
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dev = [
"flake8",
"isort",
"pre-commit",
"requests_mock",
]

[tool.setuptools]
Expand Down

0 comments on commit a322248

Please sign in to comment.