forked from apify/apify-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proxy_configuration.py
More file actions
580 lines (435 loc) · 20.7 KB
/
test_proxy_configuration.py
File metadata and controls
580 lines (435 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# ruff: noqa: ARG001 ARG005
from __future__ import annotations
import asyncio
import re
from dataclasses import asdict
from typing import TYPE_CHECKING, Any
from unittest.mock import Mock
import pytest
from apify_client import ApifyClientAsync
from apify_shared.consts import ApifyEnvVars
from apify._proxy_configuration import ProxyConfiguration, is_url
if TYPE_CHECKING:
from pytest_httpserver import HTTPServer
from werkzeug import Request, Response
from .conftest import ApifyClientAsyncPatcher
DUMMY_PASSWORD = 'DUMMY_PASSWORD'
@pytest.fixture
def patched_apify_client(apify_client_async_patcher: ApifyClientAsyncPatcher) -> ApifyClientAsync:
apify_client_async_patcher.patch(
'user',
'get',
return_value={
'proxy': {
'password': DUMMY_PASSWORD,
},
},
)
return ApifyClientAsync()
def test_basic_constructor() -> None:
groups = ['GROUP1', 'GROUP2']
password = 'abcd1234'
country_code = 'US'
proxy_configuration = ProxyConfiguration(
groups=groups,
password=password,
country_code=country_code,
)
assert proxy_configuration._groups == groups
assert proxy_configuration._password == password
assert proxy_configuration._country_code == country_code
def test_fallback_constructor(monkeypatch: pytest.MonkeyPatch) -> None:
hostname = 'example.com'
password = 'abcd1234'
port = 1234
monkeypatch.setenv('APIFY_PROXY_HOSTNAME', hostname)
monkeypatch.setenv('APIFY_PROXY_PASSWORD', password)
monkeypatch.setenv('APIFY_PROXY_PORT', f'{port}')
proxy_configuration = ProxyConfiguration()
assert proxy_configuration._hostname == hostname
assert proxy_configuration._password == password
assert proxy_configuration._port == port
def test_invalid_arguments() -> None:
for invalid_groups, bad_group_index in [
(['abc', 'de-f', 'geh'], 1),
(['', 'def', 'geh'], 0),
(['abc', 'DEF', 'geh$'], 2),
([111, 'DEF', 'geh$'], 2),
]:
bad_group = str(invalid_groups[bad_group_index]) # type: ignore[index]
# Match the actual error message pattern that includes the value and argument name
match_pattern = f'Value {re.escape(bad_group)} of argument groups does not match pattern'
with pytest.raises(ValueError, match=match_pattern):
ProxyConfiguration(groups=invalid_groups) # type: ignore[arg-type]
for invalid_country_code in ['CZE', 'aa', 'DDDD', 1111]:
match_pattern = f'Value {re.escape(str(invalid_country_code))} of argument country_code does not match pattern'
with pytest.raises(ValueError, match=match_pattern):
ProxyConfiguration(country_code=invalid_country_code) # type: ignore[arg-type]
with pytest.raises(ValueError, match='Exactly one of .* must be specified'):
ProxyConfiguration(
proxy_urls=['http://proxy.com:1111'],
new_url_function=lambda session_id=None, request=None: 'http://proxy.com:2222',
)
with pytest.raises(ValueError, match='Cannot combine custom proxies with Apify Proxy'):
ProxyConfiguration(proxy_urls=['http://proxy.com:1111'], groups=['GROUP1'])
with pytest.raises(ValueError, match=re.escape('bad-url')):
ProxyConfiguration(proxy_urls=['bad-url'])
with pytest.raises(ValueError, match='Cannot combine custom proxies with Apify Proxy'):
ProxyConfiguration(
new_url_function=lambda session_id=None, request=None: 'http://proxy.com:2222', groups=['GROUP1']
)
async def test_new_url_basic() -> None:
groups = ['GROUP1', 'GROUP2']
password = 'abcd1234'
country_code = 'US'
proxy_configuration = ProxyConfiguration(
groups=groups,
password=password,
country_code=country_code,
)
proxy_url = await proxy_configuration.new_url()
expected_username = f'groups-{"+".join(groups)},country-{country_code}'
expected_hostname = 'proxy.apify.com'
expected_port = 8000
assert proxy_url == f'http://{expected_username}:{password}@{expected_hostname}:{expected_port}'
async def test_new_url_with_session_ids() -> None:
groups = ['GROUP1', 'GROUP2']
password = 'abcd1234'
country_code = 'US'
proxy_configuration = ProxyConfiguration(
groups=groups,
password=password,
country_code=country_code,
)
session_ids: list[str] = [
'a',
'a_b',
'a_2',
'a_1_b',
'aaa~BBB',
'1',
'0.34252352',
'123456',
'XXXXXXXXXXxxxxxxxxxxXXXXXXXXXXxxxxxxxxxxXXXXXXXXXX',
]
for session_id in session_ids:
expected_username = f'groups-{"+".join(groups)},session-{session_id},country-{country_code}'
expected_hostname = 'proxy.apify.com'
expected_port = 8000
proxy_url = await proxy_configuration.new_url(session_id)
assert proxy_url == f'http://{expected_username}:{password}@{expected_hostname}:{expected_port}'
for invalid_session_id in ['a-b', 'a$b', 'XXXXXXXXXXxxxxxxxxxxXXXXXXXXXXxxxxxxxxxxXXXXXXXXXXTooLong']:
with pytest.raises(ValueError, match=re.escape(str(invalid_session_id))):
await proxy_configuration.new_url(invalid_session_id)
async def test_rotating_custom_urls() -> None:
proxy_urls: list[str | None] = ['http://proxy.com:1111', 'http://proxy.com:2222', 'http://proxy.com:3333']
proxy_configuration = ProxyConfiguration(proxy_urls=proxy_urls)
assert await proxy_configuration.new_url() == proxy_urls[0]
assert await proxy_configuration.new_url() == proxy_urls[1]
assert await proxy_configuration.new_url() == proxy_urls[2]
assert await proxy_configuration.new_url() == proxy_urls[0]
assert await proxy_configuration.new_url() == proxy_urls[1]
assert await proxy_configuration.new_url() == proxy_urls[2]
async def test_rotating_custom_urls_with_sessions() -> None:
sessions = ['sesssion_01', 'sesssion_02', 'sesssion_03', 'sesssion_04', 'sesssion_05', 'sesssion_06']
proxy_urls: list[str | None] = ['http://proxy.com:1111', 'http://proxy.com:2222', 'http://proxy.com:3333']
proxy_configuration = ProxyConfiguration(proxy_urls=proxy_urls)
# same session should use same proxy URL
assert await proxy_configuration.new_url(sessions[0]) == proxy_urls[0]
assert await proxy_configuration.new_url(sessions[0]) == proxy_urls[0]
assert await proxy_configuration.new_url(sessions[0]) == proxy_urls[0]
# different sessions should rotate different proxies
assert await proxy_configuration.new_url(sessions[1]) == proxy_urls[1]
assert await proxy_configuration.new_url(sessions[2]) == proxy_urls[2]
assert await proxy_configuration.new_url(sessions[3]) == proxy_urls[0]
assert await proxy_configuration.new_url(sessions[4]) == proxy_urls[1]
assert await proxy_configuration.new_url(sessions[5]) == proxy_urls[2]
# already used sessions should be remembered
assert await proxy_configuration.new_url(sessions[1]) == proxy_urls[1]
assert await proxy_configuration.new_url(sessions[3]) == proxy_urls[0]
async def test_with_custom_new_url_function() -> None:
custom_urls = [
'http://proxy.com:1111',
'http://proxy.com:2222',
'http://proxy.com:3333',
'http://proxy.com:4444',
'http://proxy.com:5555',
'http://proxy.com:6666',
]
def custom_new_url_function(session_id: str | None = None, request: Any = None) -> str:
nonlocal custom_urls
return custom_urls.pop()
proxy_configuration = ProxyConfiguration(new_url_function=custom_new_url_function)
for custom_url in reversed(custom_urls):
assert await proxy_configuration.new_url() == custom_url
async def test_with_async_custom_new_url_function() -> None:
custom_urls = [
'http://proxy.com:1111',
'http://proxy.com:2222',
'http://proxy.com:3333',
'http://proxy.com:4444',
'http://proxy.com:5555',
'http://proxy.com:6666',
]
async def custom_new_url_function(session_id: str | None = None, request: Any = None) -> str:
nonlocal custom_urls
await asyncio.sleep(0.1)
return custom_urls.pop()
proxy_configuration = ProxyConfiguration(new_url_function=custom_new_url_function)
for custom_url in reversed(custom_urls):
assert await proxy_configuration.new_url() == custom_url
async def test_invalid_custom_new_url_function() -> None:
def custom_new_url_function(session_id: str | None = None, request: Any = None) -> str:
raise ValueError
proxy_configuration = ProxyConfiguration(new_url_function=custom_new_url_function)
with pytest.raises(ValueError, match='The provided "new_url_function" did not return a valid URL'):
await proxy_configuration.new_url()
async def test_url_reference_not_shared_between_instances() -> None:
proxy_urls: list[str | None] = [
'http://proxy-example-1.com:8000',
'http://proxy-example-2.com:8000',
]
proxy_configuration_1 = ProxyConfiguration(proxy_urls=proxy_urls)
proxy_urls.append('http://proxy-example-3.com:8000')
proxy_configuration_2 = ProxyConfiguration(proxy_urls=proxy_urls)
assert proxy_configuration_1 is not None
assert proxy_configuration_2 is not None
assert proxy_configuration_1._proxy_urls is not proxy_configuration_2._proxy_urls
session_id = 'ABCD'
await proxy_configuration_1.new_url(session_id=session_id)
await proxy_configuration_2.new_url(session_id=session_id)
assert proxy_configuration_1._used_proxy_urls is not proxy_configuration_2._used_proxy_urls
async def test_new_proxy_info_basic_construction() -> None:
groups = ['GROUP1', 'GROUP2']
password = 'abcd1234'
country_code = 'US'
proxy_configuration = ProxyConfiguration(
groups=groups,
password=password,
country_code=country_code,
)
proxy_info = await proxy_configuration.new_proxy_info()
assert proxy_info is not None
expected_hostname = 'proxy.apify.com'
expected_port = 8000
expected_username = f'groups-{"+".join(groups)},country-{country_code}'
assert asdict(proxy_info) == {
'url': f'http://{expected_username}:{password}@{expected_hostname}:{expected_port}',
'hostname': expected_hostname,
'port': expected_port,
'groups': groups,
'country_code': country_code,
'username': expected_username,
'password': password,
'proxy_tier': None,
'session_id': None,
'scheme': 'http',
}
async def test_new_proxy_info_rotating_urls() -> None:
proxy_urls: list[str | None] = ['http://proxy.com:1111', 'http://proxy.com:2222', 'http://proxy.com:3333']
proxy_configuration = ProxyConfiguration(proxy_urls=proxy_urls)
proxy_info = await proxy_configuration.new_proxy_info()
assert proxy_info is not None
assert proxy_info.url == proxy_urls[0]
proxy_info = await proxy_configuration.new_proxy_info()
assert proxy_info is not None
assert proxy_info.url == proxy_urls[1]
proxy_info = await proxy_configuration.new_proxy_info()
assert proxy_info is not None
assert proxy_info.url == proxy_urls[2]
proxy_info = await proxy_configuration.new_proxy_info()
assert proxy_info is not None
assert proxy_info.url == proxy_urls[0]
proxy_info = await proxy_configuration.new_proxy_info()
assert proxy_info is not None
assert proxy_info.url == proxy_urls[1]
proxy_info = await proxy_configuration.new_proxy_info()
assert proxy_info is not None
assert proxy_info.url == proxy_urls[2]
async def test_new_proxy_info_rotating_urls_with_sessions() -> None:
sessions = ['sesssion_01', 'sesssion_02', 'sesssion_03', 'sesssion_04', 'sesssion_05', 'sesssion_06']
proxy_urls: list[str | None] = ['http://proxy.com:1111', 'http://proxy.com:2222', 'http://proxy.com:3333']
proxy_configuration = ProxyConfiguration(proxy_urls=proxy_urls)
# same session should use same proxy URL
proxy_info = await proxy_configuration.new_proxy_info(sessions[0])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[0]
proxy_info = await proxy_configuration.new_proxy_info(sessions[0])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[0]
proxy_info = await proxy_configuration.new_proxy_info(sessions[0])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[0]
# different sessions should rotate different proxies
proxy_info = await proxy_configuration.new_proxy_info(sessions[1])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[1]
proxy_info = await proxy_configuration.new_proxy_info(sessions[2])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[2]
proxy_info = await proxy_configuration.new_proxy_info(sessions[3])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[0]
proxy_info = await proxy_configuration.new_proxy_info(sessions[4])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[1]
proxy_info = await proxy_configuration.new_proxy_info(sessions[5])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[2]
# already used sessions should be remembered
proxy_info = await proxy_configuration.new_proxy_info(sessions[1])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[1]
proxy_info = await proxy_configuration.new_proxy_info(sessions[3])
assert proxy_info is not None
assert proxy_info.url == proxy_urls[0]
@pytest.mark.usefixtures('patched_impit_client')
async def test_initialize_with_valid_configuration(
monkeypatch: pytest.MonkeyPatch,
httpserver: HTTPServer,
patched_apify_client: ApifyClientAsync,
) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
monkeypatch.setenv(ApifyEnvVars.TOKEN.value, 'DUMMY_TOKEN')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
call_mock = Mock()
def request_handler(request: Request, response: Response) -> Response:
call_mock(request.url)
return response
httpserver.expect_oneshot_request('/').with_post_hook(request_handler).respond_with_json(
{
'connected': True,
'connectionError': None,
'isManInTheMiddle': True,
},
status=200,
)
proxy_configuration = ProxyConfiguration(_apify_client=patched_apify_client)
await proxy_configuration.initialize()
assert proxy_configuration._password == DUMMY_PASSWORD
assert proxy_configuration.is_man_in_the_middle is True
assert len(patched_apify_client.calls['user']['get']) == 1 # type: ignore[attr-defined]
assert call_mock.call_count == 1
async def test_initialize_without_password_or_token() -> None:
proxy_configuration = ProxyConfiguration()
with pytest.raises(ValueError, match='Apify Proxy password must be provided'):
await proxy_configuration.initialize()
@pytest.mark.usefixtures('patched_impit_client')
async def test_initialize_with_manual_password(monkeypatch: pytest.MonkeyPatch, httpserver: HTTPServer) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
httpserver.expect_oneshot_request('/').respond_with_json(
{
'connected': True,
'connectionError': None,
'isManInTheMiddle': False,
},
status=200,
)
proxy_configuration = ProxyConfiguration(password=DUMMY_PASSWORD)
await proxy_configuration.initialize()
assert proxy_configuration._password == DUMMY_PASSWORD
assert proxy_configuration.is_man_in_the_middle is False
@pytest.mark.usefixtures('patched_impit_client')
async def test_initialize_prefering_password_from_env_over_calling_api(
monkeypatch: pytest.MonkeyPatch,
httpserver: HTTPServer,
patched_apify_client: ApifyClientAsync,
) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
monkeypatch.setenv(ApifyEnvVars.PROXY_PASSWORD.value, DUMMY_PASSWORD)
httpserver.expect_oneshot_request('/').respond_with_json(
{
'connected': True,
'connectionError': None,
'isManInTheMiddle': False,
},
status=200,
)
proxy_configuration = ProxyConfiguration()
await proxy_configuration.initialize()
assert proxy_configuration._password == DUMMY_PASSWORD
assert proxy_configuration.is_man_in_the_middle is False
assert len(patched_apify_client.calls['user']['get']) == 0 # type: ignore[attr-defined]
@pytest.mark.usefixtures('patched_impit_client')
async def test_initialize_with_manual_password_different_than_user_one(
monkeypatch: pytest.MonkeyPatch,
httpserver: HTTPServer,
patched_apify_client: ApifyClientAsync,
) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
different_dummy_password = 'DIFFERENT_DUMMY_PASSWORD'
monkeypatch.setenv(ApifyEnvVars.TOKEN.value, 'DUMMY_TOKEN')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
monkeypatch.setenv(ApifyEnvVars.PROXY_PASSWORD.value, different_dummy_password)
httpserver.expect_oneshot_request('/').respond_with_json(
{
'connected': True,
'connectionError': None,
'isManInTheMiddle': True,
},
status=200,
)
proxy_configuration = ProxyConfiguration(_apify_client=patched_apify_client)
await proxy_configuration.initialize()
assert proxy_configuration._password == different_dummy_password
assert proxy_configuration.is_man_in_the_middle is True
@pytest.mark.usefixtures('patched_impit_client')
async def test_initialize_when_not_connected(monkeypatch: pytest.MonkeyPatch, httpserver: HTTPServer) -> None:
dummy_connection_error = 'DUMMY_CONNECTION_ERROR'
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
httpserver.expect_oneshot_request('/').respond_with_json(
{
'connected': False,
'connectionError': dummy_connection_error,
},
status=200,
)
proxy_configuration = ProxyConfiguration(password=DUMMY_PASSWORD)
with pytest.raises(ConnectionError, match=dummy_connection_error):
await proxy_configuration.initialize()
async def test_initialize_when_status_page_unavailable(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, httpserver: HTTPServer
) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
httpserver.expect_oneshot_request('/').respond_with_data(status=500)
proxy_configuration = ProxyConfiguration(password=DUMMY_PASSWORD)
await proxy_configuration.initialize()
assert len(caplog.records) == 1
assert caplog.records[0].levelname == 'WARNING'
assert 'Apify Proxy access check timed out' in caplog.records[0].message
async def test_initialize_with_non_apify_proxy(
monkeypatch: pytest.MonkeyPatch,
httpserver: HTTPServer,
patched_apify_client: ApifyClientAsync,
) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
call_mock = Mock()
def request_handler(request: Request, response: Response) -> Response:
call_mock(request.url)
return response
httpserver.expect_oneshot_request('/').with_post_hook(request_handler).respond_with_data(status=200)
proxy_configuration = ProxyConfiguration(proxy_urls=['http://dummy-proxy.com:8000'])
await proxy_configuration.initialize()
assert len(patched_apify_client.calls['user']['get']) == 0 # type: ignore[attr-defined]
assert call_mock.call_count == 0
def test_is_url_validation() -> None:
assert is_url('http://dummy-proxy.com:8000') is True
assert is_url('https://example.com') is True
assert is_url('http://localhost') is True
assert is_url('https://12.34.56.78') is True
assert is_url('http://12.34.56.78:9012') is True
assert is_url('http://::1') is True
assert is_url('https://2f45:4da6:8f56:af8c:5dce:c1de:14d2:8661') is True
assert is_url('dummy-proxy.com:8000') is False
assert is_url('gyfwgfhkjhljkfhdsf') is False
assert is_url('http://') is False
assert is_url('http://example') is False
assert is_url('http:/example.com') is False
assert is_url('12.34.56.78') is False
assert is_url('::1') is False
assert is_url('https://4da6:8f56:af8c:5dce:c1de:14d2:8661') is False