|
| 1 | +"""Run SDK contract checks against local fixtures, never production.""" |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +import subprocess |
| 7 | +import threading |
| 8 | +import time |
| 9 | +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 10 | +from urllib.parse import urlsplit, parse_qs |
| 11 | + |
| 12 | +ROOT = Path(__file__).resolve().parents[2] |
| 13 | +KEY = 'td_live_fixture_only' |
| 14 | + |
| 15 | + |
| 16 | +class Fixture(BaseHTTPRequestHandler): |
| 17 | + calls = [] |
| 18 | + |
| 19 | + def log_message(self, *args): |
| 20 | + pass |
| 21 | + |
| 22 | + def do_GET(self): self.respond() |
| 23 | + def do_POST(self): self.respond() |
| 24 | + def do_DELETE(self): self.respond() |
| 25 | + def do_PATCH(self): self.respond() |
| 26 | + |
| 27 | + def respond(self): |
| 28 | + self.calls.append((self.command, self.path)) |
| 29 | + raw = self.rfile.read(int(self.headers.get('Content-Length', '0'))) |
| 30 | + path = urlsplit(self.path).path |
| 31 | + status = 200 |
| 32 | + content_type = 'application/json; charset=utf-8' |
| 33 | + payload = {'id': 'fixture', 'status': 'succeeded', 'result_id': 'transcript', 'future_field': 42, |
| 34 | + 'words': [{'start_ms': 123, 'end_ms': 456}], |
| 35 | + 'request': {'method': self.command, 'path': path, 'query': parse_qs(urlsplit(self.path).query), |
| 36 | + 'body': json.loads(raw) if raw else None, |
| 37 | + 'idempotency_key': self.headers.get('Idempotency-Key'), |
| 38 | + 'authorized': self.headers.get('Authorization') == 'Bearer ' + KEY}} |
| 39 | + if path == '/v1/redirect': |
| 40 | + self.send_response(302) |
| 41 | + self.send_header('Location', '/v1/credential-leak') |
| 42 | + self.send_header('Content-Length', '0') |
| 43 | + self.end_headers() |
| 44 | + return |
| 45 | + if path == '/v1/rate-limit': |
| 46 | + status = 429 |
| 47 | + payload = {'error': {'code': 'RATE_LIMITED', 'message': KEY, 'retryable': True, 'retry_after_seconds': 4}, 'request_id': 'fixture-request'} |
| 48 | + elif path in ('/v1/budget', '/v1/conflict', '/v1/jobs/not-owned'): |
| 49 | + status, code = {'/v1/budget': (402, 'BUDGET_EXCEEDED'), '/v1/conflict': (409, 'IDEMPOTENCY_CONFLICT'), '/v1/jobs/not-owned': (404, 'NOT_FOUND')}[path] |
| 50 | + payload = {'error': {'code': code, 'message': 'Fixture rejection', 'retryable': False}} |
| 51 | + elif path == '/v1/gateway': |
| 52 | + status, payload = 502, b'<html>gateway failed</html>' |
| 53 | + elif path == '/v1/error-shape': |
| 54 | + status, payload = 500, ['not an error object'] |
| 55 | + elif path == '/v1/malformed': |
| 56 | + payload = b'not-json' |
| 57 | + elif path == '/v1/array': |
| 58 | + payload = [] |
| 59 | + elif path == '/v1/slow': |
| 60 | + time.sleep(.3) |
| 61 | + elif path.endswith('/export'): |
| 62 | + content_type = 'text/plain; charset=utf-8' |
| 63 | + payload = '1\n00:00:00,000 --> 00:00:01,000\nCafé fixture\n'.encode() |
| 64 | + elif path.startswith('/v1/jobs/'): |
| 65 | + name = path.rsplit('/', 1)[-1] |
| 66 | + payload['status'] = name if name in ('succeeded', 'failed', 'cancelled', 'processing') else 'succeeded' |
| 67 | + if self.command == 'POST': |
| 68 | + status = 202 |
| 69 | + data = payload if isinstance(payload, bytes) else json.dumps(payload).encode() |
| 70 | + try: |
| 71 | + self.send_response(status) |
| 72 | + self.send_header('Content-Type', content_type) |
| 73 | + self.send_header('Content-Length', str(len(data))) |
| 74 | + self.end_headers() |
| 75 | + self.wfile.write(data) |
| 76 | + except (BrokenPipeError, ConnectionResetError): |
| 77 | + pass |
0 commit comments