Debian Codegraph @

test/unit/selenium/webdriver/remote/remote_connection_tests.py

raw symbols SCIP
1 # Licensed to the Software Freedom Conservancy (SFC) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The SFC licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18 from urllib import parse
19
20 import pytest
21 import urllib3
22
23 from selenium import __version__
24 from selenium.webdriver.remote.remote_connection import RemoteConnection
25
26
27 def test_get_remote_connection_headers_defaults():
28 url = "http://remote"
29 headers = RemoteConnection.get_remote_connection_headers(parse.urlparse(url))
30 assert "Authorization" not in headers
31 assert "Connection" not in headers
32 assert headers.get("Accept") == "application/json"
33 assert headers.get("Content-Type") == "application/json;charset=UTF-8"
34 assert headers.get("User-Agent").startswith(f"selenium/{__version__} (python ")
35 assert headers.get("User-Agent").split(" ")[-1] in {"windows)", "mac)", "linux)"}
36
37
38 def test_get_remote_connection_headers_adds_auth_header_if_pass():
39 url = "http://user:pass@remote"
40 headers = RemoteConnection.get_remote_connection_headers(parse.urlparse(url))
41 assert headers.get("Authorization") == "Basic dXNlcjpwYXNz"
42
43
44 def test_get_remote_connection_headers_adds_keep_alive_if_requested():
45 url = "http://remote"
46 headers = RemoteConnection.get_remote_connection_headers(parse.urlparse(url), keep_alive=True)
47 assert headers.get("Connection") == "keep-alive"
48
49
50 def test_get_proxy_url_http(mock_proxy_settings):
51 proxy = "http://http_proxy.com:8080"
52 remote_connection = RemoteConnection("http://remote", keep_alive=False)
53 proxy_url = remote_connection._get_proxy_url()
54 assert proxy_url == proxy
55
56
57 def test_get_proxy_url_https(mock_proxy_settings):
58 proxy = "http://https_proxy.com:8080"
59 remote_connection = RemoteConnection("https://remote", keep_alive=False)
60 proxy_url = remote_connection._get_proxy_url()
61 assert proxy_url == proxy
62
63
64 def test_get_proxy_url_none(mock_proxy_settings_missing):
65 remote_connection = RemoteConnection("https://remote", keep_alive=False)
66 proxy_url = remote_connection._get_proxy_url()
67 assert proxy_url is None
68
69
70 def test_get_proxy_url_http_auth(mock_proxy_auth_settings):
71 remote_connection = RemoteConnection("http://remote", keep_alive=False)
72 proxy_url = remote_connection._get_proxy_url()
73 raw_proxy_url, basic_auth_string = remote_connection._separate_http_proxy_auth()
74 assert proxy_url == "http://user:password@http_proxy.com:8080"
75 assert raw_proxy_url == "http://http_proxy.com:8080"
76 assert basic_auth_string == "user:password"
77
78
79 def test_get_proxy_url_https_auth(mock_proxy_auth_settings):
80 remote_connection = RemoteConnection("https://remote", keep_alive=False)
81 proxy_url = remote_connection._get_proxy_url()
82 raw_proxy_url, basic_auth_string = remote_connection._separate_http_proxy_auth()
83 assert proxy_url == "https://user:password@https_proxy.com:8080"
84 assert raw_proxy_url == "https://https_proxy.com:8080"
85 assert basic_auth_string == "user:password"
86
87
88 def test_get_connection_manager_without_proxy(mock_proxy_settings_missing):
89 remote_connection = RemoteConnection("http://remote", keep_alive=False)
90 conn = remote_connection._get_connection_manager()
91 assert isinstance(conn, urllib3.PoolManager)
92
93
94 def test_get_connection_manager_for_certs_and_timeout(monkeypatch):
95 monkeypatch.setattr(RemoteConnection, "get_timeout", lambda _: 10) # Class state; leaks into subsequent tests.
96 remote_connection = RemoteConnection("http://remote", keep_alive=False)
97 conn = remote_connection._get_connection_manager()
98 assert conn.connection_pool_kw["timeout"] == 10
99 assert conn.connection_pool_kw["cert_reqs"] == "CERT_REQUIRED"
100 assert "certifi/cacert.pem" in conn.connection_pool_kw["ca_certs"]
101
102
103 def test_default_socket_timeout_is_correct():
104 remote_connection = RemoteConnection("http://remote", keep_alive=True)
105 conn = remote_connection._get_connection_manager()
106 assert conn.connection_pool_kw["timeout"] is None
107
108
109 def test_get_connection_manager_with_proxy(mock_proxy_settings):
110 remote_connection = RemoteConnection("http://remote", keep_alive=False)
111 conn = remote_connection._get_connection_manager()
112 assert isinstance(conn, urllib3.ProxyManager)
113 assert conn.proxy.scheme == "http"
114 assert conn.proxy.host == "http_proxy.com"
115 assert conn.proxy.port == 8080
116 remote_connection_https = RemoteConnection("https://remote", keep_alive=False)
117 conn = remote_connection_https._get_connection_manager()
118 assert isinstance(conn, urllib3.ProxyManager)
119 assert conn.proxy.scheme == "http"
120 assert conn.proxy.host == "https_proxy.com"
121 assert conn.proxy.port == 8080
122
123
124 def test_get_connection_manager_with_auth_proxy(mock_proxy_auth_settings):
125 proxy_auth_header = urllib3.make_headers(proxy_basic_auth="user:password")
126 remote_connection = RemoteConnection("http://remote", keep_alive=False)
127 conn = remote_connection._get_connection_manager()
128 assert isinstance(conn, urllib3.ProxyManager)
129 assert conn.proxy.scheme == "http"
130 assert conn.proxy.host == "http_proxy.com"
131 assert conn.proxy.port == 8080
132 assert conn.proxy_headers == proxy_auth_header
133 remote_connection_https = RemoteConnection("https://remote", keep_alive=False)
134 conn = remote_connection_https._get_connection_manager()
135 assert isinstance(conn, urllib3.ProxyManager)
136 assert conn.proxy.scheme == "https"
137 assert conn.proxy.host == "https_proxy.com"
138 assert conn.proxy.port == 8080
139 assert conn.proxy_headers == proxy_auth_header
140
141
142 @pytest.mark.parametrize(
143 "url",
144 [
145 "*",
146 ".localhost",
147 "localhost:80",
148 "locahost",
149 "127.0.0.1",
150 "LOCALHOST",
151 "LOCALHOST:80",
152 "http://localhost",
153 "https://localhost",
154 "test.localhost",
155 " localhost",
156 "::1",
157 "127.0.0.2",
158 ],
159 )
160 def test_get_connection_manager_when_no_proxy_set(mock_no_proxy_settings, url):
161 remote_connection = RemoteConnection(url)
162 conn = remote_connection._get_connection_manager()
163 assert isinstance(conn, urllib3.PoolManager)
164
165
166 def test_ignore_proxy_env_vars(mock_proxy_settings):
167 remote_connection = RemoteConnection("http://remote", ignore_proxy=True)
168 conn = remote_connection._get_connection_manager()
169 assert isinstance(conn, urllib3.PoolManager)
170
171
172 def test_get_socks_proxy_when_set(mock_socks_proxy_settings):
173 remote_connection = RemoteConnection("http://127.0.0.1:4444/wd/hub")
174 conn = remote_connection._get_connection_manager()
175 from urllib3.contrib.socks import SOCKSProxyManager
176
177 assert isinstance(conn, SOCKSProxyManager)
178
179
180 class MockResponse:
181 code = 200
182 headers = []
183
184 def read(self):
185 return b"{}"
186
187 def close(self):
188 pass
189
190 def getheader(self, *args, **kwargs):
191 pass
192
193
194 @pytest.fixture(scope="function")
195 def mock_proxy_settings_missing(monkeypatch):
196 monkeypatch.delenv("HTTPS_PROXY", raising=False)
197 monkeypatch.delenv("HTTP_PROXY", raising=False)
198 monkeypatch.delenv("https_proxy", raising=False)
199 monkeypatch.delenv("http_proxy", raising=False)
200
201
202 @pytest.fixture(scope="function")
203 def mock_socks_proxy_settings(monkeypatch):
204 http_proxy = "SOCKS5://http_proxy.com:8080"
205 https_proxy = "SOCKS5://https_proxy.com:8080"
206 monkeypatch.setenv("HTTPS_PROXY", https_proxy)
207 monkeypatch.setenv("HTTP_PROXY", http_proxy)
208 monkeypatch.setenv("https_proxy", https_proxy)
209 monkeypatch.setenv("http_proxy", http_proxy)
210
211
212 @pytest.fixture(scope="function")
213 def mock_proxy_settings(monkeypatch):
214 http_proxy = "http://http_proxy.com:8080"
215 https_proxy = "http://https_proxy.com:8080"
216 monkeypatch.setenv("HTTPS_PROXY", https_proxy)
217 monkeypatch.setenv("HTTP_PROXY", http_proxy)
218 monkeypatch.setenv("https_proxy", https_proxy)
219 monkeypatch.setenv("http_proxy", http_proxy)
220
221
222 @pytest.fixture(scope="function")
223 def mock_proxy_auth_settings(monkeypatch):
224 http_proxy = "http://user:password@http_proxy.com:8080"
225 https_proxy = "https://user:password@https_proxy.com:8080"
226 monkeypatch.setenv("HTTPS_PROXY", https_proxy)
227 monkeypatch.setenv("HTTP_PROXY", http_proxy)
228 monkeypatch.setenv("https_proxy", https_proxy)
229 monkeypatch.setenv("http_proxy", http_proxy)
230
231
232 @pytest.fixture(scope="function")
233 def mock_no_proxy_settings(monkeypatch):
234 http_proxy = "http://http_proxy.com:8080"
235 https_proxy = "http://https_proxy.com:8080"
236 monkeypatch.setenv("HTTPS_PROXY", https_proxy)
237 monkeypatch.setenv("HTTP_PROXY", http_proxy)
238 monkeypatch.setenv("https_proxy", https_proxy)
239 monkeypatch.setenv("http_proxy", http_proxy)
240 monkeypatch.setenv("no_proxy", "65.253.214.253,localhost,127.0.0.1,*zyz.xx,::1")
241 monkeypatch.setenv("NO_PROXY", "65.253.214.253,localhost,127.0.0.1,*zyz.xx,::1,127.0.0.0/8")