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
|
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
--- python-httpretty-0.9.5.orig/httpretty/core.py
+++ python-httpretty-0.9.5/httpretty/core.py
@@ -1071,6 +1071,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,
@@ -1078,14 +1079,14 @@ 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)
for pattern in [pattern_with_port, pattern_without_port]:
if matcher.regex.search(pattern) is not None \
or matcher.regex.pattern.startswith(pattern):
return matcher
- elif matcher.info.hostname == hostname:
+ elif (matcher.info.hostname.lower() == hostname_lower):
return matcher
return None
@@ -1096,6 +1097,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,
@@ -1108,14 +1110,14 @@ 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)
for pattern in [pattern_with_port, pattern_without_port]:
if matcher.regex.search(pattern_without_port) is not None \
or matcher.regex.pattern.startswith(pattern):
return matcher
- elif matcher.info.hostname == hostname \
+ elif matcher.info.hostname.lower() == hostname_lower \
and matcher.info.port == port:
return matcher
--- python-httpretty-0.9.5.orig/tests/unit/test_httpretty.py
+++ python-httpretty-0.9.5/tests/unit/test_httpretty.py
@@ -429,3 +429,25 @@ def test_socktype_good_python_version():
HTTPretty.enable()
expect(socket.SocketType).to.equal(socket.socket)
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)
+
+
|