-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathapp-test.py
More file actions
180 lines (155 loc) · 5.08 KB
/
Copy pathapp-test.py
File metadata and controls
180 lines (155 loc) · 5.08 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""
TEST VERSION of the HTTP server with MODIFIED responses.
Use this for TESTING mode.
EXPECTED RESULTS (Total 10):
PASS: 7
FAIL: 3 (Missing Field, Type Mismatch, Hierarchy Mismatch)
"""
import socket
import json
import random
import string
import time
PORT = 5000
RESPONSES = {
# 1. PASS: Different values, same types
'/user/profile': {
"id": 999,
"username": "different_user",
"active": False,
"profile": {
"age": 99,
"city": "New York",
"preferences": {"theme": "light", "notifications": False}
},
"roles": ["viewer"]
},
# 2. PASS: Array length diff (Standard schema matching allows this)
'/user/history': {
"user_id": 999,
"login_history": [
{"ip": "1.1.1.1", "timestamp": 9999999999}
]
},
# 3. PASS: Extra fields (Superset is allowed)
'/product/search': {
"query": "laptop",
"total_results": 1500,
"page": 1,
"items": [
{"id": "p1", "name": "Laptop Pro", "price": 1299.99, "stock": 50},
{"id": "p2", "name": "Laptop Air", "price": 999.99, "stock": 0}
],
"extra_field": "This field was not in original",
"metadata": {"source": "api", "cache": True}
},
# 4. FAIL: FIELD DELETION (Missing 'feature_flags')
'/admin/config': {
"maintenance_mode": False,
# "feature_flags": MISSING! -> Should Fail
"deprecated_since": None,
"retry_limit": 3,
"added_config": "extra"
},
# 5. FAIL: TYPE MISMATCH (Int -> String)
'/data/matrix': {
"matrix": [["1", "0", "0"], ["0", "1", "0"], ["0", "0", "1"]],
"dimension": "3x3"
},
# 6. FAIL: HIERARCHY/TUPLE MISMATCH
# Original: [int, string, bool...]
# Here: [string, int, bool...] (Swapped first two)
'/data/mixed_array': {
"mixed": ["string", 1, True, {"obj": "val"}, None, [1, 2]]
},
# 7. PASS: Exact match
'/edge/empty_response': {},
# 8. PASS: Exact match
'/edge/null_root': None,
# 9. PASS: Exact match
'/edge/special_chars': {
"text": "Hello Hello",
"emoji": "🚀 🔥 🐛",
"symbols": "!@#$%^&*()_+-=[]{}|;':\",./<>?",
"path": "C:\\Program Files\\Keploy"
},
# 10. PASS: Exact match
'/edge/nested_null': {
"data": {"value": None}
},
# 11. FAIL: Complex User (Type mismatch + Missing nested key)
'/complex/user': {
"id": "500", # FAIL: Expected int, got string
"name": "Jane Doe",
"contact": {
"email": "jane@example.com"
# FAIL: Missing "phone" key
},
"tags": ["vip", "early-adopter"],
"metadata": {
"created_at": "2023-01-01T00:00:00Z",
"login_count": 42
}
},
# 12. FAIL: Complex Product (Array item mismatch)
'/complex/product': {
"sku": "XYZ-999",
"specs": [
{"key": "weight", "value": "1.5kg", "unit": "kg"}, # FAIL: value expected float, got string
{"key": "warranty", "value": 2, "unit": "years"}
],
"in_stock": True,
"dimensions": [10, "20", 5.5] # FAIL: 2nd element expected int/float, got string
}
}
def get_large_payload():
return {
"payload_size": "5KB",
"content": "".join(random.choices(string.ascii_letters, k=5000))
}
def handle_request(client_socket):
try:
client_socket.settimeout(5.0)
request = client_socket.recv(4096).decode('utf-8', errors='ignore')
if not request: return
lines = request.split('\r\n')
if not lines: return
request_line = lines[0]
parts = request_line.split(' ')
if len(parts) < 2: return
path = parts[1]
print(f"Test Request: {path}")
if path == '/edge/large_payload':
body_data = get_large_payload()
elif path in RESPONSES:
body_data = RESPONSES[path]
else:
response = "HTTP/1.0 404 Not Found\r\n\r\n"
client_socket.sendall(response.encode('utf-8'))
return
if body_data is None:
body = "null"
else:
body = json.dumps(body_data)
response = f"HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nContent-Length: {len(body)}\r\nConnection: close\r\n\r\n{body}"
client_socket.sendall(response.encode('utf-8'))
except Exception as e:
print(f"Error: {e}")
finally:
client_socket.close()
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('0.0.0.0', PORT))
server_socket.listen(10)
print(f"Starting TEST Server on port {PORT}...")
while True:
try:
client_socket, addr = server_socket.accept()
handle_request(client_socket)
except KeyboardInterrupt:
break
server_socket.close()
if __name__ == "__main__":
main()