|
| 1 | +/* |
| 2 | +pnpm test ./http-proxy-passes-ws-incoming.test.ts |
| 3 | +*/ |
| 4 | + |
| 5 | +import { checkMethodAndHeader, XHeaders } from "../../dist/lib/http-proxy/passes/ws-incoming"; |
| 6 | + |
| 7 | +describe("#checkMethodAndHeader", () => { |
| 8 | + it("should drop non-GET connections", () => { |
| 9 | + let destroyCalled = false, |
| 10 | + stubRequest = { |
| 11 | + method: "DELETE", |
| 12 | + headers: {}, |
| 13 | + }, |
| 14 | + stubSocket = { |
| 15 | + destroy: () => { |
| 16 | + // Simulate Socket.destroy() method when call |
| 17 | + destroyCalled = true; |
| 18 | + }, |
| 19 | + }; |
| 20 | + const returnValue = checkMethodAndHeader(stubRequest, stubSocket); |
| 21 | + expect(returnValue).toBe(true); |
| 22 | + expect(destroyCalled).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should drop connections when no upgrade header", () => { |
| 26 | + let destroyCalled = false, |
| 27 | + stubRequest = { |
| 28 | + method: "GET", |
| 29 | + headers: {}, |
| 30 | + }, |
| 31 | + stubSocket = { |
| 32 | + destroy: () => { |
| 33 | + // Simulate Socket.destroy() method when call |
| 34 | + destroyCalled = true; |
| 35 | + }, |
| 36 | + }; |
| 37 | + const returnValue = checkMethodAndHeader(stubRequest, stubSocket); |
| 38 | + expect(returnValue).toBe(true); |
| 39 | + expect(destroyCalled).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should drop connections when upgrade header is different of `websocket`", () => { |
| 43 | + let destroyCalled = false, |
| 44 | + stubRequest = { |
| 45 | + method: "GET", |
| 46 | + headers: { |
| 47 | + upgrade: "anotherprotocol", |
| 48 | + }, |
| 49 | + }, |
| 50 | + stubSocket = { |
| 51 | + destroy: () => { |
| 52 | + // Simulate Socket.destroy() method when call |
| 53 | + destroyCalled = true; |
| 54 | + }, |
| 55 | + }; |
| 56 | + const returnValue = checkMethodAndHeader(stubRequest, stubSocket); |
| 57 | + expect(returnValue).toBe(true); |
| 58 | + expect(destroyCalled).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should return nothing when all is ok", () => { |
| 62 | + let destroyCalled = false, |
| 63 | + stubRequest = { |
| 64 | + method: "GET", |
| 65 | + headers: { |
| 66 | + upgrade: "websocket", |
| 67 | + }, |
| 68 | + }, |
| 69 | + stubSocket = { |
| 70 | + destroy: () => { |
| 71 | + // Simulate Socket.destroy() method when call |
| 72 | + destroyCalled = true; |
| 73 | + }, |
| 74 | + }; |
| 75 | + const returnValue = checkMethodAndHeader(stubRequest, stubSocket); |
| 76 | + expect(returnValue).toBe(undefined); |
| 77 | + expect(destroyCalled).toBe(false); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("#XHeaders", () => { |
| 82 | + it("return if no forward request", () => { |
| 83 | + let returnValue = XHeaders({}, {}, {}); |
| 84 | + expect(returnValue).toBe(undefined); |
| 85 | + }); |
| 86 | + |
| 87 | + it("set the correct x-forwarded-* headers from req.connection", () => { |
| 88 | + let stubRequest = { |
| 89 | + connection: { |
| 90 | + remoteAddress: "192.168.1.2", |
| 91 | + remotePort: "8080", |
| 92 | + }, |
| 93 | + headers: { |
| 94 | + host: "192.168.1.2:8080", |
| 95 | + }, |
| 96 | + }; |
| 97 | + XHeaders(stubRequest, {}, { xfwd: true }); |
| 98 | + expect(stubRequest.headers["x-forwarded-for"]).toBe("192.168.1.2"); |
| 99 | + expect(stubRequest.headers["x-forwarded-port"]).toBe("8080"); |
| 100 | + expect(stubRequest.headers["x-forwarded-proto"]).toBe("ws"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("set the correct x-forwarded-* headers from req.socket", () => { |
| 104 | + let stubRequest = { |
| 105 | + socket: { |
| 106 | + remoteAddress: "192.168.1.3", |
| 107 | + remotePort: "8181", |
| 108 | + }, |
| 109 | + connection: { |
| 110 | + pair: true, |
| 111 | + }, |
| 112 | + headers: { |
| 113 | + host: "192.168.1.3:8181", |
| 114 | + }, |
| 115 | + }; |
| 116 | + XHeaders(stubRequest, {}, { xfwd: true }); |
| 117 | + expect(stubRequest.headers["x-forwarded-for"]).toBe("192.168.1.3"); |
| 118 | + expect(stubRequest.headers["x-forwarded-port"]).toBe("8181"); |
| 119 | + expect(stubRequest.headers["x-forwarded-proto"]).toBe("wss"); |
| 120 | + }); |
| 121 | +}); |
0 commit comments