|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import json |
| 3 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 4 | + |
| 5 | +class Handler(BaseHTTPRequestHandler): |
| 6 | + def _send_json(self, code, payload): |
| 7 | + data = json.dumps(payload).encode("utf-8") |
| 8 | + self.send_response(code) |
| 9 | + self.send_header("Content-Type", "application/json") |
| 10 | + self.send_header("X-Upstream", "n8n-mock") |
| 11 | + self.send_header("Content-Length", str(len(data))) |
| 12 | + self.end_headers() |
| 13 | + self.wfile.write(data) |
| 14 | + |
| 15 | + def do_GET(self): |
| 16 | + self._handle() |
| 17 | + |
| 18 | + def do_HEAD(self): |
| 19 | + self._handle(head=True) |
| 20 | + |
| 21 | + def do_POST(self): |
| 22 | + self._handle() |
| 23 | + |
| 24 | + def _handle(self, head=False): |
| 25 | + path = self.path |
| 26 | + if path == "/webhook/backup/status-public": |
| 27 | + payload = {"status": "ok", "backupName": "paperless", "checkedAt": "2026-05-23T00:00:00Z"} |
| 28 | + if head: |
| 29 | + self.send_response(200) |
| 30 | + self.send_header("Content-Type", "application/json") |
| 31 | + self.send_header("X-Upstream", "n8n-mock") |
| 32 | + self.end_headers() |
| 33 | + else: |
| 34 | + self._send_json(200, payload) |
| 35 | + return |
| 36 | + |
| 37 | + if path == "/webhook/backup/names": |
| 38 | + payload = {"count": 1, "backupNames": ["paperless"], "backups": [{"backupName": "paperless", "status": "ok"}]} |
| 39 | + if head: |
| 40 | + self.send_response(200) |
| 41 | + self.send_header("Content-Type", "application/json") |
| 42 | + self.send_header("X-Upstream", "n8n-mock") |
| 43 | + self.end_headers() |
| 44 | + else: |
| 45 | + self._send_json(200, payload) |
| 46 | + return |
| 47 | + |
| 48 | + if path == "/webhook/landing/api/me": |
| 49 | + payload = {"user": "test-user", "groups": ["admins"], "role": "admin"} |
| 50 | + if head: |
| 51 | + self.send_response(200) |
| 52 | + self.send_header("Content-Type", "application/json") |
| 53 | + self.send_header("X-Upstream", "n8n-mock") |
| 54 | + self.end_headers() |
| 55 | + else: |
| 56 | + self._send_json(200, payload) |
| 57 | + return |
| 58 | + |
| 59 | + # Generic webhook sink for protected workflow endpoints. |
| 60 | + if path.startswith("/webhook/") or path.startswith("/webhook-test/") or path.startswith("/rest/oauth2-credential"): |
| 61 | + payload = {"ok": True, "path": path, "method": self.command} |
| 62 | + if head: |
| 63 | + self.send_response(200) |
| 64 | + self.send_header("Content-Type", "application/json") |
| 65 | + self.send_header("X-Upstream", "n8n-mock") |
| 66 | + self.end_headers() |
| 67 | + else: |
| 68 | + self._send_json(200, payload) |
| 69 | + return |
| 70 | + |
| 71 | + self._send_json(404, {"error": "not found", "path": path}) |
| 72 | + |
| 73 | + def log_message(self, fmt, *args): |
| 74 | + return |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + HTTPServer(("0.0.0.0", 5678), Handler).serve_forever() |
0 commit comments