-
Notifications
You must be signed in to change notification settings - Fork 18
fix: Read file-like values before upload in the impit transport #965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vdusek
wants to merge
5
commits into
master
Choose a base branch
from
worktree-fix-b7
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc092b8
fix: Read file-like values before upload in the impit transport
vdusek 4f1e9ff
fix: Read duck-typed file-like values and encode KVS records off the …
vdusek c0f62ed
style: Drop underscore prefix from test helpers and tighten comments
vdusek 6010aa2
test: Parametrize key-value store record upload tests over gzip and b…
vdusek 8902464
test: Drop redundant 'Regression test:' prefix from record upload tes…
vdusek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import gzip | ||
| import io | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import brotli | ||
| import pytest | ||
| from werkzeug import Request, Response | ||
|
|
||
| from apify_client import ApifyClient, ApifyClientAsync | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pytest_httpserver import HTTPServer | ||
|
|
||
| from apify_client.types import HttpCompressionAlgorithm | ||
|
|
||
| _MOCKED_KVS_ID = 'test_kvs_id' | ||
| _RECORD_PATH = f'/v2/key-value-stores/{_MOCKED_KVS_ID}/records/f' | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| params=[ | ||
| pytest.param(('gzip', 'gzip'), id='gzip'), | ||
| pytest.param(('brotli', 'br'), id='brotli'), | ||
| ] | ||
| ) | ||
| def compression_case(request: pytest.FixtureRequest) -> tuple[HttpCompressionAlgorithm, str]: | ||
| """Run each test over both supported request-body compression algorithms, as (algorithm, content-encoding).""" | ||
| return request.param | ||
|
|
||
|
|
||
| def decode_body(request: Request) -> bytes: | ||
| """Decompress a captured request body according to its `Content-Encoding`.""" | ||
| raw = request.get_data() | ||
| encoding = request.headers.get('Content-Encoding') | ||
| if encoding == 'gzip': | ||
| return gzip.decompress(raw) | ||
| if encoding == 'br': | ||
| return brotli.decompress(raw) | ||
| return raw | ||
|
|
||
|
|
||
| def test_set_record_reads_file_like_value_sync( | ||
| httpserver: HTTPServer, compression_case: tuple[HttpCompressionAlgorithm, str] | ||
| ) -> None: | ||
| """A file-like value is read and its bytes are uploaded, not passed through unread.""" | ||
| algorithm, content_encoding = compression_case | ||
| captured_requests: list[Request] = [] | ||
|
|
||
| def capture_request(request: Request) -> Response: | ||
| captured_requests.append(request) | ||
| return Response(status=201) | ||
|
|
||
| httpserver.expect_request(_RECORD_PATH, method='PUT').respond_with_handler(capture_request) | ||
|
|
||
| api_url = httpserver.url_for('/').removesuffix('/') | ||
| client = ApifyClient(token='test_token', api_url=api_url, compression=algorithm) | ||
|
|
||
| client.key_value_store(_MOCKED_KVS_ID).set_record('f', io.BytesIO(b'buffer data')) | ||
|
|
||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].headers['content-encoding'] == content_encoding | ||
| assert decode_body(captured_requests[0]) == b'buffer data' | ||
| assert captured_requests[0].headers['content-type'] == 'application/octet-stream' | ||
|
|
||
|
|
||
| async def test_set_record_reads_file_like_value_async( | ||
| httpserver: HTTPServer, compression_case: tuple[HttpCompressionAlgorithm, str] | ||
| ) -> None: | ||
| """A file-like value is read and its bytes are uploaded, not passed through unread.""" | ||
| algorithm, content_encoding = compression_case | ||
| captured_requests: list[Request] = [] | ||
|
|
||
| def capture_request(request: Request) -> Response: | ||
| captured_requests.append(request) | ||
| return Response(status=201) | ||
|
|
||
| httpserver.expect_request(_RECORD_PATH, method='PUT').respond_with_handler(capture_request) | ||
|
|
||
| api_url = httpserver.url_for('/').removesuffix('/') | ||
| client = ApifyClientAsync(token='test_token', api_url=api_url, compression=algorithm) | ||
|
|
||
| await client.key_value_store(_MOCKED_KVS_ID).set_record('f', io.BytesIO(b'buffer data')) | ||
|
|
||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].headers['content-encoding'] == content_encoding | ||
| assert decode_body(captured_requests[0]) == b'buffer data' | ||
| assert captured_requests[0].headers['content-type'] == 'application/octet-stream' | ||
|
|
||
|
|
||
| def test_set_record_reads_stringio_value_sync( | ||
| httpserver: HTTPServer, compression_case: tuple[HttpCompressionAlgorithm, str] | ||
| ) -> None: | ||
| """A text file-like value is read and uploaded as text/plain through the HTTP stack.""" | ||
| algorithm, content_encoding = compression_case | ||
| captured_requests: list[Request] = [] | ||
|
|
||
| def capture_request(request: Request) -> Response: | ||
| captured_requests.append(request) | ||
| return Response(status=201) | ||
|
|
||
| httpserver.expect_request(_RECORD_PATH, method='PUT').respond_with_handler(capture_request) | ||
|
|
||
| api_url = httpserver.url_for('/').removesuffix('/') | ||
| client = ApifyClient(token='test_token', api_url=api_url, compression=algorithm) | ||
|
|
||
| client.key_value_store(_MOCKED_KVS_ID).set_record('f', io.StringIO('buffer data')) | ||
|
|
||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].headers['content-encoding'] == content_encoding | ||
| assert decode_body(captured_requests[0]) == b'buffer data' | ||
| assert captured_requests[0].headers['content-type'] == 'text/plain; charset=utf-8' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asyncio.to_threadis useful only to the branch that can do IO. It just adds overhead in other cases, or? So it improves the less frequent case at the cost of the main use case