|
| 1 | +/* |
| 2 | + * Self-contained test for the Web Bot Auth key directory endpoint: |
| 3 | + * GET /.well-known/http-message-signatures-directory |
| 4 | + * |
| 5 | + * There is no server-side test runner in this repo (`yarn test` is react-scripts |
| 6 | + * for the UI), so this file is a plain Node script: boot the Express app on an |
| 7 | + * ephemeral port, make real requests, assert, exit non-zero on failure. |
| 8 | + * |
| 9 | + * Run with: node server/test/well-known-signing-directory.test.js |
| 10 | + */ |
| 11 | +const assert = require('assert') |
| 12 | +const fetch = require('node-fetch') |
| 13 | +const app = require('../index') |
| 14 | + |
| 15 | +const PATH = '/.well-known/http-message-signatures-directory' |
| 16 | +const EXPECTED_CONTENT_TYPE = |
| 17 | + 'application/http-message-signatures-directory+json' |
| 18 | + |
| 19 | +async function run() { |
| 20 | + const server = app.listen(0) |
| 21 | + await new Promise((resolve) => server.once('listening', resolve)) |
| 22 | + const { port } = server.address() |
| 23 | + const url = `http://127.0.0.1:${port}${PATH}` |
| 24 | + |
| 25 | + let failures = 0 |
| 26 | + const check = (name, fn) => { |
| 27 | + try { |
| 28 | + fn() |
| 29 | + console.log(` ok - ${name}`) |
| 30 | + } catch (err) { |
| 31 | + failures++ |
| 32 | + console.error(` FAIL - ${name}\n ${err.message}`) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + // --- GET --- |
| 38 | + const getRes = await fetch(url) |
| 39 | + const body = await getRes.text() |
| 40 | + |
| 41 | + check('GET returns 200', () => assert.strictEqual(getRes.status, 200)) |
| 42 | + check('GET sets exact Content-Type', () => |
| 43 | + assert.strictEqual( |
| 44 | + getRes.headers.get('content-type'), |
| 45 | + EXPECTED_CONTENT_TYPE |
| 46 | + ) |
| 47 | + ) |
| 48 | + check('GET sets a public Cache-Control', () => { |
| 49 | + const cc = getRes.headers.get('cache-control') || '' |
| 50 | + assert.ok(/public/.test(cc) && /max-age=\d+/.test(cc), `got "${cc}"`) |
| 51 | + }) |
| 52 | + check('GET body is valid JSON with a keys array', () => { |
| 53 | + const json = JSON.parse(body) |
| 54 | + assert.ok(Array.isArray(json.keys), 'keys is not an array') |
| 55 | + assert.ok(json.keys.length >= 1, 'keys is empty') |
| 56 | + const k = json.keys[0] |
| 57 | + assert.strictEqual(k.kty, 'OKP') |
| 58 | + assert.strictEqual(k.crv, 'Ed25519') |
| 59 | + assert.ok('x' in k, 'missing x') |
| 60 | + assert.ok('kid' in k, 'missing kid') |
| 61 | + }) |
| 62 | + check('GET body contains no private key material', () => { |
| 63 | + assert.ok(!/"d"\s*:/.test(body), 'response contains a private key "d"') |
| 64 | + }) |
| 65 | + |
| 66 | + // --- HEAD --- |
| 67 | + const headRes = await fetch(url, { method: 'HEAD' }) |
| 68 | + const headBody = await headRes.text() |
| 69 | + check('HEAD returns 200', () => assert.strictEqual(headRes.status, 200)) |
| 70 | + check('HEAD sets exact Content-Type', () => |
| 71 | + assert.strictEqual( |
| 72 | + headRes.headers.get('content-type'), |
| 73 | + EXPECTED_CONTENT_TYPE |
| 74 | + ) |
| 75 | + ) |
| 76 | + check('HEAD has an empty body', () => |
| 77 | + assert.strictEqual(headBody, '') |
| 78 | + ) |
| 79 | + } finally { |
| 80 | + server.close() |
| 81 | + } |
| 82 | + |
| 83 | + if (failures > 0) { |
| 84 | + console.error(`\n${failures} check(s) failed`) |
| 85 | + process.exit(1) |
| 86 | + } |
| 87 | + console.log('\nAll checks passed') |
| 88 | + process.exit(0) |
| 89 | +} |
| 90 | + |
| 91 | +run().catch((err) => { |
| 92 | + console.error(err) |
| 93 | + process.exit(1) |
| 94 | +}) |
0 commit comments