|
| 1 | +""" |
| 2 | +Integration tests for callback payload compression. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from dash import Dash, html, dcc, Input, Output, State |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + "backend,dash_duo_fixture", |
| 11 | + [("flask", "dash_duo"), ("quart", "dash_duo_mp"), ("fastapi", "dash_duo")], |
| 12 | +) |
| 13 | +@pytest.mark.parametrize("payload_size", [1, 500_000]) |
| 14 | +@pytest.mark.parametrize("compress_threshold", [0, 500_000]) |
| 15 | +def test_cbcomp01_compress_request_payload( |
| 16 | + request, dash_duo_fixture, backend, payload_size, compress_threshold |
| 17 | +): |
| 18 | + """Test that the client sends a compressed body when appropriate.""" |
| 19 | + if backend == "quart": |
| 20 | + pytest.importorskip( |
| 21 | + "quart", reason="Quart extra dependencies are not installed" |
| 22 | + ) |
| 23 | + pytest.importorskip("hypercorn", reason="hypercorn is not installed") |
| 24 | + elif backend == "fastapi": |
| 25 | + pytest.importorskip( |
| 26 | + "fastapi", reason="fastapi extra dependencies are not installed" |
| 27 | + ) |
| 28 | + |
| 29 | + app = Dash(__name__, backend=backend) |
| 30 | + |
| 31 | + @app.backend.before_request |
| 32 | + def capture_compression(): |
| 33 | + # intercept the request to /_dash-update-component and record whether the payload was compressed |
| 34 | + req = app.backend.request_adapter() |
| 35 | + if req.path == "/_dash-update-component": |
| 36 | + if "gzip" in req.headers.get("Content-Encoding", ""): |
| 37 | + req.context.compressed_payload_size = int( |
| 38 | + req.headers.get("content-length", 0) |
| 39 | + ) |
| 40 | + else: |
| 41 | + req.context.compressed_payload_size = None |
| 42 | + |
| 43 | + @app.callback( |
| 44 | + Output("data_size", "children"), |
| 45 | + Output("data_compressed", "children"), |
| 46 | + Output("data_compressed_size", "children"), |
| 47 | + Input("btn", "n_clicks"), |
| 48 | + State("store", "data"), |
| 49 | + compress_payload=True, |
| 50 | + compress_threshold=compress_threshold, |
| 51 | + prevent_initial_call=True, |
| 52 | + ) |
| 53 | + def on_click(n, data): |
| 54 | + # log the size of the data and whether it was compressed |
| 55 | + compressed_payload_size = ( |
| 56 | + app.backend.request_adapter().context.compressed_payload_size |
| 57 | + ) |
| 58 | + return ( |
| 59 | + len(data), |
| 60 | + repr(compressed_payload_size is not None), |
| 61 | + compressed_payload_size, |
| 62 | + ) |
| 63 | + |
| 64 | + app.layout = html.Div( |
| 65 | + [ |
| 66 | + html.Button("Click", id="btn"), |
| 67 | + html.Div(id="data_size"), |
| 68 | + html.Div(id="data_compressed"), |
| 69 | + html.Div(id="data_compressed_size"), |
| 70 | + dcc.Store(id="store", data="x" * payload_size), |
| 71 | + ] |
| 72 | + ) |
| 73 | + |
| 74 | + dash_duo = request.getfixturevalue(dash_duo_fixture) |
| 75 | + dash_duo.start_server(app) |
| 76 | + dash_duo.find_element("#btn").click() |
| 77 | + # assert that the data size matches the expected payload size |
| 78 | + dash_duo.wait_for_text_to_equal("#data_size", f"{payload_size}") |
| 79 | + # assert that the data was compressed if the payload size is greater than or equal to the compression threshold |
| 80 | + dash_duo.wait_for_text_to_equal( |
| 81 | + "#data_compressed", repr(payload_size >= compress_threshold) |
| 82 | + ) |
0 commit comments