-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_api_server.py
More file actions
111 lines (90 loc) · 3.12 KB
/
Copy pathmock_api_server.py
File metadata and controls
111 lines (90 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""
Mock API Server for local testing.
Simulates Jira, generic API, and Microsoft Graph endpoints.
Allows testing the sandbox without real external services.
"""
import json
import http.server
import socketserver
import random
import sys
PORT = 8080
class MockAPIHandler(http.server.BaseHTTPRequestHandler):
"""Mock API endpoints for testing."""
def log_message(self, fmt, *args):
print(f"[MockAPI] {fmt % args}")
def send_json(self, status: int, data: dict):
body = json.dumps(data).encode()
self.send_response(status)
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):
path = self.path
# Health check
if path == "/health":
self.send_json(200, {"status": "ok"})
return
# Mock Jira
if path.startswith("/jira/"):
if "myself" in path:
self.send_json(200, {
"displayName": "Test User",
"emailAddress": "test@example.com",
"accountId": "12345678"
})
elif "search" in path:
self.send_json(200, {
"issues": [
{"key": "TEST-1", "fields": {"summary": "Test issue 1"}},
{"key": "TEST-2", "fields": {"summary": "Test issue 2"}},
],
"total": 2
})
else:
self.send_json(200, {"result": "jira-ok"})
return
# Mock API
if path.startswith("/api/"):
if "status" in path:
self.send_json(200, {
"status": "operational",
"version": "1.0.0",
"timestamp": "2024-01-01T00:00:00Z"
})
else:
self.send_json(200, {"result": "api-ok"})
return
# Mock Graph
if path.startswith("/graph/"):
if "me" in path:
self.send_json(200, {
"displayName": "Test User",
"userPrincipalName": "test@example.com",
"id": "abcdef123456"
})
else:
self.send_json(200, {"result": "graph-ok"})
return
# 404 for unknown paths
self.send_json(404, {"error": "not found"})
class ReuseAddrTCPServer(socketserver.TCPServer):
allow_reuse_address = True
def main():
print(f"Starting Mock API Server on port {PORT}")
print("Endpoints:")
print(" GET /health - Health check")
print(" GET /jira/... - Mock Jira API")
print(" GET /api/... - Mock generic API")
print(" GET /graph/... - Mock Microsoft Graph")
print()
with ReuseAddrTCPServer(("", PORT), MockAPIHandler) as httpd:
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down...")
sys.exit(0)
if __name__ == "__main__":
main()