Skip to content

Commit 722ff81

Browse files
Fix run sync stream readiness test
1 parent e2baf44 commit 722ff81

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

tests/test_run_sync_stream.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ def iter_lines(self, decode_unicode: bool = True):
6868
yield from self._lines
6969

7070

71+
class _HealthResponse:
72+
"""Successful backend readiness probe response."""
73+
74+
status_code = 200
75+
76+
def __enter__(self):
77+
return self
78+
79+
def __exit__(self, *args):
80+
return False
81+
82+
def json(self):
83+
return {"init_ok": True, "status": "ready"}
84+
85+
7186
class _ErrorBodyResponse:
7287
"""Non-stream error response with JSON body."""
7388

@@ -105,15 +120,26 @@ def _stream_context(bp):
105120
)
106121

107122

123+
def _mock_backend_get(response):
124+
def fake_get(url, **kwargs):
125+
if url.endswith("/health"):
126+
assert "stream" not in kwargs
127+
assert kwargs.get("verify") is True
128+
return _HealthResponse()
129+
assert kwargs.get("stream") is True
130+
assert kwargs.get("verify") is True
131+
return response
132+
133+
return fake_get
134+
135+
108136
def test_run_sync_stream_success(backend_proxy_module, monkeypatch):
109137
bp = backend_proxy_module
110138
urls: list[str] = []
111139

112140
def fake_get(url, **kwargs):
113141
urls.append(url)
114-
assert kwargs.get("stream") is True
115-
assert kwargs.get("verify") is True
116-
return _StreamResponse(_sse_complete_ok())
142+
return _mock_backend_get(_StreamResponse(_sse_complete_ok()))(url, **kwargs)
117143

118144
monkeypatch.setattr(bp.requests, "get", fake_get)
119145
monkeypatch.setattr(bp, "get_fastapi_request_context", lambda: _stream_context(bp))
@@ -134,7 +160,8 @@ def on_frame(ev: str, data: dict) -> None:
134160
assert payload["response"]["ok"] is True
135161
assert payload["response"]["result"]["n"] == 3
136162
assert payload["path"] == "dcim/devices/create/stream"
137-
assert urls[0].startswith("https://proxbox.local:8800/dcim/devices/create/stream")
163+
assert urls[0] == "https://proxbox.local:8800/health"
164+
assert urls[1].startswith("https://proxbox.local:8800/dcim/devices/create/stream")
138165

139166

140167
def test_run_sync_stream_success_with_list_result(backend_proxy_module, monkeypatch):
@@ -149,7 +176,7 @@ def test_run_sync_stream_success_with_list_result(backend_proxy_module, monkeypa
149176
monkeypatch.setattr(
150177
bp.requests,
151178
"get",
152-
lambda *a, **k: _StreamResponse(lines),
179+
_mock_backend_get(_StreamResponse(lines)),
153180
)
154181
monkeypatch.setattr(bp, "get_fastapi_request_context", lambda: _stream_context(bp))
155182

@@ -174,7 +201,7 @@ def test_run_sync_stream_complete_ok_false(backend_proxy_module, monkeypatch):
174201
monkeypatch.setattr(
175202
bp.requests,
176203
"get",
177-
lambda *a, **k: _StreamResponse(lines),
204+
_mock_backend_get(_StreamResponse(lines)),
178205
)
179206
monkeypatch.setattr(bp, "get_fastapi_request_context", lambda: _stream_context(bp))
180207
payload, status = bp.run_sync_stream("full-update/stream")
@@ -192,7 +219,7 @@ def test_run_sync_stream_missing_complete(backend_proxy_module, monkeypatch):
192219
monkeypatch.setattr(
193220
bp.requests,
194221
"get",
195-
lambda *a, **k: _StreamResponse(lines),
222+
_mock_backend_get(_StreamResponse(lines)),
196223
)
197224
monkeypatch.setattr(bp, "get_fastapi_request_context", lambda: _stream_context(bp))
198225
payload, status = bp.run_sync_stream("dcim/devices/create/stream")
@@ -205,7 +232,7 @@ def test_run_sync_stream_http_error_json(backend_proxy_module, monkeypatch):
205232
monkeypatch.setattr(
206233
bp.requests,
207234
"get",
208-
lambda *a, **k: _ErrorBodyResponse(502, {"detail": "bad gateway"}),
235+
_mock_backend_get(_ErrorBodyResponse(502, {"detail": "bad gateway"})),
209236
)
210237
monkeypatch.setattr(bp, "get_fastapi_request_context", lambda: _stream_context(bp))
211238
payload, status = bp.run_sync_stream("dcim/devices/create/stream")

0 commit comments

Comments
 (0)