Skip to content

Commit 922d905

Browse files
tests/live: Add conftest for injecting wsn header in live tests
- Introduced `conftest.py` to the `tests/live` folder to facilitate header injection for live tests. - Added `pdfrest_live_wsn` fixture to retrieve the `wsn` value from environment variables with a default fallback. - Used `monkeypatch` to modify `PdfRestClient` and `AsyncPdfRestClient` constructors for seamless `wsn` header injection. Assisted-by: Codex
1 parent 0cd9d28 commit 922d905

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

tests/live/conftest.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from __future__ import annotations
2+
3+
import os
4+
from typing import Any
5+
6+
import pytest
7+
8+
from pdfrest import AsyncPdfRestClient, PdfRestClient
9+
10+
DEFAULT_LIVE_WSN = "dl-internal-python-ci"
11+
12+
13+
def _with_live_wsn_header(kwargs: dict[str, Any], wsn: str) -> dict[str, Any]:
14+
new_kwargs = dict(kwargs)
15+
headers = dict(new_kwargs.get("headers") or {})
16+
headers["wsn"] = wsn
17+
new_kwargs["headers"] = headers
18+
return new_kwargs
19+
20+
21+
@pytest.fixture(scope="session")
22+
def pdfrest_live_wsn() -> str:
23+
return os.getenv("PDFREST_LIVE_WSN", DEFAULT_LIVE_WSN)
24+
25+
26+
@pytest.fixture(autouse=True)
27+
def inject_live_test_wsn_header(
28+
monkeypatch: pytest.MonkeyPatch,
29+
pdfrest_live_wsn: str,
30+
) -> None:
31+
original_sync_init = PdfRestClient.__init__
32+
original_async_init = AsyncPdfRestClient.__init__
33+
34+
def sync_init_with_live_header(
35+
self: PdfRestClient,
36+
*args: Any,
37+
**kwargs: Any,
38+
) -> None:
39+
original_sync_init(
40+
self,
41+
*args,
42+
**_with_live_wsn_header(kwargs, pdfrest_live_wsn),
43+
)
44+
45+
def async_init_with_live_header(
46+
self: AsyncPdfRestClient,
47+
*args: Any,
48+
**kwargs: Any,
49+
) -> None:
50+
original_async_init(
51+
self,
52+
*args,
53+
**_with_live_wsn_header(kwargs, pdfrest_live_wsn),
54+
)
55+
56+
monkeypatch.setattr(PdfRestClient, "__init__", sync_init_with_live_header)
57+
monkeypatch.setattr(AsyncPdfRestClient, "__init__", async_init_with_live_header)
58+
return

0 commit comments

Comments
 (0)