Repository navigation
fix(event): save inline rx.download data through a blob url #7853
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
abulvenz
wants to merge
6
commits into
reflex-dev:main
Choose a base branch
from
abulvenz:download-blob-url
base: main
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.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f335b00
fix(event): save inline rx.download data through a blob url
abulvenz 37ec34d
fix(event): decode inline rx.download data without fetch
abulvenz c1678ac
fix(event): decode the fragment and percent escapes of an inline down…
abulvenz 86f26dc
Merge remote-tracking branch 'upstream/main' into download-blob-url
abulvenz 38a83f5
test: compare the downloaded url with the blob url the handler created
abulvenz c6f3c59
Merge branch 'download-blob-url-exact-url' into download-blob-url
abulvenz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `rx.download` with `data` saves the file on iOS (Safari, other iOS browsers and installed web apps) instead of only showing it. |
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,116 @@ | ||
| """Integration test for ``rx.download`` with inline data. | ||
|
|
||
| iOS WebKit ignores the ``download`` attribute of ``data:`` urls and only shows | ||
| the file, so data has to be saved through a ``blob:`` url. The page runs under a | ||
| ``connect-src`` policy that does not allow ``data:``, so the frontend has to | ||
| decode the data itself rather than fetch it. | ||
| """ | ||
|
|
||
| from collections.abc import Generator | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from playwright.sync_api import Page, expect | ||
|
|
||
| from reflex.testing import AppHarness | ||
|
|
||
|
|
||
| def DownloadApp(): | ||
| """App downloading inline data from the frontend and from a backend event.""" | ||
| import reflex as rx | ||
|
|
||
| class State(rx.State): | ||
| text: str = "var;text#%41 ä" | ||
|
|
||
| @rx.event | ||
| def download_bytes(self): | ||
| return rx.download(data=b"backend,bytes\n", filename="backend.csv") | ||
|
|
||
| @rx.page("/") | ||
| def index(): | ||
| return rx.box( | ||
| rx.input( | ||
| value=State.router.session.client_token, | ||
| read_only=True, | ||
| id="token", | ||
| ), | ||
| rx.button( | ||
| "text", | ||
| on_click=rx.download(data="frontend;text#%41", filename="frontend.txt"), | ||
| id="download-text", | ||
| ), | ||
| rx.button( | ||
| "var", | ||
| on_click=rx.download(data=State.text, filename="var.txt"), | ||
| id="download-var", | ||
| ), | ||
| rx.button("bytes", on_click=State.download_bytes, id="download-bytes"), | ||
| ) | ||
|
|
||
| app = rx.App( # noqa: F841 | ||
| head_components=[ | ||
| rx.el.meta( | ||
| http_equiv="Content-Security-Policy", | ||
| content="connect-src 'self' http: ws:", | ||
| ) | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def download_app(tmp_path_factory) -> Generator[AppHarness, None, None]: | ||
| """Start DownloadApp via AppHarness. | ||
|
|
||
| Args: | ||
| tmp_path_factory: pytest fixture for creating temporary directories. | ||
|
|
||
| Yields: | ||
| Running AppHarness instance. | ||
| """ | ||
| with AppHarness.create( | ||
| root=tmp_path_factory.mktemp("download_app"), | ||
| app_source=DownloadApp, | ||
| ) as harness: | ||
| assert harness.app_instance is not None, "app is not running" | ||
| yield harness | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("button_id", "filename", "content"), | ||
| [ | ||
| ("download-text", "frontend.txt", b"frontend;text#%41"), | ||
| ("download-var", "var.txt", '"var;text#%41 \u00e4"'.encode()), | ||
| ("download-bytes", "backend.csv", b"backend,bytes\n"), | ||
| ], | ||
| ) | ||
| def test_download_data_through_blob_url( | ||
| download_app: AppHarness, | ||
| page: Page, | ||
| button_id: str, | ||
| filename: str, | ||
| content: bytes, | ||
| ): | ||
| """Inline data downloads from a blob: url with its file name and content. | ||
|
|
||
| The page runs under a ``connect-src`` policy that blocks fetching ``data:`` | ||
| urls. | ||
|
|
||
| Args: | ||
| download_app: AppHarness running the test app. | ||
| page: Playwright page. | ||
| button_id: The id of the button that starts the download. | ||
| filename: The expected file name. | ||
| content: The expected file content. | ||
| """ | ||
| assert download_app.frontend_url is not None | ||
|
|
||
| page.goto(download_app.frontend_url) | ||
| expect(page.locator("#token")).not_to_have_value("") | ||
|
|
||
| with page.expect_download() as download_info: | ||
| page.click(f"#{button_id}") | ||
| download = download_info.value | ||
|
|
||
| assert download.url.startswith("blob:") | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| assert download.suggested_filename == filename | ||
| assert Path(download.path()).read_bytes() == content | ||
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.
Uh oh!
There was an error while loading. Please reload this page.