Relative URLs and trailing slashes — reproducible teaching lab
Executed: 2026-11-03
Environment: macOS 27.0.1, arm64, Node.js v22.23.1.
Requirements: Node.js 22+; built-in modules only; permission to listen on loopback.

Files in the ZIP:
  relative-url-base-lab.mjs        Complete executable lab.
  relative-url-base-results.json  Recorded output from the environment above.
  relative-url-base-README.txt    This explanation.

Run from the extracted directory:
  node relative-url-base-lab.mjs > rerun.json

Any failed assertion makes the process exit unsuccessfully. On the recorded
runtime, the output matches the supplied JSON byte for byte. With another Node
version or OS, environment fields may differ; the assertions still check all
listed expectations. No packages need installing and no public server is used.

What runs:
1. 22 WHATWG URL constructor cases, each compared to a literal expected URL.
   They cover trailing slash, dot/parent references, root paths, deployment
   prefix, query, fragment, empty input, external origins, encoded dots/slash,
   explicit alternative bases, and a filename with an .html extension.
2. An HTTP server listens on 127.0.0.1 with an OS-assigned port. 13 fetch checks
   produce 14 actual requests because one 302 response is followed. Each check
   compares HTTP status, Content-Type, and the entire response body. Request
   targets are recorded in sequence and included in the JSON.
3. The no-slash and slash page URLs return identical HTML. The wrong image
   route deliberately returns 200 text/html, modelling a broad HTML fallback.
   The right image route returns the expected SVG. This is a fixture, not a
   statement about any existing production site's configuration.
4. /go redirects to /garden/posts/note/. The final response URL resolves the
   image correctly, while using the original /go URL points to /cover.svg.
5. A real query-and-fragment request checks that the query reaches the server
   but the fragment does not. Another checks that %2F remains encoded in the
   request target. This server does not additionally percent-decode its paths.

Safety and cleanup:
The script accepts no input and performs no filesystem writes. Only requests whose
origin equals its temporary loopback origin are allowed by the fetch helper.
Example external hosts are used exclusively for URL calculations, never DNS or
HTTP. The server is closed in a finally block. <ORIGIN> replaces its temporary
address in the report, so port allocation does not make the output variable.

Scope:
This is not a browser test. The script does not parse HTML or create a DOM; the
image reference in its known fixture is passed explicitly to the URL parser.
Alternative base URL cases likewise pass the base explicitly. Multiple <base>
selection, browser rendering, image decoding, CSP, CORS, cache, service workers,
TLS, DNS, frameworks, proxy rewrites and production configurations are untested.
Fetching SVG bytes does not prove the image rendered in a browser. A framework
may decode an encoded slash later; this server's raw-target result is not a
guarantee about other servers. Do not use this as a general URL security filter.

Primary references (consulted 2026-11-03):
https://url.spec.whatwg.org/#url-parsing
https://nodejs.org/api/url.html#new-urlinput-base
https://fetch.spec.whatwg.org/#dom-response-url
https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-urls
https://html.spec.whatwg.org/multipage/semantics.html#the-base-element

The article's recommendations about diagnosis and deployment are analysis based
on these defined cases, not reports of modifying a real site's routing.
