Skip to content

Commit a0e928e

Browse files
committed
Add tests for relativeRoot
1 parent 9d9f3a4 commit a0e928e

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/node/http.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ declare global {
3333
}
3434

3535
export const createClientConfiguration = (req: express.Request): ClientConfiguration => {
36-
const base = relativeRoot(req)
36+
const base = relativeRoot(req.originalUrl)
3737

3838
return {
3939
base,
@@ -108,15 +108,28 @@ export const authenticated = async (req: express.Request): Promise<boolean> => {
108108

109109
/**
110110
* Get the relative path that will get us to the root of the page. For each
111-
* slash we need to go up a directory. For example:
111+
* slash we need to go up a directory. Will not have a trailing slash.
112+
*
113+
* For example:
114+
*
112115
* / => .
113116
* /foo => .
114117
* /foo/ => ./..
115118
* /foo/bar => ./..
116119
* /foo/bar/ => ./../..
120+
*
121+
* All paths must be relative in order to work behind a reverse proxy since we
122+
* we do not know the base path. Anything that needs to be absolute (for
123+
* example cookies) must get the base path from the frontend.
124+
*
125+
* All relative paths must be prefixed with the relative root to ensure they
126+
* work no matter the depth at which they happen to appear.
127+
*
128+
* For Express `req.originalUrl` should be used as they remove the base from the
129+
* standard `url` property making it impossible to get the true depth.
117130
*/
118-
export const relativeRoot = (req: express.Request): string => {
119-
const depth = (req.originalUrl.split("?", 1)[0].match(/\//g) || []).length
131+
export const relativeRoot = (originalUrl: string): string => {
132+
const depth = (originalUrl.split("?", 1)[0].match(/\//g) || []).length
120133
return normalize("./" + (depth > 1 ? "../".repeat(depth - 1) : ""))
121134
}
122135

@@ -138,7 +151,7 @@ export const redirect = (
138151
}
139152
})
140153

141-
const relativePath = normalize(`${relativeRoot(req)}/${to}`, true)
154+
const relativePath = normalize(`${relativeRoot(req.originalUrl)}/${to}`, true)
142155
const queryString = qs.stringify(query)
143156
const redirectPath = `${relativePath}${queryString ? `?${queryString}` : ""}`
144157
logger.debug(`redirecting from ${req.originalUrl} to ${redirectPath}`)

test/unit/node/http.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { relativeRoot } from "../../../src/node/http"
2+
3+
describe("http", () => {
4+
it("should construct a relative path to the root", () => {
5+
expect(relativeRoot("/")).toStrictEqual(".")
6+
expect(relativeRoot("/foo")).toStrictEqual(".")
7+
expect(relativeRoot("/foo/")).toStrictEqual("./..")
8+
expect(relativeRoot("/foo/bar ")).toStrictEqual("./..")
9+
expect(relativeRoot("/foo/bar/")).toStrictEqual("./../..")
10+
})
11+
})

0 commit comments

Comments
 (0)