linkchecker/tests/checker/test_http_misc.py
Antoine Beaupré ab7502b6ff
make tests pass on IPv6 hosts
Without this patch, tests would fail on IPv6 hosts with this
mysterious error:

```
_______________________________________________________________________ TestHttpMisc.test_html ________________________________________________________________________
tests/checker/test_http_misc.py:30: in test_html
    self.obfuscate_test()
tests/checker/test_http_misc.py:51: in obfuscate_test
    url = u"http://%s/" % iputil.obfuscate_ip(ip)
linkcheck/network/iputil.py:290: in obfuscate_ip
    raise ValueError('Invalid IP value %r' % ip)
E   ValueError: Invalid IP value '2a02:2e0:3fe:1001:7777:772e:2:85'
```

As it turns out, the test host (`www.heise.de`) does have an IPv6
record and our tests pass on Travis only because they do not have a
working IPv6 stack. I happen to have IPv6 at home and tests are broken
here, so add a quick workaround so tests pass again.

Ideally, we would not have to deal with this hack and would handle
"obfuscation" correctly, but I have yet to figure out what that test
actually does before fixing it properly.
2018-04-11 19:42:30 -04:00

66 lines
2.3 KiB
Python

# -*- coding: iso-8859-1 -*-
# Copyright (C) 2004-2014 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Test http checking.
"""
import os
import sys
from .httpserver import HttpServerTest
from linkcheck.network import iputil
class TestHttpMisc (HttpServerTest):
"""Test http:// misc link checking."""
def test_html (self):
self.swf_test()
self.obfuscate_test()
def swf_test (self):
url = self.get_url(u"test.swf")
resultlines = [
u"url %s" % url,
u"cache key %s" % url,
u"real url %s" % url,
u"valid",
u"url http://www.example.org/",
u"cache key http://www.example.org/",
u"real url http://www.example.org/",
u"valid",
]
self.direct(url, resultlines, recursionlevel=1)
def obfuscate_test (self):
if os.name != "posix" or sys.platform != 'linux2':
return
host = "www.heise.de"
for tentative_ip in iputil.resolve_host(host):
if iputil.is_valid_ipv4(tentative_ip):
ip = tentative_ip
break
else:
raise NotImplementedError("no IPv4 address detected for test host")
url = u"http://%s/" % iputil.obfuscate_ip(ip)
rurl = u"http://%s/" % ip
resultlines = [
u"url %s" % url,
u"cache key %s" % rurl,
u"real url %s" % rurl,
u"info Access denied by robots.txt, checked only syntax.",
u"warning URL %s has obfuscated IP address %s" % (url, ip),
u"valid",
]
self.direct(url, resultlines, recursionlevel=0)