-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-test-ui.html
More file actions
448 lines (394 loc) · 15.3 KB
/
api-test-ui.html
File metadata and controls
448 lines (394 loc) · 15.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sesh-Tracker API Tester</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 1000px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
margin-top: 0;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
input, select, textarea {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
.response {
margin-top: 20px;
padding: 15px;
background-color: #f8f8f8;
border: 1px solid #ddd;
border-radius: 4px;
white-space: pre-wrap;
font-family: monospace;
max-height: 300px;
overflow-y: auto;
}
.endpoints {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 20px;
}
.endpoint-btn {
background-color: #2196F3;
}
.tabs {
display: flex;
margin-bottom: 20px;
border-bottom: 1px solid #ddd;
}
.tab {
padding: 10px 15px;
cursor: pointer;
background-color: #f1f1f1;
border: 1px solid #ddd;
border-bottom: none;
margin-right: 5px;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
.tab.active {
background-color: white;
border-bottom-color: white;
margin-bottom: -1px;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1>Sesh-Tracker API Tester</h1>
<div class="tabs">
<div class="tab active" data-tab="request-builder">Request Builder</div>
<div class="tab" data-tab="batch-test">Batch Test</div>
</div>
<div id="request-builder" class="tab-content active">
<div class="form-group">
<label for="base-url">Base URL:</label>
<input type="text" id="base-url" value="http://localhost:4001/api/v1">
</div>
<div class="form-group">
<label for="endpoint">Endpoint:</label>
<input type="text" id="endpoint" value="/health">
</div>
<div class="endpoints">
<button class="endpoint-btn" data-endpoint="/health">Health</button>
<button class="endpoint-btn" data-endpoint="/mood">Get Moods</button>
<button class="endpoint-btn" data-endpoint="/medical-symptoms">Get Symptoms</button>
<button class="endpoint-btn" data-endpoint="/journal">Get Journal</button>
</div>
<div class="form-group">
<label for="method">Method:</label>
<select id="method">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div class="form-group">
<label for="auth-type">Authentication:</label>
<select id="auth-type">
<option value="none">None</option>
<option value="bearer">Bearer Token Only</option>
<option value="mock">Mock User Type Only</option>
<option value="both">Both Bearer + Mock</option>
</select>
</div>
<div class="form-group" id="token-group" style="display: none;">
<label for="bearer-token">Bearer Token:</label>
<input type="text" id="bearer-token" value="mock-token-for-development">
</div>
<div class="form-group" id="mock-group" style="display: none;">
<label for="mock-user">Mock User Type:</label>
<select id="mock-user">
<option value="test">Test User</option>
<option value="demo">Demo User</option>
<option value="admin">Admin User</option>
</select>
</div>
<div class="form-group" id="body-group" style="display: none;">
<label for="request-body">Request Body (JSON):</label>
<textarea id="request-body" rows="10"></textarea>
</div>
<button id="send-request">Send Request</button>
<div class="response" id="response"></div>
</div>
<div id="batch-test" class="tab-content">
<div class="form-group">
<label for="batch-base-url">Base URL:</label>
<input type="text" id="batch-base-url" value="http://localhost:4001/api/v1">
</div>
<div class="form-group">
<label for="batch-auth-type">Authentication:</label>
<select id="batch-auth-type">
<option value="none">None</option>
<option value="bearer">Bearer Token Only</option>
<option value="mock">Mock User Type Only</option>
<option value="both">Both Bearer + Mock</option>
</select>
</div>
<div class="form-group" id="batch-token-group" style="display: none;">
<label for="batch-bearer-token">Bearer Token:</label>
<input type="text" id="batch-bearer-token" value="mock-token-for-development">
</div>
<div class="form-group" id="batch-mock-group" style="display: none;">
<label for="batch-mock-user">Mock User Type:</label>
<select id="batch-mock-user">
<option value="test">Test User</option>
<option value="demo">Demo User</option>
<option value="admin">Admin User</option>
</select>
</div>
<button id="run-batch-test">Run All Tests</button>
<div class="response" id="batch-response"></div>
</div>
</div>
<script>
// Test data for requests
const testData = {
mood: {
userId: 'test-user-id',
rating: 8,
note: 'Feeling great today!',
tags: ['relaxed', 'happy', 'productive'],
createdAt: new Date().toISOString()
},
symptom: {
userId: 'test-user-id',
symptom: 'Headache',
severity: 5,
duration: '2 hours',
notes: 'Mild headache after working on computer',
tags: ['stress', 'work'],
createdAt: new Date().toISOString()
},
journal: {
userId: 'test-user-id',
title: 'Today\'s Reflections',
content: 'Had a productive day working on my wellness tracking features...',
tags: ['coding', 'wellness', 'productivity'],
mood: 7,
isPrivate: true,
createdAt: new Date().toISOString()
}
};
// Tab switching
document.querySelectorAll('.tab').forEach(tab => {
tab.addEventListener('click', () => {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
tab.classList.add('active');
document.getElementById(tab.dataset.tab).classList.add('active');
});
});
// Method change handler
document.getElementById('method').addEventListener('change', function() {
const bodyGroup = document.getElementById('body-group');
if (this.value === 'POST' || this.value === 'PUT') {
bodyGroup.style.display = 'block';
// Set appropriate test data based on endpoint
const endpoint = document.getElementById('endpoint').value;
if (endpoint.includes('mood')) {
document.getElementById('request-body').value = JSON.stringify(testData.mood, null, 2);
} else if (endpoint.includes('medical-symptoms')) {
document.getElementById('request-body').value = JSON.stringify(testData.symptom, null, 2);
} else if (endpoint.includes('journal')) {
document.getElementById('request-body').value = JSON.stringify(testData.journal, null, 2);
}
} else {
bodyGroup.style.display = 'none';
}
});
// Auth type change handlers
document.getElementById('auth-type').addEventListener('change', function() {
const tokenGroup = document.getElementById('token-group');
const mockGroup = document.getElementById('mock-group');
if (this.value === 'bearer' || this.value === 'both') {
tokenGroup.style.display = 'block';
} else {
tokenGroup.style.display = 'none';
}
if (this.value === 'mock' || this.value === 'both') {
mockGroup.style.display = 'block';
} else {
mockGroup.style.display = 'none';
}
});
document.getElementById('batch-auth-type').addEventListener('change', function() {
const tokenGroup = document.getElementById('batch-token-group');
const mockGroup = document.getElementById('batch-mock-group');
if (this.value === 'bearer' || this.value === 'both') {
tokenGroup.style.display = 'block';
} else {
tokenGroup.style.display = 'none';
}
if (this.value === 'mock' || this.value === 'both') {
mockGroup.style.display = 'block';
} else {
mockGroup.style.display = 'none';
}
});
// Endpoint button clicks
document.querySelectorAll('.endpoint-btn').forEach(button => {
button.addEventListener('click', () => {
document.getElementById('endpoint').value = button.dataset.endpoint;
// Reset method to GET
document.getElementById('method').value = 'GET';
document.getElementById('body-group').style.display = 'none';
});
});
// Send request
document.getElementById('send-request').addEventListener('click', async () => {
const baseUrl = document.getElementById('base-url').value;
const endpoint = document.getElementById('endpoint').value;
const method = document.getElementById('method').value;
const authType = document.getElementById('auth-type').value;
const responseEl = document.getElementById('response');
responseEl.textContent = 'Sending request...';
try {
const headers = { 'Content-Type': 'application/json' };
if (authType === 'bearer' || authType === 'both') {
const token = document.getElementById('bearer-token').value;
headers['Authorization'] = `Bearer ${token}`;
}
if (authType === 'mock' || authType === 'both') {
const mockUserType = document.getElementById('mock-user').value;
headers['X-Mock-User-Type'] = mockUserType;
}
const options = { method, headers };
if (method === 'POST' || method === 'PUT') {
const bodyText = document.getElementById('request-body').value;
options.body = bodyText;
}
const url = `${baseUrl}${endpoint}`;
const response = await fetch(url, options);
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
const json = await response.json();
responseEl.textContent = `Status: ${response.status}\n\n${JSON.stringify(json, null, 2)}`;
} else {
const text = await response.text();
responseEl.textContent = `Status: ${response.status}\n\n${text || '(No response body)'}`;
}
} catch (error) {
responseEl.textContent = `Error: ${error.message}`;
}
});
// Batch testing
document.getElementById('run-batch-test').addEventListener('click', async () => {
const baseUrl = document.getElementById('batch-base-url').value;
const authType = document.getElementById('batch-auth-type').value;
const responseEl = document.getElementById('batch-response');
responseEl.textContent = 'Running batch tests...\n';
// Prepare auth headers
const headers = { 'Content-Type': 'application/json' };
if (authType === 'bearer' || authType === 'both') {
const token = document.getElementById('batch-bearer-token').value;
headers['Authorization'] = `Bearer ${token}`;
}
if (authType === 'mock' || authType === 'both') {
const mockUserType = document.getElementById('batch-mock-user').value;
headers['X-Mock-User-Type'] = mockUserType;
}
// Define test endpoints
const tests = [
{ name: 'Health Check', endpoint: '/health', method: 'GET' },
{ name: 'Get Moods', endpoint: '/mood', method: 'GET' },
{ name: 'Create Mood', endpoint: '/mood', method: 'POST', body: testData.mood },
{ name: 'Get Symptoms', endpoint: '/medical-symptoms', method: 'GET' },
{ name: 'Create Symptom', endpoint: '/medical-symptoms', method: 'POST', body: testData.symptom },
{ name: 'Get Journal Entries', endpoint: '/journal', method: 'GET' },
{ name: 'Create Journal Entry', endpoint: '/journal', method: 'POST', body: testData.journal }
];
// Run each test
let allPassed = true;
for (const test of tests) {
try {
responseEl.textContent += `\n----- Testing ${test.name} -----\n`;
const options = {
method: test.method,
headers: { ...headers }
};
if (test.body && (test.method === 'POST' || test.method === 'PUT')) {
options.body = JSON.stringify(test.body);
}
const url = `${baseUrl}${test.endpoint}`;
responseEl.textContent += `Request: ${test.method} ${url}\n`;
const response = await fetch(url, options);
let responseData;
try {
responseData = await response.json();
responseEl.textContent += `Status: ${response.status}\n`;
responseEl.textContent += `Response: ${JSON.stringify(responseData, null, 2)}\n`;
} catch (e) {
const text = await response.text();
responseEl.textContent += `Status: ${response.status}\n`;
responseEl.textContent += `Response: ${text || '(No response body)'}\n`;
}
if (response.status >= 400) {
allPassed = false;
responseEl.textContent += `⚠️ Test Failed\n`;
} else {
responseEl.textContent += `✅ Test Passed\n`;
// Store IDs for subsequent tests
if (test.method === 'POST' && responseData?.data?.id) {
const idType = test.endpoint.substring(1) + 'Id'; // /mood -> moodId
window[idType] = responseData.data.id;
responseEl.textContent += `Saved ${idType}: ${responseData.data.id}\n`;
}
}
} catch (error) {
allPassed = false;
responseEl.textContent += `Error: ${error.message}\n⚠️ Test Failed\n`;
}
}
responseEl.textContent += `\n----- Test Summary -----\n`;
responseEl.textContent += allPassed ? '✅ All tests passed!' : '⚠️ Some tests failed';
});
</script>
</body>
</html>