|
1 | 1 | import shutil
|
2 | 2 | import tempfile
|
3 | 3 | import uuid
|
| 4 | +from json import dumps |
| 5 | +from json import loads |
4 | 6 | from pathlib import Path
|
5 | 7 |
|
6 | 8 | import pytest
|
7 | 9 | from httpx import HTTPStatusError
|
| 10 | +from httpx import Request |
8 | 11 | from httpx import codes
|
9 | 12 | from pytest_httpx import HTTPXMock
|
10 | 13 |
|
@@ -105,3 +108,71 @@ def test_not_a_server_error(self, client: GotenbergClient, httpx_mock: HTTPXMock
|
105 | 108 | with pytest.raises(HTTPStatusError) as exc_info:
|
106 | 109 | _ = route.index(test_file).run_with_retry(initial_retry_wait=0.1, retry_scale=0.1)
|
107 | 110 | assert exc_info.value.response.status_code == codes.NOT_FOUND
|
| 111 | + |
| 112 | + |
| 113 | +class TestWebhookHeaders: |
| 114 | + def test_webhook_basic_headers(self, client: GotenbergClient, httpx_mock: HTTPXMock): |
| 115 | + httpx_mock.add_response(method="POST", status_code=codes.OK) |
| 116 | + |
| 117 | + client.add_webhook_url("http://myapi:3000/on-success") |
| 118 | + client.add_error_webhook_url("http://myapi:3000/on-error") |
| 119 | + |
| 120 | + test_file = SAMPLE_DIR / "basic.html" |
| 121 | + with client.chromium.html_to_pdf() as route: |
| 122 | + _ = route.index(test_file).run_with_retry() |
| 123 | + |
| 124 | + requests = httpx_mock.get_requests() |
| 125 | + |
| 126 | + assert len(requests) == 1 |
| 127 | + |
| 128 | + request: Request = requests[0] |
| 129 | + |
| 130 | + assert "Gotenberg-Webhook-Url" in request.headers |
| 131 | + assert request.headers["Gotenberg-Webhook-Url"] == "http://myapi:3000/on-success" |
| 132 | + assert "Gotenberg-Webhook-Error-Url" in request.headers |
| 133 | + assert request.headers["Gotenberg-Webhook-Error-Url"] == "http://myapi:3000/on-error" |
| 134 | + |
| 135 | + def test_webhook_http_methods(self, client: GotenbergClient, httpx_mock: HTTPXMock): |
| 136 | + httpx_mock.add_response(method="POST", status_code=codes.OK) |
| 137 | + |
| 138 | + client.add_webhook_url("http://myapi:3000/on-success") |
| 139 | + client.set_webhook_http_method("POST") |
| 140 | + client.add_error_webhook_url("http://myapi:3000/on-error") |
| 141 | + client.set_error_webhook_http_method("GET") |
| 142 | + |
| 143 | + test_file = SAMPLE_DIR / "basic.html" |
| 144 | + with client.chromium.html_to_pdf() as route: |
| 145 | + _ = route.index(test_file).run_with_retry() |
| 146 | + |
| 147 | + requests = httpx_mock.get_requests() |
| 148 | + |
| 149 | + assert len(requests) == 1 |
| 150 | + |
| 151 | + request: Request = requests[0] |
| 152 | + |
| 153 | + assert "Gotenberg-Webhook-Method" in request.headers |
| 154 | + assert request.headers["Gotenberg-Webhook-Method"] == "POST" |
| 155 | + assert "Gotenberg-Webhook-Error-Method" in request.headers |
| 156 | + assert request.headers["Gotenberg-Webhook-Error-Method"] == "GET" |
| 157 | + |
| 158 | + def test_webhook_extra_headers(self, client: GotenbergClient, httpx_mock: HTTPXMock): |
| 159 | + httpx_mock.add_response(method="POST", status_code=codes.OK) |
| 160 | + |
| 161 | + headers = {"Token": "mytokenvalue"} |
| 162 | + headers_str = dumps(headers) |
| 163 | + |
| 164 | + client.set_webhook_extra_headers(headers) |
| 165 | + |
| 166 | + test_file = SAMPLE_DIR / "basic.html" |
| 167 | + with client.chromium.html_to_pdf() as route: |
| 168 | + _ = route.index(test_file).run_with_retry() |
| 169 | + |
| 170 | + requests = httpx_mock.get_requests() |
| 171 | + |
| 172 | + assert len(requests) == 1 |
| 173 | + |
| 174 | + request: Request = requests[0] |
| 175 | + |
| 176 | + assert "Gotenberg-Webhook-Extra-Http-Headers" in request.headers |
| 177 | + assert request.headers["Gotenberg-Webhook-Extra-Http-Headers"] == headers_str |
| 178 | + assert loads(request.headers["Gotenberg-Webhook-Extra-Http-Headers"]) == headers |
0 commit comments