-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-frontend-backend.html
More file actions
278 lines (248 loc) · 10.3 KB
/
test-frontend-backend.html
File metadata and controls
278 lines (248 loc) · 10.3 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<html>
<head>
<title>Test Frontend-Backend Connection</title>
<style>
body { font-family: Arial; padding: 20px; background: #f5f5f5; }
.test { margin: 10px 0; padding: 10px; border-radius: 5px; }
.success { background: #d4edda; color: #155724; }
.error { background: #f8d7da; color: #721c24; }
.info { background: #d1ecf1; color: #0c5460; }
pre { background: white; padding: 10px; border-radius: 3px; overflow-x: auto; }
button { padding: 10px 20px; margin: 5px; cursor: pointer; background: #007bff; color: white; border: none; border-radius: 5px; }
button:hover { background: #0056b3; }
</style>
</head>
<body>
<h1>EmberLearn Frontend-Backend Connection Test</h1>
<button onclick="runAllTests()">Run All Tests</button>
<button onclick="clearResults()">Clear Results</button>
<div id="results"></div>
<script>
const API_URL = 'http://localhost:8000';
const results = document.getElementById('results');
function addResult(title, message, type = 'info', data = null) {
const div = document.createElement('div');
div.className = `test ${type}`;
div.innerHTML = `<strong>${title}</strong><br>${message}`;
if (data) {
const pre = document.createElement('pre');
pre.textContent = JSON.stringify(data, null, 2);
div.appendChild(pre);
}
results.appendChild(div);
}
function clearResults() {
results.innerHTML = '';
}
async function testBackendHealth() {
addResult('Test 1', 'Testing backend health...', 'info');
try {
const response = await fetch(`${API_URL}/api/exercises`);
if (response.ok) {
const data = await response.json();
addResult(
'Test 1: Backend Health',
`✓ Backend is running and responding. Found ${data.length} exercises.`,
'success',
data.slice(0, 1)
);
return true;
} else {
addResult(
'Test 1: Backend Health',
`✗ Backend returned error: ${response.status}`,
'error'
);
return false;
}
} catch (error) {
addResult(
'Test 1: Backend Health',
`✗ Cannot connect to backend: ${error.message}`,
'error'
);
return false;
}
}
async function testCORS() {
addResult('Test 2', 'Testing CORS configuration...', 'info');
try {
const response = await fetch(`${API_URL}/api/exercises`, {
method: 'GET',
headers: {
'Origin': 'http://localhost:3000'
}
});
const corsHeader = response.headers.get('Access-Control-Allow-Origin');
if (corsHeader) {
addResult(
'Test 2: CORS',
`✓ CORS is configured. Allow-Origin: ${corsHeader}`,
'success'
);
return true;
} else {
addResult(
'Test 2: CORS',
`⚠ CORS header not found. This might cause issues.`,
'error'
);
return false;
}
} catch (error) {
addResult(
'Test 2: CORS',
`✗ CORS test failed: ${error.message}`,
'error'
);
return false;
}
}
async function testAuthentication() {
addResult('Test 3', 'Testing authentication...', 'info');
// Try to register/login
const testEmail = `test${Date.now()}@example.com`;
const testPassword = 'password123';
try {
// Try to register
const registerResponse = await fetch(`${API_URL}/api/auth/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: testEmail,
password: testPassword,
name: 'Test User'
})
});
if (registerResponse.ok) {
const data = await registerResponse.json();
localStorage.setItem('token', data.token);
addResult(
'Test 3: Authentication',
`✓ Registration successful. Token saved to localStorage.`,
'success',
{ user: data.user, token: data.token.substring(0, 20) + '...' }
);
return true;
} else {
const error = await registerResponse.json();
addResult(
'Test 3: Authentication',
`⚠ Registration failed (might already exist): ${error.detail}`,
'info'
);
// Try to login instead
const loginResponse = await fetch(`${API_URL}/api/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'test@example.com',
password: 'password123'
})
});
if (loginResponse.ok) {
const data = await loginResponse.json();
localStorage.setItem('token', data.token);
addResult(
'Test 3: Authentication',
`✓ Login successful. Token saved to localStorage.`,
'success'
);
return true;
} else {
addResult(
'Test 3: Authentication',
`✗ Both registration and login failed.`,
'error'
);
return false;
}
}
} catch (error) {
addResult(
'Test 3: Authentication',
`✗ Authentication test failed: ${error.message}`,
'error'
);
return false;
}
}
async function testExercisesWithAuth() {
addResult('Test 4', 'Testing exercises endpoint with authentication...', 'info');
const token = localStorage.getItem('token');
if (!token) {
addResult(
'Test 4: Exercises with Auth',
`✗ No token found. Please run Test 3 first.`,
'error'
);
return false;
}
try {
const response = await fetch(`${API_URL}/api/exercises`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (response.ok) {
const data = await response.json();
addResult(
'Test 4: Exercises with Auth',
`✓ Successfully fetched ${data.length} exercises with authentication.`,
'success',
data.slice(0, 2)
);
return true;
} else {
const error = await response.text();
addResult(
'Test 4: Exercises with Auth',
`✗ Failed to fetch exercises: ${error}`,
'error'
);
return false;
}
} catch (error) {
addResult(
'Test 4: Exercises with Auth',
`✗ Request failed: ${error.message}`,
'error'
);
return false;
}
}
async function runAllTests() {
clearResults();
addResult('Starting Tests', 'Running all connection tests...', 'info');
const test1 = await testBackendHealth();
if (!test1) return;
const test2 = await testCORS();
const test3 = await testAuthentication();
const test4 = await testExercisesWithAuth();
if (test1 && test2 && test3 && test4) {
addResult(
'All Tests Complete',
'✓ All tests passed! The frontend can communicate with the backend.',
'success'
);
addResult(
'Next Steps',
'Open http://localhost:3000/exercises in your browser. If exercises still don\'t show, check the browser console for errors (F12).',
'info'
);
} else {
addResult(
'Tests Failed',
'✗ Some tests failed. Please fix the issues above.',
'error'
);
}
}
// Auto-run on load
window.onload = () => {
addResult('Ready', 'Click "Run All Tests" to start testing.', 'info');
};
</script>
</body>
</html>