diff --git a/adafruit_requests.py b/adafruit_requests.py index c21f69c..75b5a70 100644 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -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() @@ -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, diff --git a/tests/chunked_redirect_test.py b/tests/chunked_redirect_test.py index 871dc21..e0be03f 100644 --- a/tests/chunked_redirect_test.py +++ b/tests/chunked_redirect_test.py @@ -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)),) diff --git a/tests/header_test.py b/tests/header_test.py index 69c16c5..bbcc43c 100644 --- a/tests/header_test.py +++ b/tests/header_test.py @@ -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)