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
149 lines (111 loc) · 4.92 KB
/
test_client_headers.py
File metadata and controls
149 lines (111 loc) · 4.92 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
from __future__ import annotations
import json
import os
import sys
from importlib import metadata
from typing import TYPE_CHECKING
import pytest
from werkzeug import Request, Response
from apify_client import ApifyClient, ApifyClientAsync
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_headers_async(httpserver: HTTPServer) -> None:
"""Test that custom headers are sent with each request."""
client = HTTPClientAsync(
token='placeholder_token',
headers={'Test-Header': 'blah', 'User-Agent': 'CustomUserAgent/1.0', 'Authorization': 'strange_value'},
)
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': 'CustomUserAgent/1.0',
'Accept': 'application/json, */*',
'Authorization': 'strange_value',
'Accept-Encoding': 'gzip, br, zstd, deflate',
'Host': f'{httpserver.host}:{httpserver.port}',
}
def test_headers_sync(httpserver: HTTPServer) -> None:
"""Test that custom headers are sent with each request."""
client = HTTPClient(
token='placeholder_token',
headers={
'Test-Header': 'blah',
'User-Agent': 'CustomUserAgent/1.0',
'Authorization': 'strange_value',
},
)
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': 'CustomUserAgent/1.0',
'Accept': 'application/json, */*',
'Authorization': 'strange_value',
'Accept-Encoding': 'gzip, br, zstd, deflate',
'Host': f'{httpserver.host}:{httpserver.port}',
}
def test_warning_on_overridden_headers_sync() -> None:
"""Test that warning is raised when default headers are overridden."""
with pytest.warns(UserWarning, match='User-Agent, Authorization headers of ApifyClient'):
ApifyClient(
token='placeholder_token',
headers={
'User-Agent': 'CustomUserAgent/1.0',
'Authorization': 'strange_value',
},
)
async def test_warning_on_overridden_headers_async() -> None:
"""Test that warning is raised when default headers are overridden."""
with pytest.warns(UserWarning, match='User-Agent, Authorization headers of ApifyClientAsync'):
ApifyClientAsync(
token='placeholder_token',
headers={
'User-Agent': 'CustomUserAgent/1.0',
'Authorization': 'strange_value',
},
)