diff --git a/py/tests/test_魔方APP.py b/py/tests/test_魔方APP.py index 5dcd073..4533b60 100644 --- a/py/tests/test_魔方APP.py +++ b/py/tests/test_魔方APP.py @@ -34,6 +34,11 @@ class TestMoFangAppSpider(unittest.TestCase): def test_replace_code_normalizes_common_ocr_misreads(self): self.assertEqual(self.spider._replace_code("5y6口"), "5960") + @patch.object(Spider, "post") + def test_api_post_returns_none_when_upstream_payload_is_not_json(self, mock_post): + mock_post.return_value = SimpleNamespace(text="blocked") + self.assertIsNone(self.spider._api_post("initV119")) + @patch.object(Spider, "_api_post") def test_home_content_filters_categories_merges_area_and_inserts_current_year(self, mock_api_post): mock_api_post.return_value = { diff --git a/py/魔方APP.py b/py/魔方APP.py index ae76836..0eae66d 100644 --- a/py/魔方APP.py +++ b/py/魔方APP.py @@ -113,7 +113,10 @@ class Spider(BaseSpider): timeout=10, verify=False, ) - body = json.loads(response.text or "{}") + try: + body = json.loads(response.text or "{}") + except (json.JSONDecodeError, TypeError): + return None if not body.get("data"): return None return json.loads(self._aes_decrypt(body["data"]))