cors-demo/public/_selftest.html

34 lines
1.6 KiB
HTML
Raw Normal View History

2026-10-01 13:02:44 -03:00
<!doctype html><meta charset="utf-8"><title>selftest</title><body><pre id="out">running</pre>
<script src="scenarios.js"></script>
<script>
const PAGE = location.origin, HOST = location.hostname;
const OTHER = HOST === 'localhost' ? '127.0.0.1' : 'localhost';
const T = { api: PAGE.replace(':3000', ':3001') + '/api/data',
same: PAGE + '/same-origin/api',
'cross-site': `http://${OTHER}:3001/api/data` };
(async () => {
const lines = [];
let bad = 0;
for (const s of window.SCENARIOS) {
if (!s.client) continue;
const p = new URLSearchParams(s.server || {});
if (!s.stableUrl) p.set('_', Math.random().toString(36).slice(2));
const init = {};
for (const k of ['method', 'mode', 'credentials', 'headers', 'body']) if (s.client[k]) init[k] = s.client[k];
let got, detail = '';
try {
const r = await fetch(T[s.target || 'api'] + '?' + p, init);
got = 'ok'; detail = `status=${r.status} type=${r.type}`;
if (s.client.readHeaders) detail += ' read=' + s.client.readHeaders.map(h => h + ':' + r.headers.get(h)).join('|');
} catch (e) { got = 'blocked'; detail = e.name; }
const want = s.expect === 'fail' ? 'blocked' : 'ok';
const okmark = got === want ? 'PASS' : 'MISMATCH';
if (got !== want) bad++;
lines.push(`${okmark.padEnd(9)} ${s.id.padEnd(34)} want=${want.padEnd(8)} got=${got.padEnd(8)} ${detail}`);
}
lines.push('', `TOTAL ${lines.length} scenarios, ${bad} mismatches`);
document.getElementById('out').textContent = lines.join('\n');
document.title = bad ? 'MISMATCHES:' + bad : 'ALL PASS';
})();
</script>