Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Fix test_connect_ipv6_addr. #75

Merged
merged 2 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/urllib3/_async/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,16 @@ def _build_tunnel_request(host, port, headers):
Builds a urllib3 Request object that is set up correctly to request a proxy
to establish a TCP tunnel to the remote host.
"""
target = "%s:%d" % (host, port)

try:
socket.inet_pton(socket.AF_INET6, host)
except OSError:
# Not a raw IPv6 address
target = "%s:%d" % (host, port)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably write it like:

    try:
        socket.inet_pton(socket.AF_INET6, host)
    except OSError:
        # Not a raw IPv6 address
        target = "%s:%d" % (host, port)
    else:
        # raw IPv6 address
        target = "[%s]:%d" % (host, port)

It doesn't make a big difference here, but it's a good habit in cases like this to only put the minimal stuff inside the try block, to make sure you don't catch any unexpected exceptions by accident.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks it seem more readable to me.

else:
# raw IPv6 address
target = "[%s]:%d" % (host, port)

if not isinstance(target, bytes):
target = target.encode('latin1')

Expand Down
1 change: 0 additions & 1 deletion test/with_dummyserver/test_socketlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,6 @@ def handler(listener):
assert exception.response.status_code == 401
assert exception.response.headers['x-custom-header'] == 'yougotit'

@pytest.mark.xfail
def test_connect_ipv6_addr(self):
ipv6_addr = '2001:4998:c:a06::2:4008'

Expand Down