Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to match hostname with different case #369

Open
bignose-debian opened this issue Jan 17, 2019 · 2 comments
Open

Fails to match hostname with different case #369

bignose-debian opened this issue Jan 17, 2019 · 2 comments
Labels

Comments

@bignose-debian
Copy link

bignose-debian commented Jan 17, 2019

A request sent with Python's http.client.HTTPConnection is not mocked by HTTPretty.

(Python version 3.7.2; HTTPretty version 0.9.5)

Example (fragment; see the attached file for a full working example):
test_http_client.py.txt

    uri = urllib.parse.urlunsplit(url_parts)
    responses = [
        httpretty.Response(
            status=http.client.OK,
            body="Lorem ipsum, dolor sit amet.",
        ),
    ]
    httpretty.register_uri(httpretty.PUT, uri, responses=responses)

    connection = http.client.HTTPConnection(url_parts.netloc)
    connection.putrequest("PUT", uri)
    connection.putheader("Connection", "close")
    body_bytes = body.encode("utf-8")
    connection.putheader("Content-Length", len(body_bytes))
    connection.endheaders()
    connection.send(body_bytes)
    response = connection.getresponse()

The code fails when attempting to begin the request:

Traceback (most recent call last):
  File "test_http_client.py", line 35, in send_fake_put_request
    connection.endheaders()
  File "/usr/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/usr/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/usr/lib/python3.7/http/client.py", line 928, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

The HTTPretty library evidently has not intercepted the HTTPConnection calls that send the HTTP request. This seems to be a failure to mock the request correctly.

@bignose-debian bignose-debian changed the title Fails to mock request sent with HTTPConnection Fails to match hostname with different case Jan 19, 2019
@bignose-debian
Copy link
Author

bignose-debian commented Jan 19, 2019

This is caused by the domain match logic: It fails to ignore letter case for hostnames.

The following test case, added to tests/unit/test_httpretty.py, demonstrates the problem:

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)

The current HTTPretty.match_http_address function fails that test because it incorrectly considers hostnames with different letter case to be different hostnames.

@bignose-debian
Copy link
Author

The branch ‘wip/issue/369/match-uri-without-hostname-case’ (in my personal fork repository https://salsa.debian.org/bignose/httpretty/) implements the test case and a correction to fix this bug.

It currently merges cleanly to the ‘master’ branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants