Skip to content
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
6 changes: 4 additions & 2 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,10 @@ def request( # noqa: PLR0912,PLR0913,PLR0915 Too many branches,Too many argumen
raise ValueError("Unsupported protocol: " + proto)

if ":" in host:
host, port = host.split(":", 1)
host_only, port = host.split(":", 1)
port = int(port)
else:
host_only = host

if self._last_response:
self._last_response.close()
Expand All @@ -638,7 +640,7 @@ def request( # noqa: PLR0912,PLR0913,PLR0915 Too many branches,Too many argumen
while retry_count < 2:
retry_count += 1
socket = self._connection_manager.get_socket(
host,
host_only,
port,
proto,
session_id=self._session_id,
Expand Down
22 changes: 22 additions & 0 deletions tests/chunked_redirect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ def test_chunked_relative_absolute_redirect():
assert response.text == str(BODY, "utf-8")


def test_chunked_relative_absolute_redirect_with_port():
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 8443)),)
chunk = _chunk(BODY_REDIRECT, len(BODY_REDIRECT))
chunk2 = _chunk(BODY, len(BODY))

redirect = HEADERS_REDIRECT.replace(b"NEW_LOCATION_HERE", RELATIVE_ABSOLUTE_REDIRECT)
sock1 = MocketRecvInto(redirect + chunk)
sock2 = mocket.Mocket(HEADERS + chunk2)
pool.socket.side_effect = (sock1, sock2)

requests_session = adafruit_requests.Session(pool, mocket.SSLContext())
response = requests_session.get("https://" + HOST + ":8443" + PATH)

sock1.connect.assert_called_once_with((HOST, 8443))
sock2.connect.assert_called_once_with(("docs.google.com", 8443))
sock2.send.assert_has_calls([mock.call(RELATIVE_ABSOLUTE_REDIRECT[1:])])
assert b"Host: docs.google.com:8443\r\n" in b"".join(sock2.sent_data)

assert response.text == str(BODY, "utf-8")


def test_chunked_relative_relative_redirect():
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 443)),)
Expand Down
9 changes: 9 additions & 0 deletions tests/header_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def test_host_replace(sock, requests):
assert sent.lower().count(b"host:") == 1


def test_host_with_port(pool, sock, requests):
headers = {}
requests.get("http://" + mocket.MOCK_HOST_1 + ":8080/get", headers=headers)

pool.getaddrinfo.assert_called_once_with(mocket.MOCK_HOST_1, 8080, 0, pool.SOCK_STREAM)
sent = b"".join(sock.sent_data)
assert b"Host: wifitest.adafruit.com:8080\r\n" in sent


def test_user_agent(sock, requests):
headers = {}
requests.get("http://" + mocket.MOCK_HOST_1 + "/get", headers=headers)
Expand Down
Loading