Skip to content

Commit b04ddc8

Browse files
committed
refactor: getMaybeProxied address function
1 parent 3d31c81 commit b04ddc8

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

test/e2e/codeServer.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { promises as fs } from "fs"
33
import * as os from "os"
44
import * as path from "path"
55
import * as util from "util"
6+
import { getMaybeProxiedCodeServer } from "../utils/helpers"
67
import { describe, test, expect } from "./baseFixture"
78
import { CodeServer } from "./models/CodeServer"
89

@@ -48,7 +49,8 @@ describe("code-server", [], {}, () => {
4849
const url = codeServerPage.page.url()
4950
// We use match because there may be a / at the end
5051
// so we don't want it to fail if we expect http://localhost:8080 to match http://localhost:8080/
51-
expect(url).toMatch(await codeServerPage.address())
52+
const address = await getMaybeProxiedCodeServer(codeServerPage)
53+
expect(url).toMatch(address)
5254
})
5355

5456
test("should always see the code-server editor", async ({ codeServerPage }) => {
@@ -92,6 +94,7 @@ describe("code-server", [], {}, () => {
9294
// domain and can write to the same database.
9395
const cs = await spawn("4.0.2", dir)
9496
const address = new URL(await cs.address())
97+
9598
await codeServerPage.navigate("/proxy/" + address.port + "/")
9699
await codeServerPage.openFile(files[1])
97100
expect(await codeServerPage.tabIsVisible(files[0])).toBe(false)

test/e2e/extensions.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import * as path from "path"
22
import { test as base } from "@playwright/test"
33
import { describe, test, expect } from "./baseFixture"
4+
import { getMaybeProxiedCodeServer } from "../utils/helpers"
45

56
function runTestExtensionTests() {
67
// This will only work if the test extension is loaded into code-server.
78
test("should have access to VSCODE_PROXY_URI", async ({ codeServerPage }) => {
8-
const address = await codeServerPage.address()
9+
const address = await getMaybeProxiedCodeServer(codeServerPage)
910

1011
await codeServerPage.executeCommandViaMenus("code-server: Get proxy URI")
1112

test/e2e/models/CodeServer.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as util from "util"
77
import { logError, normalize, plural } from "../../../src/common/util"
88
import { onLine } from "../../../src/node/util"
99
import { PASSWORD, workspaceDir } from "../../utils/constants"
10-
import { idleTimer, tmpdir } from "../../utils/helpers"
10+
import { getMaybeProxiedCodeServer, getProxiedCodeServer, idleTimer, tmpdir } from "../../utils/helpers"
1111

1212
interface CodeServerProcess {
1313
process: cp.ChildProcess
@@ -62,10 +62,10 @@ export class CodeServer {
6262
// NOTE@jsjoeio - when enabled, we assume code-server is running
6363
// via a reverse proxy with something like Caddy
6464
// and being accessed at host/port i.e. localhost:8000/1337
65-
if (process.env.USE_PROXY && process.env.USE_PROXY === "1") {
66-
const uri = new URL(address)
67-
return `http://${uri.hostname}:8000/${uri.port}/ide/`
68-
}
65+
// if (process.env.USE_PROXY && process.env.USE_PROXY === "1") {
66+
// const uri = new URL(address)
67+
// return `http://${uri.hostname}:8000/${uri.port}/ide/`
68+
// }
6969

7070
return address
7171
}
@@ -242,7 +242,8 @@ export class CodeServerPage {
242242
* editor to become available.
243243
*/
244244
async navigate(endpoint = "/") {
245-
const noramlizedUrl = normalize((await this.codeServer.address()) + endpoint, true)
245+
const address = await getMaybeProxiedCodeServer(this.codeServer)
246+
const noramlizedUrl = normalize(address + endpoint, true)
246247
const to = new URL(noramlizedUrl)
247248

248249
this.codeServer.logger.info(`navigating to ${to}`)

test/e2e/terminal.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as cp from "child_process"
22
import { promises as fs } from "fs"
33
import * as path from "path"
44
import util from "util"
5-
import { clean, tmpdir } from "../utils/helpers"
5+
import { clean, getMaybeProxiedCodeServer, tmpdir } from "../utils/helpers"
66
import { describe, expect, test } from "./baseFixture"
77

88
describe("Integrated Terminal", [], {}, () => {
@@ -26,7 +26,8 @@ describe("Integrated Terminal", [], {}, () => {
2626
await codeServerPage.page.keyboard.press("Enter")
2727

2828
const { stdout } = await output
29-
expect(stdout).toMatch(await codeServerPage.address())
29+
const address = await getMaybeProxiedCodeServer(codeServerPage)
30+
expect(stdout).toMatch(address)
3031
})
3132

3233
test("should be able to invoke `code-server` to open a file", async ({ codeServerPage }) => {

test/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export const PASSWORD = "e45432jklfdsab"
22
export const workspaceDir = "workspaces"
3+
export const REVERSE_PROXY_BASE_PATH = "ide"
4+
export const REVERSE_PROXY_PORT = "8000"

test/utils/helpers.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { promises as fs } from "fs"
33
import * as net from "net"
44
import * as os from "os"
55
import * as path from "path"
6+
import { CodeServer, CodeServerPage } from "../e2e/models/CodeServer"
7+
import { REVERSE_PROXY_PORT, REVERSE_PROXY_BASE_PATH } from "./constants"
68

79
/**
810
* Spy on the logger and console and replace with mock implementations to
@@ -119,3 +121,18 @@ export function isAddressInfo(address: unknown): address is net.AddressInfo {
119121
(address as net.AddressInfo).address !== undefined
120122
)
121123
}
124+
125+
/**
126+
* If using a proxy, return the address of the proxy.
127+
*
128+
* Otherwise, return the direct address of code-server.
129+
*/
130+
export async function getMaybeProxiedCodeServer(codeServer: CodeServerPage | CodeServer): Promise<string> {
131+
let address = await codeServer.address()
132+
if (process.env.USE_PROXY && process.env.USE_PROXY === "1") {
133+
const uri = new URL(address)
134+
return `http://${uri.hostname}:${REVERSE_PROXY_PORT}/${uri.port}/${REVERSE_PROXY_BASE_PATH}/`
135+
}
136+
137+
return address
138+
}

0 commit comments

Comments
 (0)