diff --git a/src/agent.py b/src/agent.py index ed2dc16..64a9eae 100644 --- a/src/agent.py +++ b/src/agent.py @@ -219,7 +219,7 @@ class FolderTransport: os.remove(os.path.join(req_dir, rid + ".json")) except OSError: pass - raise ConnectionError("хаб не ответил через папку %s за %.0f с" % (self.root, timeout)) + raise ConnectionError("не ответил через папку %s за %.0f с" % (self.root, timeout)) @staticmethod def _write(directory: str, name: str, payload: dict) -> None: @@ -238,6 +238,9 @@ class FolderTransport: resp = json.load(f) if not isinstance(resp, dict): raise ValueError("ответ не объект") + status, body = resp.get("status"), resp.get("body") + if type(status) is not int or not (body is None or isinstance(body, str)): + raise ValueError("неверные типы status/body") # bool — подкласс int, поэтому type(), не isinstance (M-2 agent#8) break except (ValueError, OSError): if time.monotonic() >= end: diff --git a/tests/test_folder.py b/tests/test_folder.py index d9474c5..bc00d74 100644 --- a/tests/test_folder.py +++ b/tests/test_folder.py @@ -88,6 +88,28 @@ class FolderTransportTest(unittest.TestCase): hub.settings() self.assertIn("нечитаемый ответ", str(e.exception)) + def _bad_response(self, payload): + path = os.path.join(self.root, "resp-bad.json") + with open(path, "w", encoding="utf-8") as f: + json.dump(payload, f) + ft = agent.FolderTransport(self.root, "Bearer x") + ft.BAD_JSON_WAIT = 0.3 + return ft, path + + def test_wrong_status_type_is_unreadable(self): + for payload in ({"id": "x", "status": "ok", "body": "{}"}, {"id": "x", "status": True, "body": "{}"}, + {"id": "x", "status": 200, "body": 5}): + ft, path = self._bad_response(payload) + with self.assertRaises(ConnectionError) as e: + ft._take(path) + self.assertIn("нечитаемый ответ", str(e.exception)) + + def test_no_answer_message_has_no_hub_prefix(self): + hub = self.client(timeout=0.3) + with self.assertRaises(ConnectionError) as e: + hub.settings() + self.assertFalse(str(e.exception).startswith("хаб")) # цикл сам пишет «хаб: …» (O-3 agent#8) + def test_late_response_is_not_reused(self): self.run_relay("late", delay=1.0) with self.assertRaises(ConnectionError):