Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

add tests for getCanvasRequestParams

Roman Pominovdf8bd6b7aad6
diff --git a/apps/designer/app/entry.server.tsx b/apps/designer/app/entry.server.tsx
index bb0a1594a9..207355a6c3 100644
--- a/apps/designer/app/entry.server.tsx
+++ b/apps/designer/app/entry.server.tsx
@@ -4,7 +4,7 @@ import { initSentry } from "./shared/sentry";
import { prisma } from "@webstudio-is/prisma-client";
import { handleRequest as handleRequestDesigner } from "./shared/remix";
import { handleRequest as handleRequestCanvas } from "@webstudio-is/react-sdk";
-import { getCanvasRequestParams } from "./routes/$";
+import { getCanvasRequestParams } from "./shared/router-utils";
initSentry({
integrations: [new Sentry.Integrations.Prisma({ client: prisma })],
diff --git a/apps/designer/app/routes/$.tsx b/apps/designer/app/routes/$.tsx
index c730ad30db..2741dd5b85 100644
--- a/apps/designer/app/routes/$.tsx
+++ b/apps/designer/app/routes/$.tsx
@@ -8,63 +8,15 @@ import env, { Env } from "~/env.server";
import { sentryException } from "~/shared/sentry";
import { Canvas } from "~/canvas";
import { ErrorMessage } from "~/shared/error";
-
-type Mode = "edit" | "preview" | "published";
-
-const modes = ["edit", "preview", "published"] as Mode[];
+import {
+ type CanvasRouteMode,
+ getCanvasRequestParams,
+} from "~/shared/router-utils";
type Data =
- | (CanvasData & { env: Env; mode: Mode })
+ | (CanvasData & { env: Env; mode: CanvasRouteMode })
| { errors: string; env: Env };
-type ProjectIdentifier = { type: "id" | "domain"; value: string };
-
-export const getCanvasRequestParams = (
- request: Request
-):
- | { projectIdObject: ProjectIdentifier; mode: Mode; pathname: string }
- | undefined => {
- const url = new URL(request.url);
-
- const pathname = url.pathname;
-
- if (Object.values(config).some((path) => pathname.startsWith(path))) {
- return undefined;
- }
-
- let projectIdObject: ProjectIdentifier | undefined = undefined;
-
- // @todo all this subdomain logic is very hacky
- const host =
- request.headers.get("x-forwarded-host") ||
- request.headers.get("host") ||
- "";
- const [userDomain, wstdDomain] = host.split(".");
- if (wstdDomain === "wstd" || wstdDomain?.includes("localhost")) {
- projectIdObject = { type: "domain", value: userDomain };
- }
-
- if (projectIdObject === undefined) {
- const projectIdParam = url.searchParams.get("projectId");
- if (projectIdParam !== null) {
- projectIdObject = { type: "id", value: projectIdParam };
- }
- }
-
- if (projectIdObject === undefined) {
- return undefined;
- }
-
- const modeParam = url.searchParams.get("mode");
- const mode =
- modeParam === null ? "published" : modes.find((mode) => mode === modeParam);
- if (mode === undefined) {
- throw new Error(`Invalid mode "${modeParam}"`);
- }
-
- return { projectIdObject, mode, pathname };
-};
-
export const meta: MetaFunction = ({ data }: { data: Data }) => {
if ("errors" in data) {
return { title: "Error" };
diff --git a/apps/designer/app/shared/router-utils/canvas-request-params.test.ts b/apps/designer/app/shared/router-utils/canvas-request-params.test.ts
new file mode 100644
index 0000000000..6450791f12
--- /dev/null
+++ b/apps/designer/app/shared/router-utils/canvas-request-params.test.ts
@@ -0,0 +1,91 @@
+import { getCanvasRequestParams } from "./canvas-request-params";
+
+const makeRequest = (url: string, headers: { [key: string]: string }) => ({
+ url,
+ headers: {
+ get: (name: string) => (headers[name] === undefined ? null : headers[name]),
+ },
+});
+
+describe("getCanvasRequestParams", () => {
+ test("returns undefined if the request is not an canvas request", () => {
+ expect(
+ getCanvasRequestParams(makeRequest("https://example.com", {}))
+ ).toBeUndefined();
+ });
+
+ test("detects project domain on localhost via `x-forwarded-host` header", () => {
+ expect(
+ getCanvasRequestParams(
+ makeRequest("https://foo.localhost", {
+ "x-forwarded-host": "foo.localhost",
+ })
+ )?.projectIdObject
+ ).toMatchInlineSnapshot(`
+ Object {
+ "type": "domain",
+ "value": "foo",
+ }
+ `);
+ });
+
+ test("detects project domain on localhost via `host` header", () => {
+ expect(
+ getCanvasRequestParams(
+ makeRequest("https://foo.localhost", { host: "foo.localhost" })
+ )?.projectIdObject
+ ).toMatchInlineSnapshot(`
+ Object {
+ "type": "domain",
+ "value": "foo",
+ }
+ `);
+ });
+
+ test("detects projectId parameter", () => {
+ expect(
+ getCanvasRequestParams(
+ makeRequest("https://foo.localhost?projectId=bar", {})
+ )?.projectIdObject
+ ).toMatchInlineSnapshot(`
+ Object {
+ "type": "id",
+ "value": "bar",
+ }
+ `);
+ });
+
+ test("detects path", () => {
+ expect(
+ getCanvasRequestParams(
+ makeRequest("https://foo.localhost/some/path?projectId=bar", {})
+ )?.pathname
+ ).toMatchInlineSnapshot(`"/some/path"`);
+ });
+
+ test("detects mode", () => {
+ expect(
+ getCanvasRequestParams(
+ makeRequest(
+ "https://foo.localhost/some/path?projectId=bar&mode=edit",
+ {}
+ )
+ )?.mode
+ ).toMatchInlineSnapshot(`"edit"`);
+
+ expect(
+ getCanvasRequestParams(
+ makeRequest(
+ "https://foo.localhost/some/path?projectId=bar&mode=preview",
+ {}
+ )
+ )?.mode
+ ).toMatchInlineSnapshot(`"preview"`);
+
+ expect(
+ getCanvasRequestParams(
+ makeRequest("https://foo.localhost/some/path?projectId=bar", {})
+ )?.mode
+ ).toMatchInlineSnapshot(`"published"`);
+ });
+});
diff --git a/apps/designer/app/shared/router-utils/canvas-request-params.ts b/apps/designer/app/shared/router-utils/canvas-request-params.ts
new file mode 100644
index 0000000000..b4f18eb540
--- /dev/null
+++ b/apps/designer/app/shared/router-utils/canvas-request-params.ts
@@ -0,0 +1,58 @@
+import config from "~/config";
+
+export type CanvasRouteMode = "edit" | "preview" | "published";
+
+const modes = ["edit", "preview", "published"] as CanvasRouteMode[];
+
+type ProjectIdentifier = { type: "id" | "domain"; value: string };
+
+export const getCanvasRequestParams = (request: {
+ url: string;
+ headers: { get: (name: string) => string | null };
+}):
+ | {
+ projectIdObject: ProjectIdentifier;
+ mode: CanvasRouteMode;
+ pathname: string;
+ }
+ | undefined => {
+ const url = new URL(request.url);
+
+ const pathname = url.pathname;
+
+ if (Object.values(config).some((path) => pathname.startsWith(path))) {
+ return undefined;
+ }
+
+ let projectIdObject: ProjectIdentifier | undefined = undefined;
+
+ // @todo all this subdomain logic is very hacky
+ const host =
+ request.headers.get("x-forwarded-host") ||
+ request.headers.get("host") ||
+ "";
+ const [userDomain, wstdDomain] = host.split(".");
+ if (wstdDomain === "wstd" || wstdDomain?.includes("localhost")) {
+ projectIdObject = { type: "domain", value: userDomain };
+ }
+
+ if (projectIdObject === undefined) {
+ const projectIdParam = url.searchParams.get("projectId");
+ if (projectIdParam !== null) {
+ projectIdObject = { type: "id", value: projectIdParam };
+ }
+ }
+
+ if (projectIdObject === undefined) {
+ return undefined;
+ }
+
+ const modeParam = url.searchParams.get("mode");
+ const mode =
+ modeParam === null ? "published" : modes.find((mode) => mode === modeParam);
+ if (mode === undefined) {
+ throw new Error(`Invalid mode "${modeParam}"`);
+ }
+
+ return { projectIdObject, mode, pathname };
+};
diff --git a/apps/designer/app/shared/router-utils/index.ts b/apps/designer/app/shared/router-utils/index.ts
new file mode 100644
index 0000000000..79eef16bfe
--- /dev/null
+++ b/apps/designer/app/shared/router-utils/index.ts
@@ -0,0 +1 @@
+export * from "./canvas-request-params";