The gateway answered every non-page request without a session with a login redirect, which fetch and EventSource cannot follow across origins, so polls, forms and streams failed with errors that said nothing about the session. It now answers anything that does not ask for text/html with a 401 and a WWW-Authenticate header, and the swarm UI reloads on that 401, from a wrapped fetch (one place for every call) and from a probe after a stream dies. A second reload within ten seconds is skipped so a page that keeps answering 401 cannot loop. The proxy test and the docs follow.
38 lines
1.4 KiB
JavaScript
38 lines
1.4 KiB
JavaScript
// When the swarm UI reads a response as "the login ended". Run by `npm test`.
|
|
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import { isSessionExpired, mayReload } from "../src/ui/session/session.ts";
|
|
|
|
const ORIGIN = "https://swarm.example";
|
|
|
|
test("a gateway 401 on an api address is an ended session", () => {
|
|
assert.equal(isSessionExpired("/api/agents/status", 401, true, ORIGIN), true);
|
|
assert.equal(
|
|
isSessionExpired(`${ORIGIN}/api/jobq/graph`, 401, true, ORIGIN),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("a 401 without the gateway's challenge is the controller's own", () => {
|
|
assert.equal(isSessionExpired("/api/agents", 401, false, ORIGIN), false);
|
|
});
|
|
|
|
test("other statuses and other addresses are not an ended session", () => {
|
|
assert.equal(isSessionExpired("/api/agents", 403, true, ORIGIN), false);
|
|
assert.equal(isSessionExpired("/api/agents", 500, true, ORIGIN), false);
|
|
assert.equal(isSessionExpired("/static/main.js", 401, true, ORIGIN), false);
|
|
assert.equal(
|
|
isSessionExpired("https://other.example/api/x", 401, true, ORIGIN),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("a reload is skipped when one just happened", () => {
|
|
assert.equal(mayReload(null, 1_000_000), true);
|
|
assert.equal(mayReload(1_000_000, 1_005_000), false);
|
|
assert.equal(mayReload(1_000_000, 1_010_000), true);
|
|
});
|
|
|
|
test("a clock that went backwards does not block the reload", () => {
|
|
assert.equal(mayReload(2_000_000, 1_000_000), true);
|
|
});
|