forked from apify/apify-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client_headers.py
More file actions
119 lines (87 loc) · 4.12 KB
/
test_client_headers.py
File metadata and controls
119 lines (87 loc) · 4.12 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
from __future__ import annotations
import json
import os
import sys
from importlib import metadata
from typing import TYPE_CHECKING
from werkzeug import Request, Response
from apify_client._http_client import HTTPClient, HTTPClientAsync
if TYPE_CHECKING:
from pytest_httpserver import HTTPServer
def _header_handler(request: Request) -> Response:
return Response(
status=200,
headers={},
response=json.dumps({'received_headers': dict(request.headers)}),
)
def _get_user_agent() -> str:
is_at_home = 'APIFY_IS_AT_HOME' in os.environ
python_version = '.'.join([str(x) for x in sys.version_info[:3]])
client_version = metadata.version('apify-client')
return f'ApifyClient/{client_version} ({sys.platform}; Python/{python_version}); isAtHome/{is_at_home}'
async def test_default_headers_async(httpserver: HTTPServer) -> None:
"""Test that default headers are sent with each request."""
client = HTTPClientAsync(token='placeholder_token')
httpserver.expect_request('/').respond_with_handler(_header_handler)
api_url = httpserver.url_for('/').removesuffix('/')
response = await client.call(method='GET', url=f'{api_url}/')
request_headers = json.loads(response.text)['received_headers']
assert request_headers == {
'User-Agent': _get_user_agent(),
'Accept': 'application/json, */*',
'Authorization': 'Bearer placeholder_token',
'Accept-Encoding': 'gzip, br, zstd, deflate',
'Host': f'{httpserver.host}:{httpserver.port}',
}
def test_default_headers_sync(httpserver: HTTPServer) -> None:
"""Test that default headers are sent with each request."""
client = HTTPClient(token='placeholder_token')
httpserver.expect_request('/').respond_with_handler(_header_handler)
api_url = httpserver.url_for('/').removesuffix('/')
response = client.call(method='GET', url=f'{api_url}/')
request_headers = json.loads(response.text)['received_headers']
assert request_headers == {
'User-Agent': _get_user_agent(),
'Accept': 'application/json, */*',
'Authorization': 'Bearer placeholder_token',
'Accept-Encoding': 'gzip, br, zstd, deflate',
'Host': f'{httpserver.host}:{httpserver.port}',
}
async def test_extra_headers_async(httpserver: HTTPServer) -> None:
"""Test that extra headers are sent with each request."""
extra_headers = {
'Test-Header': 'blah',
'User-Agent': 'CustomUserAgent/1.0', # Do not override Apify User-Agent
}
client = HTTPClientAsync(token='placeholder_token', extra_headers=extra_headers)
httpserver.expect_request('/').respond_with_handler(_header_handler)
api_url = httpserver.url_for('/').removesuffix('/')
response = await client.call(method='GET', url=f'{api_url}/')
request_headers = json.loads(response.text)['received_headers']
assert request_headers == {
'Test-Header': 'blah',
'User-Agent': _get_user_agent(), # Do not override Apify User-Agent
'Accept': 'application/json, */*',
'Authorization': 'Bearer placeholder_token',
'Accept-Encoding': 'gzip, br, zstd, deflate',
'Host': f'{httpserver.host}:{httpserver.port}',
}
def test_extra_headers_sync(httpserver: HTTPServer) -> None:
"""Test that extra headers are sent with each request."""
extra_headers = {
'Test-Header': 'blah',
'User-Agent': 'CustomUserAgent/1.0', # Do not override Apify User-Agent
}
client = HTTPClient(token='placeholder_token', extra_headers=extra_headers)
httpserver.expect_request('/').respond_with_handler(_header_handler)
api_url = httpserver.url_for('/').removesuffix('/')
response = client.call(method='GET', url=f'{api_url}/')
request_headers = json.loads(response.text)['received_headers']
assert request_headers == {
'Test-Header': 'blah',
'User-Agent': _get_user_agent(), # Do not override Apify User-Agent
'Accept': 'application/json, */*',
'Authorization': 'Bearer placeholder_token',
'Accept-Encoding': 'gzip, br, zstd, deflate',
'Host': f'{httpserver.host}:{httpserver.port}',
}