Skip to content

Commit

Permalink
Reworks to account for changes in httpx-mock
Browse files Browse the repository at this point in the history
  • Loading branch information
stumpylog committed Dec 10, 2024
1 parent c8977c1 commit a4a07f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 48 deletions.
18 changes: 8 additions & 10 deletions tests/test_convert_chromium_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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"),
Expand All @@ -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,
)
34 changes: 11 additions & 23 deletions tests/test_convert_chromium_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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")
Expand All @@ -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(
Expand All @@ -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,
)
23 changes: 8 additions & 15 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a4a07f7

Please sign in to comment.