From a4a07f7e8aff8b6c19147d984e8b5e3ff18699a4 Mon Sep 17 00:00:00 2001 From: Trenton Holmes <797416+stumpylog@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:18:53 -0800 Subject: [PATCH] Reworks to account for changes in httpx-mock --- tests/test_convert_chromium_html.py | 18 +++++++-------- tests/test_convert_chromium_url.py | 34 ++++++++++------------------- tests/utils.py | 23 +++++++------------ 3 files changed, 27 insertions(+), 48 deletions(-) diff --git a/tests/test_convert_chromium_html.py b/tests/test_convert_chromium_html.py index ed3d1d9..07d1a6a 100644 --- a/tests/test_convert_chromium_html.py +++ b/tests/test_convert_chromium_html.py @@ -127,8 +127,8 @@ def test_convert_page_size(self, client: GotenbergClient, sample_directory: Path _ = route.index(test_file).size(A4).run() request = httpx_mock.get_request() - verify_stream_contains("paperWidth", "8.27", request.stream) - verify_stream_contains("paperHeight", "11.7", request.stream) + verify_stream_contains(request, "paperWidth", "8.27") + verify_stream_contains(request, "paperHeight", "11.7") def test_convert_margin(self, client: GotenbergClient, sample_directory: Path, httpx_mock: HTTPXMock): httpx_mock.add_response(method="POST") @@ -149,10 +149,10 @@ def test_convert_margin(self, client: GotenbergClient, sample_directory: Path, h ) request = httpx_mock.get_request() - verify_stream_contains("marginTop", "1cm", request.stream) - verify_stream_contains("marginBottom", "2pc", request.stream) - verify_stream_contains("marginLeft", "3mm", request.stream) - verify_stream_contains("marginRight", "4", request.stream) + verify_stream_contains(request, "marginTop", "1cm") + verify_stream_contains(request, "marginBottom", "2pc") + verify_stream_contains(request, "marginLeft", "3mm") + verify_stream_contains(request, "marginRight", "4") def test_convert_render_control(self, client: GotenbergClient, sample_directory: Path, httpx_mock: HTTPXMock): httpx_mock.add_response(method="POST") @@ -161,8 +161,7 @@ def test_convert_render_control(self, client: GotenbergClient, sample_directory: with client.chromium.html_to_pdf() as route: _ = route.index(test_file).render_wait(500.0).run() - request = httpx_mock.get_request() - verify_stream_contains("waitDelay", "500.0", request.stream) + verify_stream_contains(httpx_mock.get_request(), "waitDelay", "500.0") @pytest.mark.parametrize( ("orientation"), @@ -181,9 +180,8 @@ def test_convert_orientation( with client.chromium.html_to_pdf() as route: _ = route.index(test_file).orient(orientation).run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "landscape", "true" if orientation == PageOrientation.Landscape else "false", - request.stream, ) diff --git a/tests/test_convert_chromium_url.py b/tests/test_convert_chromium_url.py index e0f5e7d..1204ed6 100644 --- a/tests/test_convert_chromium_url.py +++ b/tests/test_convert_chromium_url.py @@ -41,11 +41,10 @@ def test_convert_orientation( with client.chromium.url_to_pdf() as route: _ = route.url(webserver_docker_internal_url).media_type(emulation).run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "emulatedMediaType", "screen" if emulation == EmulatedMediaType.Screen else "print", - request.stream, ) @pytest.mark.parametrize( @@ -66,11 +65,10 @@ def test_convert_css_or_not_size( getattr(route, method)() _ = route.run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "preferCssPageSize", "true" if method == "prefer_css_page_size" else "false", - request.stream, ) @pytest.mark.parametrize( @@ -91,11 +89,10 @@ def test_convert_background_graphics_or_not( getattr(route, method)() _ = route.run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "printBackground", "true" if method == "background_graphics" else "false", - request.stream, ) @pytest.mark.parametrize( @@ -116,11 +113,10 @@ def test_convert_hide_background_or_not( getattr(route, method)() _ = route.run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "omitBackground", "true" if method == "hide_background" else "false", - request.stream, ) @pytest.mark.parametrize( @@ -141,11 +137,10 @@ def test_convert_fail_exceptions( getattr(route, method)() _ = route.run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "failOnConsoleExceptions", "true" if method == "fail_on_exceptions" else "false", - request.stream, ) def test_convert_scale( @@ -159,11 +154,10 @@ def test_convert_scale( with client.chromium.url_to_pdf() as route: _ = route.url(webserver_docker_internal_url).scale(1.5).run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "scale", "1.5", - request.stream, ) def test_convert_page_ranges( @@ -177,11 +171,10 @@ def test_convert_page_ranges( with client.chromium.url_to_pdf() as route: _ = route.url(webserver_docker_internal_url).page_ranges("1-5").run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "nativePageRanges", "1-5", - request.stream, ) def test_convert_url_render_wait( @@ -195,11 +188,10 @@ def test_convert_url_render_wait( with client.chromium.url_to_pdf() as route: _ = route.url(webserver_docker_internal_url).render_wait(500).run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "waitDelay", "500", - request.stream, ) def test_convert_url_render_expression( @@ -213,11 +205,10 @@ def test_convert_url_render_expression( with client.chromium.url_to_pdf() as route: _ = route.url(webserver_docker_internal_url).render_expr("wait while false;").run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "waitForExpression", "wait while false;", - request.stream, ) @pytest.mark.filterwarnings("ignore::DeprecationWarning") @@ -232,11 +223,10 @@ def test_convert_url_user_agent( with client.chromium.url_to_pdf() as route: _ = route.url(webserver_docker_internal_url).user_agent("Firefox").run() - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "userAgent", "Firefox", - request.stream, ) def test_convert_url_headers( @@ -251,10 +241,8 @@ def test_convert_url_headers( with client.chromium.url_to_pdf() as route: _ = route.url(webserver_docker_internal_url).headers(headers).run() - - request = httpx_mock.get_request() verify_stream_contains( + httpx_mock.get_request(), "extraHttpHeaders", json.dumps(headers), - request.stream, ) diff --git a/tests/utils.py b/tests/utils.py index 1da4577..54bb19a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,24 +6,17 @@ import tempfile from pathlib import Path -from httpx._multipart import DataField -from httpx._multipart import FileField -from httpx._multipart import MultipartStream +def verify_stream_contains(request, key: str, value: str) -> None: + content_type = request.headers["Content-Type"] + assert "multipart/form-data" in content_type -def verify_stream_contains(key: str, value: str, stream: MultipartStream) -> None: - for item in stream.fields: - if isinstance(item, FileField): - continue - elif isinstance(item, DataField) and item.name == key: - actual_value = item.value - if isinstance(actual_value, bytes): - actual_value = actual_value.decode("utf-8") - assert actual_value == value, f"Key '{actual_value}' /= {value}" - return + boundary = content_type.split("boundary=")[1] - msg = f'Key "{key}" with value "{value}" not found in stream' - raise AssertionError(msg) + parts = request.content.split(f"--{boundary}".encode()) + + form_field_found = any(f'name="{key}"'.encode() in part and value.encode() in part for part in parts) + assert form_field_found, f'Key "{key}" with value "{value}" not found in stream' def extract_text(pdf_path: Path) -> str: