Archived
1
0

refactor: add helper fn getMaybeProxiedPathname

This commit is contained in:
Joe Previte 2022-12-21 15:13:59 -07:00
parent 19b7ba8735
commit fe33adc4bf
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
3 changed files with 48 additions and 14 deletions

View File

@ -1,5 +1,5 @@
import { describe, test, expect } from "./baseFixture" 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" import { REVERSE_PROXY_BASE_PATH } from "../utils/constants"
const routes = ["/", "/vscode", "/vscode/"] const routes = ["/", "/vscode", "/vscode/"]
@ -16,13 +16,8 @@ describe("VS Code Routes", ["--disable-workspace-trust"], {}, async () => {
// Check there were no redirections // Check there were no redirections
const url = new URL(codeServerPage.page.url()) const url = new URL(codeServerPage.page.url())
if (process.env.USE_PROXY === "1") { const pathname = getMaybeProxiedPathname(url)
// Behind proxy, path will be /<port/ide + route expect(pathname).toBe(route)
const pathWithoutProxy = url.pathname.split(`/${REVERSE_PROXY_BASE_PATH}`)[1]
expect(pathWithoutProxy).toBe(route)
} else {
expect(url.pathname).toBe(route)
}
// TODO@jsjoeio // TODO@jsjoeio
// now that we are in a proper browser instead of scraping the HTML we // now that we are in a proper browser instead of scraping the HTML we
@ -48,7 +43,8 @@ const CODE_WORKSPACE_DIR = process.env.CODE_WORKSPACE_DIR || ""
describe("VS Code Routes with code-workspace", ["--disable-workspace-trust", CODE_WORKSPACE_DIR], {}, async () => { describe("VS Code Routes with code-workspace", ["--disable-workspace-trust", CODE_WORKSPACE_DIR], {}, async () => {
test("should redirect to the passed in workspace using human-readable query", async ({ codeServerPage }) => { test("should redirect to the passed in workspace using human-readable query", async ({ codeServerPage }) => {
const url = new URL(codeServerPage.page.url()) 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}`) 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 () => { 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 }) => { test("should redirect to the passed in folder using human-readable query", async ({ codeServerPage }) => {
const url = new URL(codeServerPage.page.url()) 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}`) expect(url.search).toBe(`?folder=${CODE_FOLDER_DIR}`)
}) })
}) })
@ -74,7 +71,8 @@ describe(
await codeServerPage.navigate(`/`) await codeServerPage.navigate(`/`)
const url = new URL(codeServerPage.page.url()) const url = new URL(codeServerPage.page.url())
expect(url.pathname).toBe("/") const pathname = getMaybeProxiedPathname(url)
expect(pathname).toBe("/")
expect(url.search).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) { for (const route of routes) {
await codeServerPage.navigate(route) await codeServerPage.navigate(route)
const url = new URL(codeServerPage.page.url()) 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}`) 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. // Closing the folder should stop the redirecting.
await codeServerPage.navigate("/?ew=true") await codeServerPage.navigate("/?ew=true")
let url = new URL(codeServerPage.page.url()) let url = new URL(codeServerPage.page.url())
expect(url.pathname).toBe("/") const pathname = getMaybeProxiedPathname(url)
expect(pathname).toBe("/")
expect(url.search).toBe("?ew=true") expect(url.search).toBe("?ew=true")
}) })
}) })

View File

@ -1,5 +1,6 @@
import { promises as fs } from "fs" 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). * This file is for testing test helpers (not core code).
@ -56,3 +57,22 @@ describe("getAvailablePort", () => {
expect(portOne).not.toEqual(portTwo) 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()
})
})

View File

@ -136,3 +136,17 @@ export async function getMaybeProxiedCodeServer(codeServer: CodeServerPage | Cod
return address return address
} }
/**
* Stripes proxy base from url.pathname
* i.e. /<port>/ide + route returns just route
*/
export function getMaybeProxiedPathname(url: URL): string {
if (process.env.USE_PROXY === "1") {
// Behind proxy, path will be /<port>/ide + route
const pathWithoutProxy = url.pathname.split(`/${REVERSE_PROXY_BASE_PATH}`)[1]
return pathWithoutProxy
}
return url.pathname
}