The test nginx is now the production shape of DESIGN 11.1 and the browser gate's real path (#66, spec #63 decision 6): TLS-only listener with http2 on, a self-signed certificate generated per run (tools/tlsdev.mjs, added to the process's default CA list so verification stays on), the client dist at / with SPA fallback, index no-cache and a year-immutable /assets/ cache, and the tile gate's internal /_tiles/ still unreachable. Existing tile/doors/oneway/wallbehaviour harnesses point their server's public origin at the https vhost, so they prove the same listener the browser uses. tools/vhost.test.mjs is the independent node:http2 client (cert SANs, ALPN h2, cache headers, SPA fallback, direct /_tiles/ 404); e2e/h2.test.mjs drives devbox chromium via playwright-core and asserts the page reports h2 (`devbox run e2e`, npm deps outside tools/). Root devbox gains chromium + openssl. Harness builds use a worktree-local nimcache so parallel worktrees stop corrupting each other's objects. Refs #66
125 lines
5.4 KiB
JavaScript
125 lines
5.4 KiB
JavaScript
// #66 (spec #63 decision 6): the test vhost is the production shape of DESIGN 11.1 — TLS only, h2
|
|
// by ALPN, the client build at `/` (SPA fallback, index no-cache, hashed assets immutable), and the
|
|
// tile gate's `internal` location still unreachable from outside. The certificate is generated per
|
|
// run by tools/tlsdev.mjs. Here a Node HTTP/2 client (built-in `node:http2`, TESTING 10.4) is the
|
|
// independent client; the headless-browser proof lives in `e2e/` (npm dep, outside tools/).
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import http2 from 'node:http2'
|
|
import { X509Certificate } from 'node:crypto'
|
|
import { freePort, httpsBaseUrl, refuseLivePort } from './lib.mjs'
|
|
import { makeStateDir, startServer, stopServer } from './withserver.mjs'
|
|
import { startTestNginx, stopTestNginx } from './nginx.mjs'
|
|
import { writeClientDist } from './distfixture.mjs'
|
|
|
|
async function stack(fn) {
|
|
const stateDir = makeStateDir()
|
|
const dist = writeClientDist(`${stateDir}/dist`)
|
|
const nginxPort = await freePort()
|
|
refuseLivePort(nginxPort)
|
|
const server = await startServer({ stateDir, publicOrigin: httpsBaseUrl(nginxPort) })
|
|
let nginx
|
|
try {
|
|
nginx = await startTestNginx({ backendPort: server.port, stateDir, port: nginxPort, distDir: dist.dir })
|
|
return await fn({ stateDir, server, nginx, dist })
|
|
} finally {
|
|
await stopTestNginx(nginx)
|
|
await stopServer(server)
|
|
fs.rmSync(stateDir, { recursive: true, force: true })
|
|
}
|
|
}
|
|
|
|
// One HTTP/2 request; resolves with the negotiated protocol, the response and the body.
|
|
async function h2Get(origin, urlPath) {
|
|
const client = http2.connect(origin)
|
|
try {
|
|
await new Promise((resolve, reject) => {
|
|
client.once('connect', resolve)
|
|
client.once('error', reject)
|
|
})
|
|
const alpn = client.alpnProtocol
|
|
const res = await new Promise((resolve, reject) => {
|
|
const req = client.request({ ':path': urlPath, ':method': 'GET' })
|
|
let headers = null
|
|
let body = ''
|
|
req.setEncoding('utf8')
|
|
req.on('response', h => { headers = h })
|
|
req.on('data', c => { body += c })
|
|
req.on('end', () => resolve({ status: headers[':status'], headers, body }))
|
|
req.on('error', reject)
|
|
req.end()
|
|
})
|
|
return { alpn, ...res }
|
|
} finally {
|
|
client.close()
|
|
}
|
|
}
|
|
|
|
test('vhost: the generated certificate is self-signed and names loopback', async () => {
|
|
await stack(async ({ nginx }) => {
|
|
const cert = new X509Certificate(fs.readFileSync(nginx.certPath))
|
|
assert.equal(cert.subject, cert.issuer, 'the certificate is self-signed')
|
|
assert.equal(cert.verify(cert.publicKey), true, 'its own signature verifies')
|
|
assert.equal(cert.checkIP('127.0.0.1'), '127.0.0.1', 'the SAN covers the loopback address')
|
|
assert.equal(cert.checkHost('localhost'), 'localhost', 'the SAN covers localhost')
|
|
assert.ok(new Date(cert.validTo) > new Date(), 'the certificate is still valid')
|
|
})
|
|
})
|
|
|
|
test('vhost: an HTTP/2 client negotiates h2 and serves the client index', async () => {
|
|
await stack(async ({ nginx, dist }) => {
|
|
const r = await h2Get(nginx.origin, '/')
|
|
assert.equal(r.alpn, 'h2', 'TLS ALPN must select h2, not http/1.1')
|
|
assert.equal(r.status, 200)
|
|
assert.match(r.headers['content-type'], /^text\/html/)
|
|
assert.equal(r.body, dist.indexHtml, 'index.html is served at /')
|
|
assert.equal(r.headers['cache-control'], 'no-cache', 'index.html is revalidated, never cached hard')
|
|
})
|
|
})
|
|
|
|
test('vhost: hashed assets are immutable, the SPA falls back, a missing asset is 404', async () => {
|
|
await stack(async ({ nginx, dist }) => {
|
|
const asset = await h2Get(nginx.origin, dist.assetPath)
|
|
assert.equal(asset.status, 200)
|
|
assert.equal(asset.alpn, 'h2')
|
|
assert.match(asset.headers['content-type'], /^application\/javascript/)
|
|
assert.equal(asset.headers['cache-control'], 'public, max-age=31536000, immutable')
|
|
assert.match(asset.body, /content-hashed fixture asset/)
|
|
|
|
// A client-side route with no file behind it is index.html (SPA fallback), revalidated.
|
|
const route = await h2Get(nginx.origin, '/scene/abc/tokens')
|
|
assert.equal(route.status, 200)
|
|
assert.equal(route.body, dist.indexHtml)
|
|
assert.equal(route.headers['cache-control'], 'no-cache')
|
|
|
|
// A stale index naming a deleted hash must fail, not boot the app.
|
|
const missing = await h2Get(nginx.origin, '/assets/app-000000000000.js')
|
|
assert.equal(missing.status, 404)
|
|
})
|
|
})
|
|
|
|
test('vhost: direct /_tiles/ is 404 over TLS', async () => {
|
|
await stack(async ({ nginx }) => {
|
|
for (const p of ['/_tiles/', '/_tiles/scene1/v1/0_0.png']) {
|
|
const r = await h2Get(nginx.origin, p)
|
|
assert.equal(r.status, 404, `${p} is internal and must not be reachable from outside`)
|
|
}
|
|
// The backend still answers through the proxy, so the 404 is the location's, not a dead vhost.
|
|
const health = await h2Get(nginx.origin, '/healthz')
|
|
assert.equal(health.status, 200)
|
|
assert.equal(health.body, '{"status":"ok"}')
|
|
})
|
|
})
|
|
|
|
test('vhost: an HTTP/1.1 client without ALPN is still served over TLS', async () => {
|
|
// Global fetch is Node's HTTP/1.1 client: it proves the TLS listener is not h2-only, so the
|
|
// harnesses (fetch + WebSocket) keep working against the same vhost the browser uses.
|
|
await stack(async ({ nginx, dist }) => {
|
|
const r = await fetch(`${nginx.origin}/`)
|
|
assert.equal(r.status, 200)
|
|
assert.equal(await r.text(), dist.indexHtml)
|
|
assert.equal(r.headers.get('cache-control'), 'no-cache')
|
|
})
|
|
})
|