Answer stability round 2: 1 🔴, 0 🟡 (0 misread); read an error answer's body guarded, so one cut short names its status
This commit is contained in:
@@ -104,7 +104,7 @@ def http_json(method: str, url: str, token: str, body: object) -> object:
|
||||
raw = response.read()
|
||||
except urllib.error.HTTPError as failure:
|
||||
with failure:
|
||||
return Failure(f"{request_name} answered {failure.code}: {error_message(failure.read()) or failure.reason}")
|
||||
return Failure(f"{request_name} answered {failure.code}: {error_message(failure) or failure.reason}")
|
||||
except http.client.HTTPException as failure:
|
||||
return Failure(f"{request_name} got a broken answer: {failure!r}")
|
||||
except (OSError, ValueError) as failure:
|
||||
@@ -116,10 +116,11 @@ def http_json(method: str, url: str, token: str, body: object) -> object:
|
||||
return Failure(f"{request_name} answered with something other than JSON")
|
||||
|
||||
|
||||
def error_message(raw: bytes) -> str:
|
||||
def error_message(failure: urllib.error.HTTPError) -> str:
|
||||
"""Mattermost's message in an error answer; empty where the body is cut short, breaks HTTP or holds none."""
|
||||
try:
|
||||
answer = json.loads(raw)
|
||||
except ValueError:
|
||||
answer = json.loads(failure.read())
|
||||
except (http.client.HTTPException, OSError, ValueError):
|
||||
return ""
|
||||
message = answer.get("message") if isinstance(answer, dict) else None
|
||||
return message if isinstance(message, str) else ""
|
||||
|
||||
@@ -176,6 +176,10 @@ class HttpJson(unittest.TestCase):
|
||||
failed = dm.http_json("GET", f"{base}/api/v4/users/me", "t", None)
|
||||
self.assertTrue(isinstance(failed, Failure) and failed.reason.startswith("GET /api/v4/users/me got a broken answer: "), failed)
|
||||
|
||||
def test_names_the_status_of_an_error_whose_body_is_cut_short(self):
|
||||
base = self.serve((None, b"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 100\r\n\r\n{}"))
|
||||
self.assertEqual(dm.http_json("GET", f"{base}/api/v4/users/me", "t", None), Failure("GET /api/v4/users/me answered 500: Internal Server Error"))
|
||||
|
||||
def test_gives_up_on_a_server_that_never_answers_after_the_timeout(self):
|
||||
silent = socket.socket()
|
||||
self.addCleanup(silent.close)
|
||||
|
||||
Reference in New Issue
Block a user