diff --git a/test/e2e/routes.test.ts b/test/e2e/routes.test.ts index 641270494..b79f5ba16 100644 --- a/test/e2e/routes.test.ts +++ b/test/e2e/routes.test.ts @@ -1,5 +1,5 @@ import { describe, test, expect } from "./baseFixture" -import { clean } from "../utils/helpers" +import { clean, getMaybeProxiedPathname } from "../utils/helpers" import { REVERSE_PROXY_BASE_PATH } from "../utils/constants" const routes = ["/", "/vscode", "/vscode/"] @@ -16,13 +16,8 @@ describe("VS Code Routes", ["--disable-workspace-trust"], {}, async () => { // Check there were no redirections const url = new URL(codeServerPage.page.url()) - if (process.env.USE_PROXY === "1") { - // Behind proxy, path will be / { test("should redirect to the passed in workspace using human-readable query", async ({ codeServerPage }) => { const url = new URL(codeServerPage.page.url()) - expect(url.pathname).toBe("/") + const pathname = getMaybeProxiedPathname(url) + expect(pathname).toBe("/") expect(url.search).toBe(`?workspace=${CODE_WORKSPACE_DIR}`) }) }) @@ -57,7 +53,8 @@ const CODE_FOLDER_DIR = process.env.CODE_FOLDER_DIR || "" describe("VS Code Routes with code-workspace", ["--disable-workspace-trust", CODE_FOLDER_DIR], {}, async () => { test("should redirect to the passed in folder using human-readable query", async ({ codeServerPage }) => { const url = new URL(codeServerPage.page.url()) - expect(url.pathname).toBe("/") + const pathname = getMaybeProxiedPathname(url) + expect(pathname).toBe("/") expect(url.search).toBe(`?folder=${CODE_FOLDER_DIR}`) }) }) @@ -74,7 +71,8 @@ describe( await codeServerPage.navigate(`/`) const url = new URL(codeServerPage.page.url()) - expect(url.pathname).toBe("/") + const pathname = getMaybeProxiedPathname(url) + expect(pathname).toBe("/") expect(url.search).toBe("") }) }, @@ -91,7 +89,8 @@ describe("VS Code Routes with no workspace or folder", ["--disable-workspace-tru for (const route of routes) { await codeServerPage.navigate(route) const url = new URL(codeServerPage.page.url()) - expect(url.pathname).toBe(route) + const pathname = getMaybeProxiedPathname(url) + expect(pathname).toBe(route) expect(url.search).toBe(`?folder=${folder}&workspace=${workspace}`) } }) @@ -106,7 +105,8 @@ describe("VS Code Routes with no workspace or folder", ["--disable-workspace-tru // Closing the folder should stop the redirecting. await codeServerPage.navigate("/?ew=true") let url = new URL(codeServerPage.page.url()) - expect(url.pathname).toBe("/") + const pathname = getMaybeProxiedPathname(url) + expect(pathname).toBe("/") expect(url.search).toBe("?ew=true") }) }) diff --git a/test/unit/helpers.test.ts b/test/unit/helpers.test.ts index ba3a54d28..28420b060 100644 --- a/test/unit/helpers.test.ts +++ b/test/unit/helpers.test.ts @@ -1,5 +1,6 @@ import { promises as fs } from "fs" -import { clean, getAvailablePort, tmpdir, useEnv } from "../../test/utils/helpers" +import { clean, getAvailablePort, getMaybeProxiedPathname, tmpdir, useEnv } from "../../test/utils/helpers" +import { REVERSE_PROXY_BASE_PATH } from "../utils/constants" /** * This file is for testing test helpers (not core code). @@ -56,3 +57,22 @@ describe("getAvailablePort", () => { expect(portOne).not.toEqual(portTwo) }) }) + +describe("getMaybeProxiedPathname", () => { + it("should return the route", () => { + const route = "/vscode" + const url = new URL(`http://localhost:3000${route}`) + const actual = getMaybeProxiedPathname(url) + expect(actual).toBe(route) + }) + it("should strip proxy if env var set", () => { + const envKey = "USE_PROXY" + const [setValue, resetValue] = useEnv(envKey) + setValue("1") + const route = "/vscode" + const url = new URL(`http://localhost:3000/8000/${REVERSE_PROXY_BASE_PATH}${route}`) + const actual = getMaybeProxiedPathname(url) + expect(actual).toBe(route) + resetValue() + }) +}) \ No newline at end of file diff --git a/test/utils/helpers.ts b/test/utils/helpers.ts index 889ea48ce..cd54be486 100644 --- a/test/utils/helpers.ts +++ b/test/utils/helpers.ts @@ -136,3 +136,17 @@ export async function getMaybeProxiedCodeServer(codeServer: CodeServerPage | Cod return address } + +/** + * Stripes proxy base from url.pathname + * i.e. //ide + route returns just route + */ +export function getMaybeProxiedPathname(url: URL): string { + if (process.env.USE_PROXY === "1") { + // Behind proxy, path will be //ide + route + const pathWithoutProxy = url.pathname.split(`/${REVERSE_PROXY_BASE_PATH}`)[1] + return pathWithoutProxy + } + + return url.pathname +}