Skip to content

Commit fe33adc

Browse files
committed
refactor: add helper fn getMaybeProxiedPathname
1 parent 19b7ba8 commit fe33adc

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

test/e2e/routes.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, test, expect } from "./baseFixture"
2-
import { clean } from "../utils/helpers"
2+
import { clean, getMaybeProxiedPathname } from "../utils/helpers"
33
import { REVERSE_PROXY_BASE_PATH } from "../utils/constants"
44

55
const routes = ["/", "/vscode", "/vscode/"]
@@ -16,13 +16,8 @@ describe("VS Code Routes", ["--disable-workspace-trust"], {}, async () => {
1616

1717
// Check there were no redirections
1818
const url = new URL(codeServerPage.page.url())
19-
if (process.env.USE_PROXY === "1") {
20-
// Behind proxy, path will be /<port/ide + route
21-
const pathWithoutProxy = url.pathname.split(`/${REVERSE_PROXY_BASE_PATH}`)[1]
22-
expect(pathWithoutProxy).toBe(route)
23-
} else {
24-
expect(url.pathname).toBe(route)
25-
}
19+
const pathname = getMaybeProxiedPathname(url)
20+
expect(pathname).toBe(route)
2621

2722
// TODO@jsjoeio
2823
// 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 || ""
4843
describe("VS Code Routes with code-workspace", ["--disable-workspace-trust", CODE_WORKSPACE_DIR], {}, async () => {
4944
test("should redirect to the passed in workspace using human-readable query", async ({ codeServerPage }) => {
5045
const url = new URL(codeServerPage.page.url())
51-
expect(url.pathname).toBe("/")
46+
const pathname = getMaybeProxiedPathname(url)
47+
expect(pathname).toBe("/")
5248
expect(url.search).toBe(`?workspace=${CODE_WORKSPACE_DIR}`)
5349
})
5450
})
@@ -57,7 +53,8 @@ const CODE_FOLDER_DIR = process.env.CODE_FOLDER_DIR || ""
5753
describe("VS Code Routes with code-workspace", ["--disable-workspace-trust", CODE_FOLDER_DIR], {}, async () => {
5854
test("should redirect to the passed in folder using human-readable query", async ({ codeServerPage }) => {
5955
const url = new URL(codeServerPage.page.url())
60-
expect(url.pathname).toBe("/")
56+
const pathname = getMaybeProxiedPathname(url)
57+
expect(pathname).toBe("/")
6158
expect(url.search).toBe(`?folder=${CODE_FOLDER_DIR}`)
6259
})
6360
})
@@ -74,7 +71,8 @@ describe(
7471
await codeServerPage.navigate(`/`)
7572

7673
const url = new URL(codeServerPage.page.url())
77-
expect(url.pathname).toBe("/")
74+
const pathname = getMaybeProxiedPathname(url)
75+
expect(pathname).toBe("/")
7876
expect(url.search).toBe("")
7977
})
8078
},
@@ -91,7 +89,8 @@ describe("VS Code Routes with no workspace or folder", ["--disable-workspace-tru
9189
for (const route of routes) {
9290
await codeServerPage.navigate(route)
9391
const url = new URL(codeServerPage.page.url())
94-
expect(url.pathname).toBe(route)
92+
const pathname = getMaybeProxiedPathname(url)
93+
expect(pathname).toBe(route)
9594
expect(url.search).toBe(`?folder=${folder}&workspace=${workspace}`)
9695
}
9796
})
@@ -106,7 +105,8 @@ describe("VS Code Routes with no workspace or folder", ["--disable-workspace-tru
106105
// Closing the folder should stop the redirecting.
107106
await codeServerPage.navigate("/?ew=true")
108107
let url = new URL(codeServerPage.page.url())
109-
expect(url.pathname).toBe("/")
108+
const pathname = getMaybeProxiedPathname(url)
109+
expect(pathname).toBe("/")
110110
expect(url.search).toBe("?ew=true")
111111
})
112112
})

test/unit/helpers.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { promises as fs } from "fs"
2-
import { clean, getAvailablePort, tmpdir, useEnv } from "../../test/utils/helpers"
2+
import { clean, getAvailablePort, getMaybeProxiedPathname, tmpdir, useEnv } from "../../test/utils/helpers"
3+
import { REVERSE_PROXY_BASE_PATH } from "../utils/constants"
34

45
/**
56
* This file is for testing test helpers (not core code).
@@ -56,3 +57,22 @@ describe("getAvailablePort", () => {
5657
expect(portOne).not.toEqual(portTwo)
5758
})
5859
})
60+
61+
describe("getMaybeProxiedPathname", () => {
62+
it("should return the route", () => {
63+
const route = "/vscode"
64+
const url = new URL(`http://localhost:3000${route}`)
65+
const actual = getMaybeProxiedPathname(url)
66+
expect(actual).toBe(route)
67+
})
68+
it("should strip proxy if env var set", () => {
69+
const envKey = "USE_PROXY"
70+
const [setValue, resetValue] = useEnv(envKey)
71+
setValue("1")
72+
const route = "/vscode"
73+
const url = new URL(`http://localhost:3000/8000/${REVERSE_PROXY_BASE_PATH}${route}`)
74+
const actual = getMaybeProxiedPathname(url)
75+
expect(actual).toBe(route)
76+
resetValue()
77+
})
78+
})

test/utils/helpers.ts

+14
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,17 @@ export async function getMaybeProxiedCodeServer(codeServer: CodeServerPage | Cod
136136

137137
return address
138138
}
139+
140+
/**
141+
* Stripes proxy base from url.pathname
142+
* i.e. /<port>/ide + route returns just route
143+
*/
144+
export function getMaybeProxiedPathname(url: URL): string {
145+
if (process.env.USE_PROXY === "1") {
146+
// Behind proxy, path will be /<port>/ide + route
147+
const pathWithoutProxy = url.pathname.split(`/${REVERSE_PROXY_BASE_PATH}`)[1]
148+
return pathWithoutProxy
149+
}
150+
151+
return url.pathname
152+
}

0 commit comments

Comments
 (0)