-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI _Testing.py
More file actions
137 lines (117 loc) · 4.6 KB
/
Copy pathAPI _Testing.py
File metadata and controls
137 lines (117 loc) · 4.6 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
import requests
import json
import re
from urllib.parse import urlparse
from enum import Enum
# ANSI Color Codes
COLOR_RED = "\033[91m"
COLOR_GREEN = "\033[92m"
COLOR_YELLOW = "\033[93m"
COLOR_BLUE = "\033[94m"
COLOR_MAGENTA = "\033[95m"
COLOR_CYAN = "\033[96m"
COLOR_RESET = "\033[0m"
class HttpMethod(Enum):
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
PATCH = "PATCH"
def colored_text(text: str, color: str) -> str:
return f"{color}{text}{COLOR_RESET}"
def validate_url(url: str) -> bool:
try:
result = urlparse(url)
return all([result.scheme in ['http', 'https'], result.netloc])
except ValueError:
return False
def validate_json(input_str: str) -> bool:
try:
json.loads(input_str)
return True
except json.JSONDecodeError:
return False
def get_valid_input(prompt: str, validation_func, error_msg: str) -> str:
while True:
user_input = input(prompt).strip()
if validation_func(user_input):
return user_input
print(colored_text(f"Invalid input: {error_msg}", COLOR_RED))
def select_http_method() -> HttpMethod:
print(colored_text("Select HTTP Method:", COLOR_CYAN))
for i, method in enumerate(HttpMethod, 1):
print(f"{i}. {method.value}")
while True:
choice = input(colored_text("Enter choice (1-5): ", COLOR_YELLOW))
if choice.isdigit() and 1 <= int(choice) <= len(HttpMethod):
return list(HttpMethod)[int(choice)-1]
print(colored_text("Invalid choice! Please select 1-5", COLOR_RED))
def format_headers(headers_str: str) -> dict:
try:
return json.loads(headers_str) if headers_str else {}
except json.JSONDecodeError:
print(colored_text("Warning: Headers not in valid JSON format. Using empty headers.", COLOR_YELLOW))
return {}
def test_api():
print(colored_text("\n=== API Testing Interface ===", COLOR_MAGENTA))
# Get user inputs with validation
method = select_http_method()
url = get_valid_input(
colored_text("Enter API URL: ", COLOR_YELLOW),
validate_url,
"Must be a valid URL starting with http:// or https://"
)
headers = {}
if input(colored_text("Add custom headers? (y/N): ", COLOR_YELLOW)).lower() == 'y':
headers_str = get_valid_input(
colored_text("Enter headers as JSON (e.g., {\"Content-Type\":\"application/json\"}): ", COLOR_YELLOW),
lambda x: x == '' or validate_json(x),
"Must be valid JSON or empty"
)
headers = format_headers(headers_str)
params = {}
if input(colored_text("Add URL parameters? (y/N): ", COLOR_YELLOW)).lower() == 'y':
params_str = get_valid_input(
colored_text("Enter parameters as JSON (e.g., {\"key\":\"value\"}): ", COLOR_YELLOW),
lambda x: x == '' or validate_json(x),
"Must be valid JSON or empty"
)
params = json.loads(params_str) if params_str else {}
data = None
if method != HttpMethod.GET and input(colored_text("Add request body? (y/N): ", COLOR_YELLOW)).lower() == 'y':
data_str = get_valid_input(
colored_text("Enter request body as JSON: ", COLOR_YELLOW),
validate_json,
"Must be valid JSON"
)
data = json.loads(data_str)
# Execute request
try:
response = requests.request(
method=method.value,
url=url,
headers=headers,
params=params,
json=data,
timeout=10
)
print(colored_text("\n=== Response Details ===", COLOR_BLUE))
print(colored_text(f"Status Code: {response.status_code}",
COLOR_GREEN if response.ok else COLOR_RED))
print(colored_text(f"Response Time: {response.elapsed.total_seconds():.2f}s", COLOR_CYAN))
try:
formatted_json = json.dumps(response.json(), indent=2)
print(colored_text("\nResponse Body:", COLOR_BLUE))
print(formatted_json)
except json.JSONDecodeError:
print(colored_text("\nResponse Body (non-JSON):", COLOR_BLUE))
print(response.text)
except requests.exceptions.RequestException as e:
print(colored_text(f"\nRequest failed: {str(e)}", COLOR_RED))
def main():
while True:
test_api()
if input(colored_text("\nTest another API? (y/N): ", COLOR_YELLOW)).lower() != 'y':
break
if __name__ == "__main__":
main()