mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 12:35:13 +00:00
fix(remote): map a cut-off answer to a recoverable error and stop trusting an empty optional answer
This commit is contained in:
@@ -4,13 +4,19 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import socket
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
import pytest
|
||||
import requests
|
||||
import responses
|
||||
|
||||
from unshackle.core.remote_service import RemoteClient
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.remote_protocol import ClientErrorKind
|
||||
from unshackle.core.remote_service import RemoteClient, RemoteError
|
||||
from unshackle.core.utilities import close_debug_logger, init_debug_logger
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -216,7 +222,65 @@ def test_late_error_after_heartbeat_is_fatal(client: RemoteClient) -> None:
|
||||
responses.add(responses.GET, "http://srv:8786/api/session/s/titles", body=body, status=200)
|
||||
with pytest.raises(SystemExit):
|
||||
client.get("/api/session/s/titles")
|
||||
assert client.request("get", "/api/session/s/titles", optional=True) == {}
|
||||
with pytest.raises(RemoteError):
|
||||
client.request("get", "/api/session/s/titles", optional=True)
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_an_optional_route_the_server_lacks_answers_empty(client: RemoteClient) -> None:
|
||||
responses.add(responses.POST, "http://srv:8786/api/session/s/segment_filter", body="404: Not Found", status=404)
|
||||
assert client.post_optional("/api/session/s/segment_filter", {}) == {}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("status", "code"),
|
||||
[(422, "SERVICE_ERROR"), (404, "TRACK_NOT_FOUND"), (404, "SESSION_NOT_FOUND"), (503, "SERVER_DRAINING")],
|
||||
)
|
||||
@responses.activate
|
||||
def test_an_optional_route_that_fails_on_the_server_raises(client: RemoteClient, status: int, code: str) -> None:
|
||||
body = {"status": "error", "error_code": code, "message": "no"}
|
||||
responses.add(responses.POST, "http://srv:8786/api/session/s/segment_filter", json=body, status=status)
|
||||
with pytest.raises(RemoteError) as e:
|
||||
client.post_optional("/api/session/s/segment_filter", {})
|
||||
assert e.value.kind == code
|
||||
|
||||
|
||||
@pytest.mark.parametrize("recorded", [False, True], ids=["plain", "debug-requests"])
|
||||
@pytest.mark.parametrize(
|
||||
("headers", "body"),
|
||||
[
|
||||
(b"Content-Length: 100\r\n", b'{"titles": ['),
|
||||
(b"Content-Encoding: gzip\r\nContent-Length: 13\r\n", b'{"titles": ['),
|
||||
(b"Transfer-Encoding: chunked\r\n", b'2\r\n\n\n\r\nc\r\n{"titles": ['),
|
||||
],
|
||||
ids=["short-body", "bad-gzip", "cut-chunked"],
|
||||
)
|
||||
def test_an_answer_cut_off_after_its_headers_is_a_connect_error(
|
||||
headers: bytes, body: bytes, recorded: bool, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""A body that stops early, or a gzip body that does not decode, is a broken connection the session can survive."""
|
||||
if recorded:
|
||||
monkeypatch.setattr(config, "debug_requests", True)
|
||||
init_debug_logger(log_path=tmp_path / "debug.jsonl", enabled=True)
|
||||
with socket.create_server(("127.0.0.1", 0)) as server:
|
||||
|
||||
def answer() -> None:
|
||||
conn, _ = server.accept()
|
||||
with conn:
|
||||
conn.recv(65536)
|
||||
conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n" + headers + b"\r\n" + body)
|
||||
|
||||
thread = threading.Thread(target=answer, daemon=True)
|
||||
thread.start()
|
||||
client = RemoteClient(server_url=f"http://127.0.0.1:{server.getsockname()[1]}", api_key="k")
|
||||
try:
|
||||
with pytest.raises(RemoteError) as e:
|
||||
client.get("/api/session/s/titles")
|
||||
finally:
|
||||
close_debug_logger()
|
||||
thread.join(5)
|
||||
assert e.value.kind == ClientErrorKind.CONNECT
|
||||
assert e.value.session_broke
|
||||
|
||||
|
||||
@responses.activate
|
||||
@@ -381,3 +445,14 @@ def test_a_probe_that_cannot_connect_logs_no_error(client: RemoteClient, caplog:
|
||||
|
||||
assert client.probe("/api/health") == {}
|
||||
assert not [r for r in caplog.records if r.levelno >= logging.WARNING]
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_any_other_requests_failure_is_one_remote_error(client: RemoteClient) -> None:
|
||||
leak = requests.TooManyRedirects("http://user:pw@gw.example/login?token=SECRET")
|
||||
responses.add(responses.GET, "http://srv:8786/api/session/s/titles", body=leak)
|
||||
with pytest.raises(RemoteError) as e:
|
||||
client.get("/api/session/s/titles")
|
||||
assert e.value.kind == ClientErrorKind.UNKNOWN
|
||||
assert e.value.message == "Request to remote server failed on /api/session/s/titles: TooManyRedirects"
|
||||
assert not e.value.session_broke
|
||||
|
||||
Reference in New Issue
Block a user