| 10 | |
| 11 | |
| 12 | class UDSFaker: |
| 13 | |
| 14 | def __init__(self, path): |
| 15 | self._uds_path = path |
| 16 | self._done = False |
| 17 | |
| 18 | def start(self): |
| 19 | def process(self): |
| 20 | self._socket.listen(1) |
| 21 | self._process() |
| 22 | |
| 23 | try: |
| 24 | os.unlink(self._uds_path) |
| 25 | except OSError: |
| 26 | if os.path.exists(self._uds_path): |
| 27 | raise |
| 28 | self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 29 | self._socket.bind(self._uds_path) |
| 30 | self._thread = Thread(target=process, daemon=True, args=[self]) |
| 31 | self._thread.start() |
| 32 | |
| 33 | def stop(self): |
| 34 | self._done = True |
| 35 | self._socket.close() |
| 36 | |
| 37 | def _process(self): |
| 38 | while self._done is False: |
| 39 | try: |
| 40 | c, client_address = self._socket.accept() |
| 41 | try: |
| 42 | data = c.recv(16) |
| 43 | c.sendall("""HTTP/1.1 200 Ok |
| 44 | Server: UdsFaker |
| 45 | Content-Type: application/json |
| 46 | Content-Length: 19 |
| 47 | |
| 48 | { "host": "faked" }""".encode()) |
| 49 | finally: |
| 50 | c.close() |
| 51 | |
| 52 | except ConnectionAbortedError: |
| 53 | self._done = True |
| 54 | |
| 55 | |
| 56 | class TestProxyUds: |