Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(web): extend mobile request origin unit tests

Richie McIlroy04334b4bd760
diff --git a/apps/web/__tests__/unit/mobile-request-origin.test.ts b/apps/web/__tests__/unit/mobile-request-origin.test.ts
index 282dd3dbe..a58e4b037 100644
--- a/apps/web/__tests__/unit/mobile-request-origin.test.ts
+++ b/apps/web/__tests__/unit/mobile-request-origin.test.ts
@@ -1,7 +1,10 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
-import { resolveMobileRequestOrigin } from "@/lib/mobile-request-origin";
+import {
+ resolveMobileRequestOrigin,
+ resolveMobileWebResourceUrl,
+} from "@/lib/mobile-request-origin";
describe("mobile request origin", () => {
it("uses the private request origin when local WEB_URL is loopback", () => {
@@ -13,6 +16,16 @@ describe("mobile request origin", () => {
).toBe("http://192.168.4.42:3000");
});
+ it("uses a private Host header when the dev server normalizes its URL", () => {
+ expect(
+ resolveMobileRequestOrigin(
+ "http://localhost:3000",
+ "http://localhost:3000/api/mobile/caps",
+ "192.168.4.42:3000",
+ ),
+ ).toBe("http://192.168.4.42:3000");
+ });
+
it("supports private IPv6 and local hostnames", () => {
expect(
resolveMobileRequestOrigin(
@@ -58,6 +71,35 @@ describe("mobile request origin", () => {
).toBe("http://localhost:3000");
});
+ it("makes local web-backed media URLs reachable from the phone", () => {
+ expect(
+ resolveMobileWebResourceUrl(
+ "http://localhost:3000/api/storage/object?videoId=cap_123&token=signed",
+ "http://localhost:3000",
+ "http://192.168.4.42:3000",
+ ),
+ ).toBe(
+ "http://192.168.4.42:3000/api/storage/object?videoId=cap_123&token=signed",
+ );
+ });
+
+ it("does not rewrite storage endpoints or production media origins", () => {
+ expect(
+ resolveMobileWebResourceUrl(
+ "http://localhost:9000/cap/video.mp4?signature=signed",
+ "http://localhost:3000",
+ "http://192.168.4.42:3000",
+ ),
+ ).toBe("http://localhost:9000/cap/video.mp4?signature=signed");
+ expect(
+ resolveMobileWebResourceUrl(
+ "https://cdn.example.com/video.mp4?signature=signed",
+ "https://cap.so",
+ "https://cap.so",
+ ),
+ ).toBe("https://cdn.example.com/video.mp4?signature=signed");
+ });
+
it("uses Effect's unmodified request URL for mobile response origins", () => {
const route = readFileSync(
join(process.cwd(), "app/api/mobile/[...route]/route.ts"),
@@ -65,8 +107,17 @@ describe("mobile request origin", () => {
);
expect(route).toContain(
- "getPlayback(path.id, getMobilePublicOrigin(request.originalUrl))",
+ "getPlayback(path.id, getMobilePublicOrigin(request))",
);
expect(route).not.toContain("getMobilePublicOrigin(request.url)");
+
+ const playlistRoute = readFileSync(
+ join(process.cwd(), "app/api/playlist/route.ts"),
+ "utf8",
+ );
+ expect(playlistRoute).toContain('request.headers["x-forwarded-host"]');
+ expect(playlistRoute).not.toContain(
+ "resolveMobileRequestOrigin(serverEnv().WEB_URL, request.url)",
+ );
});
});