Files
courrier/tests/test_mirror_session_expiree.py

52 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Reprise automatique de la session MCP quand le miroir redémarre (404 sur l’ancienne session)."""
import json
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
from app.mirror_client import MirrorClient
class _FauxMiroir(BaseHTTPRequestHandler):
session_valide = "session-A"
appels = []
def log_message(self, *a):
pass
def do_POST(self):
corps = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
sid = self.headers.get("mcp-session-id")
type(self).appels.append((corps["method"], sid))
if corps["method"] == "initialize":
self.send_response(200)
self.send_header("mcp-session-id", type(self).session_valide)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"jsonrpc": "2.0", "id": corps["id"], "result": {}}).encode())
return
if sid != type(self).session_valide:
self.send_response(404)
self.end_headers()
return
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
texte = json.dumps({"ok": True})
self.wfile.write(json.dumps({"jsonrpc": "2.0", "id": corps["id"],
"result": {"content": [{"type": "text", "text": texte}]}}).encode())
def test_session_expiree_est_rouverte():
_FauxMiroir.appels = []
_FauxMiroir.session_valide = "session-A"
srv = HTTPServer(("127.0.0.1", 0), _FauxMiroir)
threading.Thread(target=srv.serve_forever, daemon=True).start()
try:
c = MirrorClient(base_url=f"http://127.0.0.1:{srv.server_port}/mcp", token="t", cache_ttl_seconds=0)
assert c.call_tool("mirror_status", {}) == {"ok": True}
_FauxMiroir.session_valide = "session-B" # le miroir a redémarré
assert c.call_tool("mirror_status", {}) == {"ok": True}
assert c.session_id == "session-B"
finally:
srv.shutdown()