This repository has been archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapitest.html
355 lines (316 loc) · 13 KB
/
apitest.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
<!DOCTYPE html>
<html lang="nl" data-ng-app="ga">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Groepsadministratie Scouts en Gidsen Vlaanderen</title>
<link href="css/bootstrap.css" rel="stylesheet">
<style>
body {
padding-top: 0;
}
.long {
font-size: 0.8rem;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
#api {
float: right;
}
.form-control,
.bottom-space {
margin-bottom: 10px;
}
.column {
display: inline-block;
width: 19.5%;
padding-right: 20px;
}
span.col {
display: inline-block;
width: 15%;
font-size: 1rem;
}
span.value {
width: 70%;
word-break: break-all;
}
#result_size {
height: 3000px;
resize: vertical;
overflow: hidden;
}
#result {
width: 100%;
height: 100%;
border: 0;
}
</style>
</head>
<body class="container-fluid">
<h1>API Tester</h1>
<div id='api'>
<a href='https://groepsadmin-develop.scoutsengidsenvlaanderen.net/groepsadmin/client/docs/api.html' target='_blank'>Documentatie API</a>
</div>
<h2>Request</h2>
<label>Access token (keycloak token)</label>
<div id="access_token" class="long">Niet beschikbaar</div>
<div id="access_token_details" class="bottom-space"></div>
<label>Refresh token</label>
<div id="refresh_token" class="long">Niet beschikbaar</div>
<div id="refresh_token_details" class="bottom-space"></div>
<label>ID token</label>
<div id="id_token" class="long">Niet beschikbaar</div>
<div id="id_token_details" class="bottom-space"></div>
<form id='form'>
<div style="float: right; font-weight: bold;">
<a href="javascript:history.go(0)">↻</a>
</div>
<label>Url</label>
<input type="text" id="urlToTest" class="form-control">
<div>
<div class="column">
<label>Client id</label>
<input type="text" id="clientId" class="form-control">
</div>
<div class="column">
<label>Methode</label>
<select id="method" class="form-control">
<option value="GET" selected>GET</option>
<option value="POST">POST</option>
<option value="PATCH">PATCH</option>
<option value="DELETE">DELETE</option>
<option disabled>---</option>
<option value="HEAD">HEAD</option>
<option value="OPTIONS">OPTIONS</option>
<option value="TRACE">TRACE</option>
</select>
</div>
<div class="column">
<label>Accept</label>
<select id="acceptHeader" class="form-control">
<option value="*/*" selected>*/*</option>
<option value="application/json">application/json</option>
<option value="application/pdf">application/pdf</option>
<option value="text/csv">text/csv</option>
<option disabled>---</option>
<option value="multipart/form-data; boundary=AaB03x">multipart/form-data; boundary=AaB03x (ongebruikt)</option>
<option value="text/plain">text/plain (ongebruikt)</option>
<option value="abc">abc (ongeldig)</option>
</select>
</div>
<div class="column">
<label>If-None-Match</label>
<input type="text" id="ifnonematch" class="form-control">
</div>
<div class="column contentType">
<label>Content-Type</label>
<select id="contentType" class="form-control">
<option value="application/json" selected>application/json</option>
<option value="multipart/form-data; boundary=AaB03x">multipart/form-data; boundary=AaB03x</option>
<option disabled>---</option>
<option value="application/pdf">application/pdf (ongebruikt)</option>
<option value="text/csv">text/csv (ongebruikt)</option>
<option value="text/plain">text/plain (ongebruikt)</option>
<option value="abc">abc (ongeldig)</option>
</select>
</div>
</div>
<label class='requestBody'>Body</label>
<textarea id="requestBody" class="form-control requestBody"></textarea>
<input type="submit" value="Verstuur">
</form>
<h2>Response</h2>
<div>
<div class="column">
<label>Status</label>
<input id="status" class="form-control" disabled="disabled" value="Nog geen request verstuurd">
</div>
<div class="column">
<label>Content type</label>
<input id="type" class="form-control" disabled="disabled" value="Nog geen request verstuurd">
</div>
<div class="column">
<label>Etag</label>
<input id="etag" class="form-control" disabled="disabled" value="Nog geen request verstuurd">
</div>
</div>
<label>Content</label>
<div id="result_size">
<iframe id="result" src='data:text/plain,Nog%20geen%20request%20verstuurd'></iframe>
</div>
<script src="bower_components/jquery/jquery.js"></script>
<script src="node_modules/keycloak-js/dist/keycloak.js"></script>
<script src="js/keycloak-config.js"></script>
<script>
// Gekopieerd van http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/
// use this transport for "binary" data type
$.ajaxTransport("+binary", function(options, originalOptions, jqXHR){
// check for conditions and support for blob / arraybuffer response type
if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob))))) {
return {
send: function(headers, callback){
var xhr = new XMLHttpRequest(),
url = options.url,
type = options.type,
dataType = options.responseType || "blob",
data = options.data || null;
xhr.addEventListener('load', function(){
var data = {};
data[options.dataType] = xhr.response;
// make callback and send data
callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
});
xhr.open(type, url, true);
for (var i in headers) {
xhr.setRequestHeader(i, headers[i]);
}
xhr.responseType = dataType;
xhr.send(data);
},
abort: function(){
jqXHR.abort();
}
};
}
});
$('#clientId').val(localStorage.getItem("client"));
$('#urlToTest').val(localStorage.getItem("url"));
$('#method').val(localStorage.getItem("method"));
$('#acceptHeader').val(localStorage.getItem("acceptHeader"));
$('#ifnonematch').val(localStorage.getItem("ifnonematch"));
$('#contentType').val(localStorage.getItem("contentType"));
$('#requestBody').val(localStorage.getItem("body"));
if (!$('#clientId').val()) {
if (getClient().clientId) {
localStorage.setItem("client", getClient().clientId);
$('#clientId').val(getClient().clientId);
}
}
function processResult(jqXHR, statusText, data) {
var contentType = jqXHR.getResponseHeader('Content-Type')
|| (jqXHR.status == 0 && "(Niet opvraagbaar door CORS)")
|| (jqXHR.status == 204 && "(Niet van toepassing)")
|| "(Geen response content?)";
$('#status').val(jqXHR.status + ' - ' + statusText);
$('#type').val(contentType);
$('#etag').val(jqXHR.getResponseHeader('Etag')
|| (jqXHR.status == 0 && "(Niet opvraagbaar door CORS)")
|| (jqXHR.status == 204 && "(Niet van toepassing)")
|| '(Geen response content?)');
if (contentType === 'application/pdf') {
document.getElementById('result').src = URL.createObjectURL(new Blob([data], {type : contentType}));
document.getElementById('result').style.height = '1000px';
return;
}
var content = jqXHR.responseText
|| (jqXHR.status == 0 && "(Niet opvraagbaar door CORS)")
|| (jqXHR.status == 204 && "(Niet van toepassing)")
|| '"(Geen response content?)"';
if (contentType === 'application/json') {
try {
content = JSON.stringify(JSON.parse(content), undefined, 2);
} catch (e) {
// invalid syntax? Display as-is
}
}
document.getElementById('result').src = 'data:' + contentType + ',' + encodeURIComponent(content);
}
var keycloak;
setInterval(function(){
if (document.getElementById('result').contentDocument) {
var resultBody = document.getElementById('result').contentDocument.body;
if (resultBody) {
document.getElementById('result_size').style.height = parseFloat(resultBody.scrollHeight) + 'px';
}
}
localStorage.setItem("client", $('#clientId').val());
localStorage.setItem("url", $('#urlToTest').val());
localStorage.setItem("method", $('#method').val());
localStorage.setItem("acceptHeader", $('#acceptHeader').val());
localStorage.setItem("ifnonematch", $('#ifnonematch').val());
localStorage.setItem("contentType", $('#contentType').val());
localStorage.setItem("body", $('#requestBody').val());
if (keycloak) {
$('#access_token').text(keycloak.token);
$('#access_token_details').html(getDetails(keycloak.tokenParsed));
$('#refresh_token').text(keycloak.refreshToken);
$('#refresh_token_details').html(getDetails(keycloak.refreshTokenParsed));
$('#id_token').text(keycloak.idToken);
$('#id_token_details').html(getDetails(keycloak.idTokenParsed));
if (keycloak.refreshTokenParsed) {
$('#left').html(parseInt(keycloak.refreshTokenParsed['exp']) - Math.round(new Date().getTime() / 1000) - keycloak.timeSkew);
}
keycloak.updateToken();
}
}, 100);
function getDetails(token) {
if (!token) {
return 'Niet beschikbaar';
}
function row(name, key, extra) {
return '<span class="col">' + name + '</span><span class="col">' + key + '</span><span class="col value">' + token[key] + (extra ? ' - ' + extra : '') + '</span><br/>'
}
var result = row('Type', 'typ');
if (token['typ'] == 'Bearer') {
result += row('Client', 'aud');
result += row('Expires', 'exp', new Date(token['exp'] * 1000) + ' - Left: <span id="left"></span> s');
result += row('Gebruikersnaam', 'preferred_username');
result += row('Keycloak id', 'sub');
result += row('Session state', 'session_state');
result += row('Allowed origin', 'allowed-origins');
}
return result;
}
$(document).ready(function () {
var clientConfig = getClient();
clientConfig.clientId = localStorage.getItem("client");
if (!clientConfig.clientId) {
console.log("Client id empty - Keyclaok not initialized");
return;
}
console.log(clientConfig);
keycloak = Keycloak(clientConfig);
keycloak.init({
onLoad: 'login-required',
checkLoginIframe: true
});
function onMethodChange(e) {
var body = 0 <= ['POST', 'PATCH', 'OPTIONS'].indexOf($('#method').val())
$('.requestBody').toggle(body);
$('.contentType').toggle(body);
}
$('#method').on('change', onMethodChange);
onMethodChange();
$('#form').on('submit', function (e) {
e.preventDefault(); // Pagina niet refreshen
var binary = ($('#acceptHeader').val() === 'application/pdf');
$.ajax(
$('#urlToTest').val(),
{
method: $('#method').val(),
contentType: $('#contentType').val(),
data: $('#requestBody:visible').val() || '',
dataType: binary ? 'binary' : null,
processData: !binary,
beforeSend: function (xhr) {
$('#status').html('Nog geen resultaat gekregen...');
$('#result').get()[0].src = 'data:text/plain,Nog geen resultaat gekregen...';
xhr.setRequestHeader("Authorization", "Bearer " + keycloak.token);
xhr.setRequestHeader('Accept', $('#acceptHeader').val());
xhr.setRequestHeader('If-None-Match', $('#ifnonematch').val());
},
error: function (jqXHR, textStatus, errorThrown) {
processResult(jqXHR, errorThrown || textStatus, null);
},
success: function (data, textStatus, jqXHR) {
processResult(jqXHR, textStatus, data);
}
}
);
});
});
</script>
</body>
</html>