-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·263 lines (229 loc) · 9.23 KB
/
test.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python3
import requests
import sys
from typing import Optional, Union
# Set test server address
HOST = "http://localhost:8080"
# ANSI color codes
GREEN = '\033[0;32m'
RED = '\033[0;31m'
NC = '\033[0m'
# Add test statistics variables
test_stats = {
'total': 0,
'passed': 0,
'failed': 0
}
def test_endpoint(
url: str,
*,
method: str = "GET",
expected_code: int = 200,
expected_response: Optional[Union[str, dict, list]] = None,
expected_content_type: Optional[str] = None,
expected_substring: Optional[str] = None,
) -> None:
"""
Test a single endpoint
Args:
url: Request path
method: HTTP method
expected_code: Expected status code
expected_response: Expected response content. Can be:
- string: for substring matching
- dict/list: for complete JSON matching
expected_substring: Expected substring in response content (optional)
expected_content_type: Expected Content-Type (optional)
"""
test_stats['total'] += 1
print(f"Testing {method} {url}")
try:
response = requests.request(method, f"{HOST}{url}")
code = response.status_code
content_type = response.headers.get('content-type', '')
# Check status code
if code != expected_code:
print(f"{RED}✗ Status code mismatch{NC}")
print(f"Expected: {expected_code}")
print(f"Got: {code}")
test_stats['failed'] += 1
print()
return
# Check Content-Type
if expected_content_type is not None:
if expected_content_type not in content_type:
print(f"{RED}✗ Content-Type mismatch{NC}")
print(f"Expected: {expected_content_type}")
print(f"Got: {content_type}")
test_stats['failed'] += 1
print()
return
# If no expected response content, test passes
if expected_response is None:
print(f"{GREEN}✓ Test passed{NC}")
test_stats['passed'] += 1
print()
return
# Handle JSON response
if 'application/json' in content_type:
try:
actual_json = response.json()
if actual_json == expected_response:
print(f"{GREEN}✓ Test passed{NC}")
test_stats['passed'] += 1
else:
print(f"{RED}✗ JSON response mismatch{NC}")
print(f"Expected: {expected_response}")
print(f"Got: {actual_json}")
test_stats['failed'] += 1
except ValueError:
print(f"{RED}✗ Response is not valid JSON{NC}")
print(f"Got: {response.text}")
test_stats['failed'] += 1
# Handle text response
else:
actual_text = response.text
if isinstance(expected_response, str):
if expected_response == actual_text:
print(f"{GREEN}✓ Test passed{NC}")
test_stats['passed'] += 1
else:
print(f"{RED}✗ Response content mismatch{NC}")
print(f"Expected: {repr(expected_response)}")
print(f"Got: {repr(actual_text)}")
test_stats['failed'] += 1
else:
print(f"{RED}✗ Response type mismatch{NC}")
print(f"Expected JSON but got text: {actual_text}")
test_stats['failed'] += 1
except requests.RequestException as e:
print(f"{RED}✗ Request failed: {e}{NC}")
test_stats['failed'] += 1
print()
def run_tests():
"""Run all test cases"""
# 1. Test static path
test_endpoint("/hello", expected_response="Hello World", expected_content_type="text/plain")
test_endpoint("/hello-201", expected_code=201, expected_response="Hello World", expected_content_type="text/plain")
# 2. Test JSON response
test_endpoint("/json",
expected_response={"message": "success", "code": 0},
expected_content_type="application/json")
# 3. Test dynamic path parameters
test_endpoint("/users/123",
expected_response={"id": 123, "type": "number"},
expected_content_type="application/json")
test_endpoint("/users/john",
expected_response={"name": "john", "type": "string"},
expected_content_type="application/json")
# 4. Test regex path
test_endpoint("/version/1.0",
expected_response={"version": "1.0"},
expected_content_type="application/json")
test_endpoint("/version/abc",
expected_code=404,
expected_response="match route failed")
# 5. Test wildcard
test_endpoint("/files/path/to/file.txt",
expected_response="/path/to/file.txt")
# 6. Test HTTP methods
test_endpoint("/accounts",
method="POST",
expected_response={"method": "POST"},
expected_content_type="application/json")
test_endpoint("/accounts/123",
method="PUT",
expected_response={"method": "PUT", "id": 123},
expected_content_type="application/json")
test_endpoint("/accounts", method="DELETE", expected_code=405)
# 7. Test error handling
test_endpoint("/error",
expected_code=500,
expected_substring="Test Error")
test_endpoint("/custom-error",
expected_code=500,
expected_response={"code": 400, "message": "Custom Error"},
expected_content_type="application/json")
test_endpoint("/return-error",
expected_code=402,
expected_response="Parameter Error")
test_endpoint("/handled-error",
expected_code=500,
expected_content_type="text/plain",
expected_response="handled error")
# 8. Test status code
test_endpoint("/404", expected_code=404, expected_response="Not Found")
# 9. Test HTML response
test_endpoint("/html",
expected_response="<h1>Hello HTML</h1>",
expected_content_type="text/html")
test_endpoint("/html-error",
expected_code=501,
expected_content_type="text/html",
expected_response="<h1>Hello HTML error</h1>")
# 10. Test function return
test_endpoint("/func", expected_response="function called")
# 11. Test events
test_endpoint("/add", expected_response=0)
test_endpoint("/events", expected_response=1)
# 12. Test ctx.response.body
test_endpoint("/response-body", expected_response="response body")
# 13. Test fs
test_endpoint("/account/users", expected_response=[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}])
# Test array-style route definitions
test_endpoint("/array_group/bloggers",
expected_response=[
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
],
expected_content_type="application/json")
test_endpoint("/array_group/bloggers/Alice",
expected_response={"name": "Alice"},
expected_content_type="application/json")
test_endpoint("/array_group/bloggers/123/posts",
expected_response={
"user_id": 123,
"posts": [
{"id": 1, "title": "Post 1"},
{"id": 2, "title": "Post 2"}
]
},
expected_content_type="application/json")
# Test hash-style route definitions
test_endpoint("/hash_group/list",
expected_response=[
{"id": 1, "type": "item1"},
{"id": 2, "type": "item2"}
],
expected_content_type="application/json")
test_endpoint("/hash_group/detail/42",
expected_response={
"id": 42,
"type": "detail",
"description": "Item details"
},
expected_content_type="application/json")
test_endpoint("/hash_group/search/test",
expected_response={
"keyword": "test",
"results": [
{"id": 1, "match": True},
{"id": 2, "match": False}
]
},
expected_content_type="application/json")
if __name__ == "__main__":
try:
run_tests()
# Print test statistics
print("Test Statistics:")
print(f"Total: {test_stats['total']} tests")
print(f"{GREEN}Passed: {test_stats['passed']}{NC}")
if test_stats['failed'] > 0:
print(f"{RED}Failed: {test_stats['failed']}{NC}")
print(f"Pass rate: {(test_stats['passed'] / test_stats['total'] * 100):.1f}%")
# Set exit code based on whether there were any failed tests
sys.exit(1 if test_stats['failed'] > 0 else 0)
except KeyboardInterrupt:
print("\nTests interrupted")
sys.exit(1)