-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
472 lines (411 loc) · 16.6 KB
/
index.html
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Tester</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"], select, textarea {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
textarea {
min-height: 100px;
font-family: monospace;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.response {
margin-top: 20px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 4px;
}
#responseHeaders, #responseBody {
white-space: pre-wrap;
font-family: monospace;
}
.form-field {
display: flex;
gap: 10px;
margin-bottom: 10px;
align-items: center;
}
.field-key, .field-value {
flex: 1;
}
.field-type {
width: 100px;
}
.field-file {
flex: 1;
}
.menu-bar {
background-color: #f0f0f0;
padding: 10px;
margin-bottom: 20px;
border-radius: 4px;
}
.saved-tests {
position: fixed;
top: 0;
right: -300px;
width: 300px;
height: 100%;
background: white;
box-shadow: -2px 0 5px rgba(0,0,0,0.1);
transition: right 0.3s;
padding: 20px;
box-sizing: border-box;
overflow-y: auto;
}
.saved-tests.open {
right: 0;
}
.test-item {
padding: 10px;
border: 1px solid #ddd;
margin-bottom: 10px;
border-radius: 4px;
cursor: pointer;
}
.test-item:hover {
background-color: #f5f5f5;
}
.test-item .delete-btn {
float: right;
color: red;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="menu-bar">
<button onclick="saveCurrentTest()">Save Test Case</button>
<button onclick="toggleSavedTests()">Show Saved Tests</button>
<button onclick="createNewTest()">New Test</button>
<button onclick="exportTests()">Export All Tests</button>
<input type="file" id="importFile" style="display: none;" onchange="importTests(event)">
<button onclick="document.getElementById('importFile').click()">Import Tests</button>
</div>
<h1>API Tester</h1>
<div class="input-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="PATCH">PATCH</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div class="input-group">
<label for="url">URL:</label>
<input type="text" id="url" placeholder="Enter API URL">
</div>
<div class="input-group">
<label for="headers">Headers (JSON format):</label>
<textarea id="headers" placeholder='{"Content-Type": "application/json"}'></textarea>
</div>
<div class="input-group">
<label>Request Type:</label>
<select id="requestType" onchange="toggleRequestInputs()">
<option value="json">JSON</option>
<option value="formData">Form Data</option>
</select>
</div>
<div id="jsonInput" class="input-group">
<label for="body">Request Body (JSON format):</label>
<textarea id="body" placeholder='{"key": "value"}'></textarea>
</div>
<div id="formDataInput" class="input-group" style="display: none;">
<label>Form Data:</label>
<div id="formFields">
<div class="form-field">
<input type="text" class="field-key" placeholder="Key">
<select class="field-type">
<option value="text">Text</option>
<option value="file">File</option>
</select>
<input type="text" class="field-value" placeholder="Value">
<input type="file" class="field-file" style="display: none;">
<button type="button" onclick="removeFormField(this)">Remove</button>
</div>
</div>
<button type="button" onclick="addFormField()">Add Field</button>
</div>
<button onclick="sendRequest()">Send Request</button>
<div class="response">
<h3>Response</h3>
<div>
<strong>Status:</strong> <span id="status"></span>
</div>
<div>
<strong>Headers:</strong>
<pre id="responseHeaders"></pre>
</div>
<div>
<strong>Body:</strong>
<pre id="responseBody"></pre>
</div>
</div>
<div class="saved-tests" id="savedTests">
<h3>Saved Test Cases</h3>
<div id="testsList"></div>
</div>
</div>
<script>
function toggleRequestInputs() {
const requestType = document.getElementById('requestType').value;
document.getElementById('jsonInput').style.display =
requestType === 'json' ? 'block' : 'none';
document.getElementById('formDataInput').style.display =
requestType === 'formData' ? 'block' : 'none';
}
function addFormField() {
const formFields = document.getElementById('formFields');
const newField = document.createElement('div');
newField.className = 'form-field';
newField.innerHTML = `
<input type="text" class="field-key" placeholder="Key">
<select class="field-type" onchange="toggleFieldInput(this)">
<option value="text">Text</option>
<option value="file">File</option>
</select>
<input type="text" class="field-value" placeholder="Value">
<input type="file" class="field-file" style="display: none;">
<button type="button" onclick="removeFormField(this)">Remove</button>
`;
formFields.appendChild(newField);
}
function removeFormField(button) {
button.parentElement.remove();
}
function toggleFieldInput(select) {
const fieldValue = select.parentElement.querySelector('.field-value');
const fieldFile = select.parentElement.querySelector('.field-file');
if (select.value === 'file') {
fieldValue.style.display = 'none';
fieldFile.style.display = 'block';
} else {
fieldValue.style.display = 'block';
fieldFile.style.display = 'none';
}
}
async function sendRequest() {
const method = document.getElementById('method').value;
const url = document.getElementById('url').value;
const requestType = document.getElementById('requestType').value;
let headers = {};
let body = null;
try {
headers = JSON.parse(document.getElementById('headers').value || '{}');
} catch (e) {
alert('Invalid headers JSON format');
return;
}
if (requestType === 'json') {
try {
const bodyText = document.getElementById('body').value;
if (bodyText) {
body = JSON.stringify(JSON.parse(bodyText));
}
} catch (e) {
alert('Invalid body JSON format');
return;
}
} else {
const formData = new FormData();
const formFields = document.querySelectorAll('.form-field');
for (const field of formFields) {
const key = field.querySelector('.field-key').value;
const type = field.querySelector('.field-type').value;
if (!key) continue;
if (type === 'file') {
const fileInput = field.querySelector('.field-file');
if (fileInput.files.length > 0) {
formData.append(key, fileInput.files[0]);
}
} else {
const value = field.querySelector('.field-value').value;
formData.append(key, value);
}
}
body = formData;
// Remove Content-Type header for FormData as browser will set it automatically
delete headers['Content-Type'];
}
try {
const response = await fetch(url, {
method: method,
headers: headers,
body: body
});
// Display status
document.getElementById('status').textContent =
`${response.status} ${response.statusText}`;
// Display headers
const responseHeaders = {};
response.headers.forEach((value, key) => {
responseHeaders[key] = value;
});
document.getElementById('responseHeaders').textContent =
JSON.stringify(responseHeaders, null, 2);
// Display body
try {
const responseBody = await response.json();
document.getElementById('responseBody').textContent =
JSON.stringify(responseBody, null, 2);
} catch (e) {
const textBody = await response.text();
document.getElementById('responseBody').textContent = textBody;
}
} catch (error) {
document.getElementById('status').textContent = 'Error';
document.getElementById('responseBody').textContent = error.message;
}
}
function createNewTest() {
document.getElementById('method').value = 'GET';
document.getElementById('url').value = '';
document.getElementById('headers').value = '';
document.getElementById('body').value = '';
document.getElementById('requestType').value = 'json';
toggleRequestInputs();
}
function saveCurrentTest() {
const testName = prompt('Enter a name for this test case:');
if (!testName) return;
const testCase = {
name: testName,
method: document.getElementById('method').value,
url: document.getElementById('url').value,
headers: document.getElementById('headers').value,
requestType: document.getElementById('requestType').value,
body: document.getElementById('body').value,
formData: Array.from(document.querySelectorAll('.form-field')).map(field => ({
key: field.querySelector('.field-key').value,
type: field.querySelector('.field-type').value,
value: field.querySelector('.field-value').value
}))
};
let savedTests = JSON.parse(localStorage.getItem('apiTests') || '[]');
savedTests.push(testCase);
localStorage.setItem('apiTests', JSON.stringify(savedTests));
updateTestsList();
}
function loadTest(test) {
document.getElementById('method').value = test.method;
document.getElementById('url').value = test.url;
document.getElementById('headers').value = test.headers;
document.getElementById('requestType').value = test.requestType;
document.getElementById('body').value = test.body;
// Clear existing form fields
const formFields = document.getElementById('formFields');
formFields.innerHTML = '';
// Recreate form fields if they exist
if (test.formData) {
test.formData.forEach(field => {
const newField = document.createElement('div');
newField.className = 'form-field';
newField.innerHTML = `
<input type="text" class="field-key" value="${field.key}" placeholder="Key">
<select class="field-type">
<option value="text" ${field.type === 'text' ? 'selected' : ''}>Text</option>
<option value="file" ${field.type === 'file' ? 'selected' : ''}>File</option>
</select>
<input type="text" class="field-value" value="${field.value}" placeholder="Value">
<input type="file" class="field-file" style="display: none;">
<button type="button" onclick="removeFormField(this)">Remove</button>
`;
formFields.appendChild(newField);
});
}
toggleRequestInputs();
}
function deleteTest(index) {
if (!confirm('Are you sure you want to delete this test case?')) return;
let savedTests = JSON.parse(localStorage.getItem('apiTests') || '[]');
savedTests.splice(index, 1);
localStorage.setItem('apiTests', JSON.stringify(savedTests));
updateTestsList();
}
function updateTestsList() {
const testsList = document.getElementById('testsList');
const savedTests = JSON.parse(localStorage.getItem('apiTests') || '[]');
testsList.innerHTML = savedTests.map((test, index) => `
<div class="test-item">
<span onclick="loadTest(${JSON.stringify(test)})">${test.name}</span>
<span class="delete-btn" onclick="event.stopPropagation(); deleteTest(${index})">×</span>
</div>
`).join('');
}
function toggleSavedTests() {
const sidebar = document.getElementById('savedTests');
sidebar.classList.toggle('open');
updateTestsList();
}
function exportTests() {
const savedTests = localStorage.getItem('apiTests') || '[]';
const blob = new Blob([savedTests], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'api-tests.json';
a.click();
URL.revokeObjectURL(url);
}
function importTests(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
try {
const tests = JSON.parse(e.target.result);
localStorage.setItem('apiTests', JSON.stringify(tests));
updateTestsList();
alert('Tests imported successfully!');
} catch (error) {
alert('Error importing tests: Invalid file format');
}
};
reader.readAsText(file);
}
updateTestsList();
</script>
</body>
</html>