Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions tests/integration/test_key_value_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,13 @@ async def test_stream_record_signature(
# Note: stream_record returns a context manager, so we need to handle it differently
# For sync/async unification, we can't use 'with await maybe_await(...)' directly
# We'll test the error condition separately based on client type
try:
if is_async:
if is_async:
with pytest.raises(ApifyApiError):
async with kvs.stream_record(key=key) as stream: # ty: ignore[invalid-context-manager]
pass
pytest.fail('Expected ApifyApiError')
else:
with kvs.stream_record(key=key) as stream: # ty: ignore[invalid-context-manager]
pass
pytest.fail('Expected ApifyApiError')
except ApifyApiError:
pass # Expected
else:
with pytest.raises(ApifyApiError), kvs.stream_record(key=key) as stream: # ty: ignore[invalid-context-manager]
pass

# Kvs content retrieved with correct signature
if is_async:
Expand Down
25 changes: 17 additions & 8 deletions tests/unit/test_client_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from pytest_httpserver import HTTPServer


def _parse_accept_encoding(header: str) -> set[str]:
"""Parse Accept-Encoding header into a set of encoding names, ignoring order and whitespace."""
return {enc.strip() for enc in header.split(',')}


def _header_handler(request: Request) -> Response:
return Response(
status=200,
Expand All @@ -39,13 +44,14 @@ async def test_default_headers_async(httpserver: HTTPServer) -> None:

request_headers = json.loads(response.text)['received_headers']

assert request_headers == {
expected_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}',
}
assert {k: v for k, v in request_headers.items() if k != 'Accept-Encoding'} == expected_headers
assert _parse_accept_encoding(request_headers['Accept-Encoding']) == {'gzip', 'br', 'zstd', 'deflate'}


def test_default_headers_sync(httpserver: HTTPServer) -> None:
Expand All @@ -58,13 +64,14 @@ def test_default_headers_sync(httpserver: HTTPServer) -> None:

request_headers = json.loads(response.text)['received_headers']

assert request_headers == {
expected_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}',
}
assert {k: v for k, v in request_headers.items() if k != 'Accept-Encoding'} == expected_headers
assert _parse_accept_encoding(request_headers['Accept-Encoding']) == {'gzip', 'br', 'zstd', 'deflate'}


async def test_headers_async(httpserver: HTTPServer) -> None:
Expand All @@ -80,14 +87,15 @@ async def test_headers_async(httpserver: HTTPServer) -> None:

request_headers = json.loads(response.text)['received_headers']

assert request_headers == {
expected_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}',
}
assert {k: v for k, v in request_headers.items() if k != 'Accept-Encoding'} == expected_headers
assert _parse_accept_encoding(request_headers['Accept-Encoding']) == {'gzip', 'br', 'zstd', 'deflate'}


def test_headers_sync(httpserver: HTTPServer) -> None:
Expand All @@ -107,11 +115,12 @@ def test_headers_sync(httpserver: HTTPServer) -> None:

request_headers = json.loads(response.text)['received_headers']

assert request_headers == {
expected_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}',
}
assert {k: v for k, v in request_headers.items() if k != 'Accept-Encoding'} == expected_headers
assert _parse_accept_encoding(request_headers['Accept-Encoding']) == {'gzip', 'br', 'zstd', 'deflate'}
Loading
Loading