openvibers/openvibe.media Commits
Not found: an unknown /api path answers JSON ({ error: 'Not found' }), as Media's API routes do, not the HTML page (#140)
Changes
@@@ -18,8 +18,10 @@ const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);function notFound(req, res) {res.status(404);-- res.set('Content-Security-Policy', CSP);res.set('X-Content-Type-Options', 'nosniff');++ // An API caller gets JSON, as from Media's own API routes (it got this HTML page).++ if (req.path === '/api' || req.path.startsWith('/api/')) return res.json({ error: 'Not found' });++ res.set('Content-Security-Policy', CSP);res.type('html').send(`<!DOCTYPE html><html lang="en"><head>@@@ -160,6 +160,10 @@ const { spawn } = require('child_process');}).on('error', reject));assert.deepStrictEqual([nf.status, nf.csp], [404, require('../server/not-found').CSP]);assert.ok(nf.body.includes('<pre>Cannot GET /__ovcheck-404</pre>'));++ // ...and an unknown /api path is JSON, as from Media's API routes.++ const api404 = await request(b2, '/api/nope');++ assert.strictEqual(api404.status, 404);++ assert.deepStrictEqual(typeof api404.body === 'string' ? JSON.parse(api404.body) : api404.body, { error: 'Not found' });// The release manifest (registry.release-manifest@1) names where open tabs report updates,// and those reports land in /metrics.const rel = await request(b2, '/release.json');
@@ -18,8 +18,10 @@ const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
@@ -18,8 +18,10 @@ const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
function notFound(req, res) {
function notFound(req, res) {
res.status(404);
res.status(404);
- res.set('Content-Security-Policy', CSP);
res.set('X-Content-Type-Options', 'nosniff');
res.set('X-Content-Type-Options', 'nosniff');
+ // An API caller gets JSON, as from Media's own API routes (it got this HTML page).
+ if (req.path === '/api' || req.path.startsWith('/api/')) return res.json({ error: 'Not found' });
+ res.set('Content-Security-Policy', CSP);
res.type('html').send(`<!DOCTYPE html>
res.type('html').send(`<!DOCTYPE html>
<html lang="en">
<html lang="en">
<head>
<head>
@@ -160,6 +160,10 @@ const { spawn } = require('child_process');
@@ -160,6 +160,10 @@ const { spawn } = require('child_process');
}).on('error', reject));
}).on('error', reject));
assert.deepStrictEqual([nf.status, nf.csp], [404, require('../server/not-found').CSP]);
assert.deepStrictEqual([nf.status, nf.csp], [404, require('../server/not-found').CSP]);
assert.ok(nf.body.includes('<pre>Cannot GET /__ovcheck-404</pre>'));
assert.ok(nf.body.includes('<pre>Cannot GET /__ovcheck-404</pre>'));
+ // ...and an unknown /api path is JSON, as from Media's API routes.
+ const api404 = await request(b2, '/api/nope');
+ assert.strictEqual(api404.status, 404);
+ assert.deepStrictEqual(typeof api404.body === 'string' ? JSON.parse(api404.body) : api404.body, { error: 'Not found' });
// The release manifest (registry.release-manifest@1) names where open tabs report updates,
// The release manifest (registry.release-manifest@1) names where open tabs report updates,
// and those reports land in /metrics.
// and those reports land in /metrics.
const rel = await request(b2, '/release.json');
const rel = await request(b2, '/release.json');