From a32224880171156020ebb3699547edd1f6654555 Mon Sep 17 00:00:00 2001 From: Timo Brembeck Date: Fri, 17 Nov 2023 00:51:37 +0100 Subject: [PATCH] Use requests_mock for punycode domain tests --- .github/workflows/test.yml | 1 + linkcheck/tests/test_linkcheck.py | 52 ++++++++++++++++++++----------- pyproject.toml | 1 + 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5f678b9..0d20328 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/linkcheck/tests/test_linkcheck.py b/linkcheck/tests/test_linkcheck.py index 329a7d9..c93499a 100644 --- a/linkcheck/tests/test_linkcheck.py +++ b/linkcheck/tests/test_linkcheck.py @@ -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 @@ -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 ( @@ -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(\": " + "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') @@ -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, '') diff --git a/pyproject.toml b/pyproject.toml index 760d33a..fd1d315 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,7 @@ dev = [ "flake8", "isort", "pre-commit", + "requests_mock", ] [tool.setuptools]