#!/usr/bin/env python3 # Minimal mock of the WordPress REST API /wp/v2/posts endpoint, for testing # post.sh without a real WordPress install. # # Behavior is controlled by two files (paths given via env vars): # MOCK_WP_CONTROL_FILE - optional JSON {"status": int, "body": {...}} that # determines the response to the next request. If absent, responds # 201 with the submitted fields echoed back under an "id". # MOCK_WP_REQUEST_FILE - overwritten on every request with # {"path": ..., "payload": ..., "user_agent": ...} so a test can assert # on what was sent. # # Listens on 127.0.0.1:$MOCK_WP_PORT (default 8899). import http.server import json import os PORT = int(os.environ.get("MOCK_WP_PORT", "8899")) CONTROL_FILE = os.environ["MOCK_WP_CONTROL_FILE"] REQUEST_FILE = os.environ["MOCK_WP_REQUEST_FILE"] class Handler(http.server.BaseHTTPRequestHandler): def do_POST(self): length = int(self.headers.get("Content-Length", 0)) body = self.rfile.read(length) try: data = json.loads(body) if body else {} except json.JSONDecodeError: data = {"_raw": body.decode(errors="replace")} with open(REQUEST_FILE, "w") as f: json.dump({ "path": self.path, "payload": data, "user_agent": self.headers.get("User-Agent", ""), }, f) status, resp_body = 201, {"id": 1, **data} if os.path.exists(CONTROL_FILE): with open(CONTROL_FILE) as f: control = json.load(f) status = control.get("status", status) resp_body = control.get("body", resp_body) payload = json.dumps(resp_body).encode() self.send_response(status) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(payload))) self.end_headers() self.wfile.write(payload) def log_message(self, format, *args): pass if __name__ == "__main__": http.server.HTTPServer(("127.0.0.1", PORT), Handler).serve_forever()