| 
 | 1 | +const { expect } = require("chai");  | 
 | 2 | +const proxyHandler = require("./proxy");  | 
 | 3 | +const httpMock = require("node-mocks-http");  | 
 | 4 | + | 
 | 5 | +describe("proxyHandler", () => {  | 
 | 6 | +    it("should not proxy request to target when url from forward is not present", () => {  | 
 | 7 | +        const config = [  | 
 | 8 | +            {  | 
 | 9 | +                forward: ["sample", "proxy-this"],  | 
 | 10 | +                target: "www.github.com",  | 
 | 11 | +            },  | 
 | 12 | +        ];  | 
 | 13 | +        const req = httpMock.createRequest({  | 
 | 14 | +            method: "GET",  | 
 | 15 | +            url: "https://localhost:4200/proxyTest",  | 
 | 16 | +            pipe: () => {},  | 
 | 17 | +        });  | 
 | 18 | + | 
 | 19 | +        expect(proxyHandler(req, {}, config, false)).to.equal(false);  | 
 | 20 | +    });  | 
 | 21 | + | 
 | 22 | +    it("should proxy request to target when url from forward is present", () => {  | 
 | 23 | +        const config = [  | 
 | 24 | +            {  | 
 | 25 | +                forward: ["sample", "proxy-this"],  | 
 | 26 | +                target: "www.github.com",  | 
 | 27 | +            },  | 
 | 28 | +        ];  | 
 | 29 | +        const req = httpMock.createRequest({  | 
 | 30 | +            method: "GET",  | 
 | 31 | +            url: "https://localhost:4200/proxyTest/proxy-this",  | 
 | 32 | +            pipe: () => {},  | 
 | 33 | +        });  | 
 | 34 | + | 
 | 35 | +        expect(proxyHandler(req, {}, config, false)).to.equal(true);  | 
 | 36 | +    });  | 
 | 37 | + | 
 | 38 | +    it("should throw error when no config present", () => {  | 
 | 39 | +        config = [];  | 
 | 40 | +        const req = httpMock.createRequest({  | 
 | 41 | +            method: "GET",  | 
 | 42 | +            url: "https://localhost:4200/proxyTest/proxy-this",  | 
 | 43 | +            pipe: () => {},  | 
 | 44 | +        });  | 
 | 45 | + | 
 | 46 | +        expect(() => proxyHandler(req, {}, null, false)).to.throw(  | 
 | 47 | +            "no proxyConfig present, please configure in your config file"  | 
 | 48 | +        );  | 
 | 49 | +    });  | 
 | 50 | + | 
 | 51 | +    it("should throw error when forward is missing", () => {  | 
 | 52 | +        const config = [  | 
 | 53 | +            {  | 
 | 54 | +                forward: [],  | 
 | 55 | +                target: "www.github.com",  | 
 | 56 | +            },  | 
 | 57 | +        ];  | 
 | 58 | +        const req = httpMock.createRequest({  | 
 | 59 | +            method: "GET",  | 
 | 60 | +            url: "https://localhost:4200/proxyTest/proxy-this",  | 
 | 61 | +            pipe: () => {},  | 
 | 62 | +        });  | 
 | 63 | + | 
 | 64 | +        expect(() => proxyHandler(req, {}, config, false)).to.throw(  | 
 | 65 | +            "forward is missing or empty in your proxyConfig"  | 
 | 66 | +        );  | 
 | 67 | +    });  | 
 | 68 | +    it("should throw error when target is missing", () => {  | 
 | 69 | +        const config = [  | 
 | 70 | +            {  | 
 | 71 | +                forward: ["sample", "proxy-this"],  | 
 | 72 | +                target: "",  | 
 | 73 | +            },  | 
 | 74 | +        ];  | 
 | 75 | +        const req = httpMock.createRequest({  | 
 | 76 | +            method: "GET",  | 
 | 77 | +            url: "https://localhost:4200/proxyTest/proxy-this",  | 
 | 78 | +            pipe: () => {},  | 
 | 79 | +        });  | 
 | 80 | + | 
 | 81 | +        expect(() => proxyHandler(req, {}, config, false)).to.throw(  | 
 | 82 | +            "target is missing or empty in your proxyConfig"  | 
 | 83 | +        );  | 
 | 84 | +    });  | 
 | 85 | +});  | 
0 commit comments