tests/sims/httpmsg.test.ts
The automated tests for the /simulators/http-flow simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- Freshness lifetime follows RFC 9111 §4.2.1 in order: s-maxage (shared), max-age, Expires − Date, heuristic.
- No-store, private, no-cache and Authorization limit storage as RFC 9111 §3, §3.5 and §5.2.2 say.
- Set-Cookie attributes are parsed per RFC 6265.
- Every example on the page parses into a request and a response with no unparsed lines.
// /simulators/http-flow: HTTP message parsing and cache freshness (RFC 9110, 9111, 9112).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parse, freshness, cookies } from '../../src/lib/sims/httpmsg.ts';
import { HTTP_PRESETS } from '../../src/lib/sims/httpPresets.ts';
const res = (headers: string, status = '200 OK') => parse(`HTTP/1.1 ${status}\n${headers}`).msgs[0];
const req = (headers: string) => parse(`GET / HTTP/1.1\nHost: example.com\n${headers}`).msgs[0];
const DATE = 'Date: Tue, 06 Oct 2026 10:00:00 GMT';
test('freshness lifetime follows RFC 9111 §4.2.1 in order: s-maxage (shared), max-age, Expires − Date, heuristic', () => {
assert.equal(freshness(res(`${DATE}\nCache-Control: max-age=600\nAge: 100`), false, null).remaining, 500);
assert.equal(freshness(res(`${DATE}\nCache-Control: max-age=60, s-maxage=600`), true, null).lifetime, 600);
assert.equal(freshness(res(`${DATE}\nCache-Control: max-age=60, s-maxage=600`), false, null).lifetime, 60);
assert.equal(freshness(res(`${DATE}\nExpires: Tue, 06 Oct 2026 11:00:00 GMT`), false, null).lifetime, 3600);
assert.equal(freshness(res(`${DATE}\nExpires: 0`), false, null).lifetime, 0); // an invalid Expires means already expired (§5.3)
// §4.2.2: a typical heuristic is 10% of the time since Last-Modified
assert.equal(freshness(res(`${DATE}\nLast-Modified: Fri, 26 Sep 2026 10:00:00 GMT`), false, null).lifetime, 86400);
assert.equal(freshness(res(`${DATE}\nLast-Modified: Fri, 26 Sep 2026 10:00:00 GMT`, '302 Found'), false, null).lifetime, null); // 302 is not heuristically cacheable
});
test('no-store, private, no-cache and Authorization limit storage as RFC 9111 §3, §3.5 and §5.2.2 say', () => {
assert.equal(freshness(res(`${DATE}\nCache-Control: no-store, max-age=600`), false, null).storable, false);
assert.equal(freshness(res(`${DATE}\nCache-Control: private, max-age=600`), true, null).storable, false);
assert.equal(freshness(res(`${DATE}\nCache-Control: private, max-age=600`), false, null).lifetime, 600);
assert.equal(freshness(res(`${DATE}\nCache-Control: no-cache, max-age=600`), false, null).remaining, 0);
const auth = req('Authorization: Bearer x');
assert.equal(freshness(res(`${DATE}\nCache-Control: max-age=600`), true, auth).storable, false);
assert.equal(freshness(res(`${DATE}\nCache-Control: public, max-age=600`), true, auth).storable, true);
assert.equal(freshness(res(`${DATE}\nCache-Control: s-maxage=600`), true, auth).storable, true);
});
test('Set-Cookie attributes are parsed per RFC 6265', () => {
const c = cookies(res('Set-Cookie: sid=abc123; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=3600'));
assert.equal(c.length, 1);
assert.equal(c[0].name, 'sid');
assert.equal(c[0].value, 'abc123');
const keys = Object.keys(c[0].attrs).map((k) => k.toLowerCase());
for (const k of ['path', 'httponly', 'secure', 'samesite', 'max-age']) assert.ok(keys.includes(k), `missing ${k}`);
});
test('every example on the page parses into a request and a response with no unparsed lines', () => {
for (const p of HTTP_PRESETS) {
const { msgs, bad } = parse(p.text);
assert.deepEqual(bad, [], p.id);
assert.ok(msgs.some((m) => m.kind === 'response'), `${p.id} has no response`);
}
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.