|
1 | 1 | import { getMockReq } from "@jest-mock/express"
|
2 |
| -import { constructRedirectPath, relativeRoot } from "../../../src/node/http" |
| 2 | +import * as http from "../../../src/node/http" |
| 3 | +import { mockLogger } from "../../utils/helpers" |
3 | 4 |
|
4 | 5 | describe("http", () => {
|
| 6 | + beforeEach(() => { |
| 7 | + mockLogger() |
| 8 | + }) |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + jest.clearAllMocks() |
| 12 | + }) |
| 13 | + |
5 | 14 | it("should construct a relative path to the root", () => {
|
6 |
| - expect(relativeRoot("/")).toStrictEqual(".") |
7 |
| - expect(relativeRoot("/foo")).toStrictEqual(".") |
8 |
| - expect(relativeRoot("/foo/")).toStrictEqual("./..") |
9 |
| - expect(relativeRoot("/foo/bar ")).toStrictEqual("./..") |
10 |
| - expect(relativeRoot("/foo/bar/")).toStrictEqual("./../..") |
| 15 | + expect(http.relativeRoot("/")).toStrictEqual(".") |
| 16 | + expect(http.relativeRoot("/foo")).toStrictEqual(".") |
| 17 | + expect(http.relativeRoot("/foo/")).toStrictEqual("./..") |
| 18 | + expect(http.relativeRoot("/foo/bar ")).toStrictEqual("./..") |
| 19 | + expect(http.relativeRoot("/foo/bar/")).toStrictEqual("./../..") |
11 | 20 | })
|
12 |
| -}) |
13 | 21 |
|
14 |
| -describe("constructRedirectPath", () => { |
15 |
| - it("should preserve slashes in queryString so they are human-readable", () => { |
16 |
| - const mockReq = getMockReq({ |
17 |
| - originalUrl: "localhost:8080", |
| 22 | + describe("origin", () => { |
| 23 | + ;[ |
| 24 | + { |
| 25 | + origin: "", |
| 26 | + host: "", |
| 27 | + expected: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + origin: "http://localhost:8080", |
| 31 | + host: "", |
| 32 | + expected: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + origin: "http://localhost:8080", |
| 36 | + host: "localhost:8080", |
| 37 | + expected: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + origin: "http://localhost:8080", |
| 41 | + host: "localhost:8081", |
| 42 | + expected: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + origin: "localhost:8080", |
| 46 | + host: "localhost:8080", |
| 47 | + expected: false, // Gets parsed as host: localhost and path: 8080. |
| 48 | + }, |
| 49 | + { |
| 50 | + origin: "test.org", |
| 51 | + host: "localhost:8080", |
| 52 | + expected: false, // Parsing fails completely. |
| 53 | + }, |
| 54 | + ].forEach((test) => { |
| 55 | + ;[ |
| 56 | + ["host", test.host], |
| 57 | + ["x-forwarded-host", test.host], |
| 58 | + ["forwarded", `for=127.0.0.1, host=${test.host}, proto=http`], |
| 59 | + ["forwarded", `for=127.0.0.1;proto=http;host=${test.host}`], |
| 60 | + ["forwarded", `proto=http;host=${test.host}, for=127.0.0.1`], |
| 61 | + ].forEach(([key, value]) => { |
| 62 | + it(`${test.origin} -> [${key}: ${value}]`, () => { |
| 63 | + const req = getMockReq({ |
| 64 | + originalUrl: "localhost:8080", |
| 65 | + headers: { |
| 66 | + origin: test.origin, |
| 67 | + [key]: value, |
| 68 | + }, |
| 69 | + }) |
| 70 | + expect(http.authenticateOrigin(req)).toBe(test.expected) |
| 71 | + }) |
| 72 | + }) |
18 | 73 | })
|
19 |
| - const mockQueryParams = { folder: "/Users/jp/dev/coder" } |
20 |
| - const mockTo = "" |
21 |
| - const actual = constructRedirectPath(mockReq, mockQueryParams, mockTo) |
22 |
| - const expected = "./?folder=/Users/jp/dev/coder" |
23 |
| - expect(actual).toBe(expected) |
24 | 74 | })
|
25 |
| - it("should use an empty string if no query params", () => { |
26 |
| - const mockReq = getMockReq({ |
27 |
| - originalUrl: "localhost:8080", |
| 75 | + |
| 76 | + describe("constructRedirectPath", () => { |
| 77 | + it("should preserve slashes in queryString so they are human-readable", () => { |
| 78 | + const mockReq = getMockReq({ |
| 79 | + originalUrl: "localhost:8080", |
| 80 | + }) |
| 81 | + const mockQueryParams = { folder: "/Users/jp/dev/coder" } |
| 82 | + const mockTo = "" |
| 83 | + const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo) |
| 84 | + const expected = "./?folder=/Users/jp/dev/coder" |
| 85 | + expect(actual).toBe(expected) |
28 | 86 | })
|
29 |
| - const mockQueryParams = {} |
30 |
| - const mockTo = "" |
31 |
| - const actual = constructRedirectPath(mockReq, mockQueryParams, mockTo) |
32 |
| - const expected = "./" |
33 |
| - expect(actual).toBe(expected) |
34 |
| - }) |
35 |
| - it("should append the 'to' path relative to the originalUrl", () => { |
36 |
| - const mockReq = getMockReq({ |
37 |
| - originalUrl: "localhost:8080", |
| 87 | + it("should use an empty string if no query params", () => { |
| 88 | + const mockReq = getMockReq({ |
| 89 | + originalUrl: "localhost:8080", |
| 90 | + }) |
| 91 | + const mockQueryParams = {} |
| 92 | + const mockTo = "" |
| 93 | + const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo) |
| 94 | + const expected = "./" |
| 95 | + expect(actual).toBe(expected) |
38 | 96 | })
|
39 |
| - const mockQueryParams = {} |
40 |
| - const mockTo = "vscode" |
41 |
| - const actual = constructRedirectPath(mockReq, mockQueryParams, mockTo) |
42 |
| - const expected = "./vscode" |
43 |
| - expect(actual).toBe(expected) |
44 |
| - }) |
45 |
| - it("should append append queryParams after 'to' path", () => { |
46 |
| - const mockReq = getMockReq({ |
47 |
| - originalUrl: "localhost:8080", |
| 97 | + it("should append the 'to' path relative to the originalUrl", () => { |
| 98 | + const mockReq = getMockReq({ |
| 99 | + originalUrl: "localhost:8080", |
| 100 | + }) |
| 101 | + const mockQueryParams = {} |
| 102 | + const mockTo = "vscode" |
| 103 | + const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo) |
| 104 | + const expected = "./vscode" |
| 105 | + expect(actual).toBe(expected) |
| 106 | + }) |
| 107 | + it("should append append queryParams after 'to' path", () => { |
| 108 | + const mockReq = getMockReq({ |
| 109 | + originalUrl: "localhost:8080", |
| 110 | + }) |
| 111 | + const mockQueryParams = { folder: "/Users/jp/dev/coder" } |
| 112 | + const mockTo = "vscode" |
| 113 | + const actual = http.constructRedirectPath(mockReq, mockQueryParams, mockTo) |
| 114 | + const expected = "./vscode?folder=/Users/jp/dev/coder" |
| 115 | + expect(actual).toBe(expected) |
48 | 116 | })
|
49 |
| - const mockQueryParams = { folder: "/Users/jp/dev/coder" } |
50 |
| - const mockTo = "vscode" |
51 |
| - const actual = constructRedirectPath(mockReq, mockQueryParams, mockTo) |
52 |
| - const expected = "./vscode?folder=/Users/jp/dev/coder" |
53 |
| - expect(actual).toBe(expected) |
54 | 117 | })
|
55 | 118 | })
|
0 commit comments