Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
169 lines (152 loc) Β· 5.31 KB
/
Copy pathtest.py
File metadata and controls
169 lines (152 loc) Β· 5.31 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
"""
Test script to verify Flask API is working correctly
Run this after starting the Flask app to test all endpoints
"""
import requests
import json
# Base URL of your Flask app
BASE_URL = "http://localhost:5000"
def test_api():
print("π§ Testing Multi-Format Converter API\n")
print("=" * 50)
# Test data
test_json = {
"application": {
"name": "Test App",
"version": "1.0.0",
"features": ["json", "yaml", "xml"],
"settings": {
"theme": "dark",
"enabled": True
}
}
}
# Test 1: Convert JSON to YAML
print("\nπ Test 1: JSON β YAML Conversion")
try:
response = requests.post(
f"{BASE_URL}/api/convert",
json={
"input_format": "json",
"output_format": "yaml",
"content": json.dumps(test_json),
"prettify": True
}
)
if response.status_code == 200:
data = response.json()
if data.get("success"):
print("β
SUCCESS: JSON to YAML conversion working")
print("Output preview:")
print(data["output"][:200] + "..." if len(data["output"]) > 200 else data["output"])
else:
print("β FAILED: " + data.get("error", "Unknown error"))
else:
print(f"β FAILED: HTTP {response.status_code}")
except requests.exceptions.ConnectionError:
print("β FAILED: Cannot connect to Flask app. Is it running?")
except Exception as e:
print(f"β FAILED: {str(e)}")
# Test 2: Validate JSON
print("\nπ Test 2: JSON Validation")
try:
response = requests.post(
f"{BASE_URL}/api/validate",
json={
"format": "json",
"content": json.dumps(test_json)
}
)
if response.status_code == 200:
data = response.json()
if data.get("valid"):
print("β
SUCCESS: JSON validation working")
else:
print("β FAILED: Invalid JSON")
else:
print(f"β FAILED: HTTP {response.status_code}")
except:
print("β FAILED: Validation endpoint not responding")
# Test 3: Invalid JSON handling
print("\nπ Test 3: Invalid JSON Handling")
try:
response = requests.post(
f"{BASE_URL}/api/convert",
json={
"input_format": "json",
"output_format": "yaml",
"content": "{invalid json}",
"prettify": True
}
)
if response.status_code == 400:
print("β
SUCCESS: Invalid JSON properly rejected")
else:
print("β FAILED: Should return 400 for invalid JSON")
except:
print("β FAILED: Error handling test failed")
# Test 4: Convert JSON to XML
print("\nπ Test 4: JSON β XML Conversion")
try:
response = requests.post(
f"{BASE_URL}/api/convert",
json={
"input_format": "json",
"output_format": "xml",
"content": json.dumps(test_json),
"prettify": True
}
)
if response.status_code == 200:
data = response.json()
if data.get("success"):
print("β
SUCCESS: JSON to XML conversion working")
else:
print("β FAILED: " + data.get("error", "Unknown error"))
else:
print(f"β FAILED: HTTP {response.status_code}")
except:
print("β FAILED: XML conversion not working")
# Test 5: Prettify
print("\nπ Test 5: Prettify Function")
try:
response = requests.post(
f"{BASE_URL}/api/prettify",
json={
"format": "json",
"content": '{"compact":true,"no_spaces":123}'
}
)
if response.status_code == 200:
data = response.json()
if data.get("success"):
print("β
SUCCESS: Prettify working")
else:
print("β FAILED: " + data.get("error", "Unknown error"))
else:
print(f"β FAILED: HTTP {response.status_code}")
except:
print("β FAILED: Prettify endpoint not working")
print("\n" + "=" * 50)
print("\nπ― Testing Complete!\n")
print("If all tests passed, your Flask backend is ready!")
print("Open http://localhost:5000 in your browser to use the app.\n")
if __name__ == "__main__":
# Check if Flask is running
print("\nπ Checking Flask server...")
try:
response = requests.get(BASE_URL, timeout=2)
print("β
Flask server is running at", BASE_URL)
test_api()
except requests.exceptions.ConnectionError:
print("β Flask server is not running!")
print("\nTo start the server:")
print("1. Open a terminal")
print("2. Navigate to your project folder")
print("3. Run: python app.py")
print("\nThen run this test script again.")
except Exception as e:
print(f"β Error: {str(e)}")
# Usage:
# 1. Start Flask app: python app.py
# 2. Run this test: python test_api.py