60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
"""Tiny stand-in for the backend, for testing the app without a real connection.
|
|
|
|
Run: python tools/mock_server.py
|
|
Then point the app at it: $env:NOTIFIER_URL = "http://127.0.0.1:8000/internal/pull-notifications"
|
|
|
|
Every request gets the same answer: MESSAGE_COUNT messages and has_more=true.
|
|
Uses only the standard library.
|
|
"""
|
|
|
|
import json
|
|
import time
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
|
HOST = "127.0.0.1"
|
|
PORT = 8000
|
|
PATH = "/internal/pull-notifications"
|
|
MESSAGE_COUNT = 10
|
|
HAS_MORE = True
|
|
|
|
|
|
def build_payload() -> dict:
|
|
now = int(time.time())
|
|
messages = [
|
|
{
|
|
"title": f"Test notification {i}",
|
|
"body": f"Test message {i} of {MESSAGE_COUNT}",
|
|
"category": "podcasts",
|
|
"timestamp": now - (MESSAGE_COUNT - i),
|
|
"link": "https://www.example.com",
|
|
}
|
|
for i in range(1, MESSAGE_COUNT + 1)
|
|
]
|
|
return {"messages": messages, "count": len(messages), "has_more": HAS_MORE}
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def _respond(self) -> None:
|
|
if self.path != PATH:
|
|
self.send_error(404)
|
|
return
|
|
body = json.dumps(build_payload()).encode()
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Content-Length", str(len(body)))
|
|
self.end_headers()
|
|
self.wfile.write(body)
|
|
|
|
def do_GET(self) -> None:
|
|
self._respond()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
server = ThreadingHTTPServer((HOST, PORT), Handler)
|
|
print(f"Mock server on http://{HOST}:{PORT}{PATH} (Ctrl+C to stop)")
|
|
try:
|
|
server.serve_forever()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
server.server_close() |