Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import { describe } from "vite-plus/test";
3
4import { assetResponseHeaders, isLoopbackHostname, resolveDevRedirectUrl } from "./http.ts";
5
6describe("http dev routing", () => {
7 it("treats localhost and loopback addresses as local", () => {
8 expect(isLoopbackHostname("127.0.0.1")).toBe(true);
9 expect(isLoopbackHostname("localhost")).toBe(true);
10 expect(isLoopbackHostname("::1")).toBe(true);
11 expect(isLoopbackHostname("[::1]")).toBe(true);
12 });
13
14 it("does not treat LAN addresses as local", () => {
15 expect(isLoopbackHostname("192.168.86.35")).toBe(false);
16 expect(isLoopbackHostname("10.0.0.24")).toBe(false);
17 expect(isLoopbackHostname("example.local")).toBe(false);
18 });
19
20 it("preserves path and query when redirecting to the dev server", () => {
21 const devUrl = new URL("http://127.0.0.1:5173/");
22 const requestUrl = new URL("http://127.0.0.1:3774/pair?token=test-token");
23
24 expect(resolveDevRedirectUrl(devUrl, requestUrl)).toBe(
25 "http://127.0.0.1:5173/pair?token=test-token",
26 );
27 });
28});
29
30describe("assetResponseHeaders", () => {
31 it("sandboxes SVG assets", () => {
32 expect(assetResponseHeaders("/attachments/user-image.svg")).toMatchObject({
33 "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox",
34 "X-Content-Type-Options": "nosniff",
35 });
36 expect(assetResponseHeaders("/attachments/user-image.SVG")).toHaveProperty(
37 "Content-Security-Policy",
38 );
39 });
40
41 it("does not apply document policy to raster images", () => {
42 expect(assetResponseHeaders("/attachments/user-image.png")).toEqual({
43 "Cache-Control": "private, max-age=3600",
44 "X-Content-Type-Options": "nosniff",
45 });
46 });
47});