1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
Description: Match URI without letter case in hostname.
The URI matching should ignore the hostname letter case, so that
intermediate “normalising” of the hostname does not affect whether
the URI matches.
Bug: https://github.com/gabrielfalcao/HTTPretty/issues/369
Bug-Debian: http://bugs.debian.org/919599
Author: Ben Finney <bignose@debian.org>
Co-Author: Thomas Goirand <zigo@debian.org>
Forwarded: https://github.com/gabrielfalcao/HTTPretty/issues/369#issuecomment-455744083
Last-Update: 2019-01-21
Index: python-httpretty/httpretty/core.py
===================================================================
--- python-httpretty.orig/httpretty/core.py
+++ python-httpretty/httpretty/core.py
@@ -1425,6 +1425,7 @@ class httpretty(HttpBaseClass):
:param hostname: a string
:returns: an :py:class:`~httpretty.core.URLMatcher` or ``None``
"""
+ hostname_lower = hostname.lower()
items = sorted(
cls._entries.items(),
key=lambda matcher_entries: matcher_entries[0].priority,
@@ -1432,8 +1433,8 @@ class httpretty(HttpBaseClass):
)
for matcher, value in items:
if matcher.info is None:
- pattern_with_port = "https://{0}:".format(hostname)
- pattern_without_port = "https://{0}/".format(hostname)
+ pattern_with_port = "https://{0}:".format(hostname_lower)
+ pattern_without_port = "https://{0}/".format(hostname_lower)
hostname_pattern = (
hostname_re
.match(matcher.regex.pattern)
@@ -1443,7 +1444,7 @@ class httpretty(HttpBaseClass):
if re.match(hostname_pattern, pattern):
return matcher
- elif matcher.info.hostname == hostname:
+ elif (matcher.info.hostname.lower() == hostname_lower):
return matcher
return None
@@ -1454,6 +1455,7 @@ class httpretty(HttpBaseClass):
:param port: an integer
:returns: an :py:class:`~httpretty.core.URLMatcher` or ``None``
"""
+ hostname_lower = hostname.lower()
items = sorted(
cls._entries.items(),
key=lambda matcher_entries: matcher_entries[0].priority,
@@ -1466,8 +1468,8 @@ class httpretty(HttpBaseClass):
else:
scheme = 'http://'
- pattern_without_port = "{0}{1}/".format(scheme, hostname)
- pattern_with_port = "{0}{1}:{2}/".format(scheme, hostname, port)
+ pattern_without_port = "{0}{1}/".format(scheme, hostname_lower)
+ pattern_with_port = "{0}{1}:{2}/".format(scheme, hostname_lower, port)
hostname_pattern = (
hostname_re
.match(matcher.regex.pattern)
@@ -1477,7 +1479,7 @@ class httpretty(HttpBaseClass):
if re.match(hostname_pattern, pattern):
return matcher
- elif matcher.info.hostname == hostname \
+ elif matcher.info.hostname.lower() == hostname_lower \
and matcher.info.port == port:
return matcher
Index: python-httpretty/tests/unit/test_httpretty.py
===================================================================
--- python-httpretty.orig/tests/unit/test_httpretty.py
+++ python-httpretty/tests/unit/test_httpretty.py
@@ -387,6 +387,26 @@ def test_socktype_good_python_version():
HTTPretty.disable()
+def test_match_http_address_should_ignore_hostname_case():
+ "HTTPretty.match_http_address should ignore case of hostname."
+
+ for (register_hostname, match_hostname) in [
+ ('foo.example.com', 'foo.example.com'),
+ ('FOO.example.COM', 'foo.example.com'),
+ ('foo.EXAMPLE.com', 'foo.example.com'),
+ ('fOo.eXaMpLe.com', 'foo.example.com'),
+ ('foo.example.com', 'FOO.example.COM'),
+ ('foo.example.com', 'foo.EXAMPLE.com'),
+ ('foo.example.com', 'fOo.eXaMpLe.com'),
+ ]:
+ HTTPretty.register_uri(
+ HTTPretty.GET,
+ "http://{hostname}/".format(hostname=register_hostname),
+ body="yay",
+ )
+ assert HTTPretty.match_http_address(match_hostname, 80)
+
+
def test_httpretty_should_allow_registering_regex_hostnames():
"HTTPretty should allow registering regexes with requests"
|