2022-03-01 20:11:56 +01:00
|
|
|
import { getMockReq } from "@jest-mock/express"
|
2023-03-03 10:12:34 +01:00
|
|
|
import * as http from "../../../src/node/http"
|
|
|
|
import { mockLogger } from "../../utils/helpers"
|
2021-12-08 22:52:15 +01:00
|
|
|
|
|
|
|
describe("http", () => {
|
2023-03-03 10:12:34 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
mockLogger()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2021-12-08 22:52:15 +01:00
|
|
|
it("should construct a relative path to the root", () => {
|
2023-03-03 10:12:34 +01:00
|
|
|
expect(http.relativeRoot("/")).toStrictEqual(".")
|
|
|
|
expect(http.relativeRoot("/foo")).toStrictEqual(".")
|
|
|
|
expect(http.relativeRoot("/foo/")).toStrictEqual("./..")
|
|
|
|
expect(http.relativeRoot("/foo/bar ")).toStrictEqual("./..")
|
|
|
|
expect(http.relativeRoot("/foo/bar/")).toStrictEqual("./../..")
|
2021-12-08 22:52:15 +01:00
|
|
|
})
|
2022-03-01 20:11:56 +01:00
|
|
|
|
2023-03-03 10:12:34 +01:00
|
|
|
describe("origin", () => {
|
|
|
|
;[
|
|
|
|
{
|
|
|
|
origin: "",
|
|
|
|
host: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
origin: "http://localhost:8080",
|
|
|
|
host: "",
|
2023-03-30 19:24:33 +02:00
|
|
|
expected: "no host headers",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
origin: "http://localhost:8080",
|
|
|
|
host: " ",
|
|
|
|
expected: "does not match",
|
2023-03-03 10:12:34 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
origin: "http://localhost:8080",
|
|
|
|
host: "localhost:8080",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
origin: "http://localhost:8080",
|
|
|
|
host: "localhost:8081",
|
2023-03-30 19:24:33 +02:00
|
|
|
expected: "does not match",
|
2023-03-03 10:12:34 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
origin: "localhost:8080",
|
|
|
|
host: "localhost:8080",
|
2023-03-30 19:24:33 +02:00
|
|
|
expected: "does not match", // Gets parsed as host: localhost and path: 8080.
|
2023-03-03 10:12:34 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
origin: "test.org",
|
|
|
|
host: "localhost:8080",
|
2023-03-30 19:24:33 +02:00
|
|
|
expected: "malformed", // Parsing fails completely.
|
2023-03-03 10:12:34 +01:00
|
|
|
},
|
|
|
|
].forEach((test) => {
|
|
|
|
;[
|
|
|
|
["host", test.host],
|
|
|
|
["x-forwarded-host", test.host],
|
2023-05-17 21:51:05 +02:00
|
|
|
["x-forwarded-host", `${test.host}, ${test.host}`],
|
2023-03-03 10:12:34 +01:00
|
|
|
["forwarded", `for=127.0.0.1, host=${test.host}, proto=http`],
|
|
|
|
["forwarded", `for=127.0.0.1;proto=http;host=${test.host}`],
|
|
|
|
["forwarded", `proto=http;host=${test.host}, for=127.0.0.1`],
|
|
|
|
].forEach(([key, value]) => {
|
|
|
|
it(`${test.origin} -> [${key}: ${value}]`, () => {
|
|
|
|
const req = getMockReq({
|
|
|
|
originalUrl: "localhost:8080",
|
|
|
|
headers: {
|
|
|
|
origin: test.origin,
|
|
|
|
[key]: value,
|
|
|
|
},
|
|
|
|
})
|
2023-03-30 19:24:33 +02:00
|
|
|
if (typeof test.expected === "string") {
|
|
|
|
expect(() => http.authenticateOrigin(req)).toThrow(test.expected)
|
|
|
|
} else {
|
|
|
|
expect(() => http.authenticateOrigin(req)).not.toThrow()
|
|
|
|
}
|
2023-03-03 10:12:34 +01:00
|
|
|
})
|
|
|
|
})
|
2022-03-01 20:11:56 +01:00
|
|
|
})
|
|
|
|
})
|
2023-03-03 10:12:34 +01:00
|
|
|
|
|
|
|
describe("constructRedirectPath", () => {
|
|
|
|
it("should preserve slashes in queryString so they are human-readable", () => {
|
|
|
|
const mockReq = getMockReq({
|
|
|
|
originalUrl: "localhost:8080",
|
|
|
|
})
|
|
|
|
const mockQueryParams = { folder: "/Users/jp/dev/coder" }
|
|
|
|
const mockTo = ""
|
|
|
|
const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo)
|
|
|
|
const expected = "./?folder=/Users/jp/dev/coder"
|
|
|
|
expect(actual).toBe(expected)
|
2022-03-01 20:11:56 +01:00
|
|
|
})
|
2023-03-03 10:12:34 +01:00
|
|
|
it("should use an empty string if no query params", () => {
|
|
|
|
const mockReq = getMockReq({
|
|
|
|
originalUrl: "localhost:8080",
|
|
|
|
})
|
|
|
|
const mockQueryParams = {}
|
|
|
|
const mockTo = ""
|
|
|
|
const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo)
|
|
|
|
const expected = "./"
|
|
|
|
expect(actual).toBe(expected)
|
2022-03-01 20:11:56 +01:00
|
|
|
})
|
2023-03-03 10:12:34 +01:00
|
|
|
it("should append the 'to' path relative to the originalUrl", () => {
|
|
|
|
const mockReq = getMockReq({
|
|
|
|
originalUrl: "localhost:8080",
|
|
|
|
})
|
|
|
|
const mockQueryParams = {}
|
|
|
|
const mockTo = "vscode"
|
|
|
|
const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo)
|
|
|
|
const expected = "./vscode"
|
|
|
|
expect(actual).toBe(expected)
|
|
|
|
})
|
|
|
|
it("should append append queryParams after 'to' path", () => {
|
|
|
|
const mockReq = getMockReq({
|
|
|
|
originalUrl: "localhost:8080",
|
|
|
|
})
|
|
|
|
const mockQueryParams = { folder: "/Users/jp/dev/coder" }
|
|
|
|
const mockTo = "vscode"
|
|
|
|
const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo)
|
|
|
|
const expected = "./vscode?folder=/Users/jp/dev/coder"
|
|
|
|
expect(actual).toBe(expected)
|
2022-03-01 20:11:56 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|