Skip to content

Commit 6a53c2b

Browse files
committed
Update tests to reflect new upstream behavior.
1 parent 7978a0c commit 6a53c2b

File tree

4 files changed

+13
-98
lines changed

4 files changed

+13
-98
lines changed

src/common/util.ts

-29
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,6 @@ export const resolveBase = (base?: string): string => {
5050
return normalize(url.pathname)
5151
}
5252

53-
/**
54-
* Get client-side configuration embedded in the HTML or query params.
55-
*/
56-
export const getClientConfiguration = <T extends CodeServerLib.ClientConfiguration>(): T => {
57-
let config: T
58-
try {
59-
config = JSON.parse(document.getElementById("coder-options")!.getAttribute("data-settings")!)
60-
} catch (error) {
61-
config = {} as T
62-
}
63-
64-
// You can also pass options in stringified form to the options query
65-
// variable. Options provided here will override the ones in the options
66-
// element.
67-
const params = new URLSearchParams(location.search)
68-
const queryOpts = params.get("options")
69-
if (queryOpts) {
70-
config = {
71-
...config,
72-
...JSON.parse(queryOpts),
73-
}
74-
}
75-
76-
config.base = resolveBase(config.base)
77-
config.csStaticBase = resolveBase(config.csStaticBase)
78-
79-
return config
80-
}
81-
8253
/**
8354
* Wrap the value in an array if it's not already an array. If the value is
8455
* undefined return an empty array.

src/node/http.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import { version as codeServerVersion } from "./constants"
1010
import { Heart } from "./heart"
1111
import { getPasswordMethod, IsCookieValidArgs, isCookieValid, sanitizeString, escapeHtml, escapeJSON } from "./util"
1212

13+
/**
14+
* Base options included on every page.
15+
*/
16+
export interface ClientConfiguration {
17+
codeServerVersion: string
18+
base: string
19+
csStaticBase: string
20+
}
21+
1322
declare global {
1423
// eslint-disable-next-line @typescript-eslint/no-namespace
1524
namespace Express {
@@ -20,7 +29,7 @@ declare global {
2029
}
2130
}
2231

23-
export const createClientConfiguration = (req: express.Request): CodeServerLib.ClientConfiguration => {
32+
export const createClientConfiguration = (req: express.Request): ClientConfiguration => {
2433
const base = relativeRoot(req)
2534

2635
return {
@@ -38,7 +47,7 @@ export const replaceTemplates = <T extends object>(
3847
content: string,
3948
extraOpts?: Omit<T, "base" | "csStaticBase" | "logLevel">,
4049
): string => {
41-
const serverOptions: CodeServerLib.ClientConfiguration = {
50+
const serverOptions: ClientConfiguration = {
4251
...createClientConfiguration(req),
4352
...extraOpts,
4453
}

test/unit/common/util.test.ts

-65
Original file line numberDiff line numberDiff line change
@@ -110,71 +110,6 @@ describe("util", () => {
110110
})
111111
})
112112

113-
describe("getOptions", () => {
114-
beforeEach(() => {
115-
const location: LocationLike = {
116-
pathname: "/healthz",
117-
origin: "http://localhost:8080",
118-
// search: "?environmentId=600e0187-0909d8a00cb0a394720d4dce",
119-
}
120-
121-
// Because resolveBase is not a pure function
122-
// and relies on the global location to be set
123-
// we set it before all the tests
124-
// and tell TS that our location should be looked at
125-
// as Location (even though it's missing some properties)
126-
global.location = location as Location
127-
})
128-
129-
afterEach(() => {
130-
jest.restoreAllMocks()
131-
})
132-
133-
it("should return options with base and cssStaticBase even if it doesn't exist", () => {
134-
expect(util.getClientConfiguration()).toStrictEqual({
135-
base: "",
136-
csStaticBase: "",
137-
})
138-
})
139-
140-
it("should return options when they do exist", () => {
141-
// Mock getElementById
142-
const spy = jest.spyOn(document, "getElementById")
143-
// Create a fake element and set the attribute
144-
const mockElement = document.createElement("div")
145-
mockElement.setAttribute(
146-
"data-settings",
147-
'{"base":".","csStaticBase":"./static/development/Users/jp/Dev/code-server","logLevel":2,"disableUpdateCheck":false}',
148-
)
149-
// Return mockElement from the spy
150-
// this way, when we call "getElementById"
151-
// it returns the element
152-
spy.mockImplementation(() => mockElement)
153-
154-
expect(util.getClientConfiguration()).toStrictEqual({
155-
base: "",
156-
csStaticBase: "/static/development/Users/jp/Dev/code-server",
157-
disableUpdateCheck: false,
158-
logLevel: 2,
159-
})
160-
})
161-
162-
it("should include queryOpts", () => {
163-
// Trying to understand how the implementation works
164-
// 1. It grabs the search params from location.search (i.e. ?)
165-
// 2. it then grabs the "options" param if it exists
166-
// 3. then it creates a new options object
167-
// spreads the original options
168-
// then parses the queryOpts
169-
location.search = '?options={"logLevel":2}'
170-
expect(util.getClientConfiguration()).toStrictEqual({
171-
base: "",
172-
csStaticBase: "",
173-
logLevel: 2,
174-
})
175-
})
176-
})
177-
178113
describe("arrayify", () => {
179114
it("should return value it's already an array", () => {
180115
expect(util.arrayify(["hello", "world"])).toStrictEqual(["hello", "world"])

test/unit/node/routes/static.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as integration from "../../../utils/integration"
77

88
const NOT_FOUND = {
99
code: 404,
10-
message: "Not Found",
10+
message: /not found/i,
1111
}
1212

1313
describe("/_static", () => {
@@ -44,7 +44,7 @@ describe("/_static", () => {
4444
expect(resp.status).toBe(NOT_FOUND.code)
4545

4646
const content = await resp.json()
47-
expect(content.error).toContain(NOT_FOUND.message)
47+
expect(content.error).toMatch(NOT_FOUND.message)
4848
})
4949
}
5050

0 commit comments

Comments
 (0)