| 
 | 1 | +import { globalAgent } from "node:https";  | 
1 | 2 | import HttpURLConnectionClient from "../httpClient/httpURLConnectionClient";  | 
 | 3 | +import HttpClientException from "../httpClient/httpClientException";  | 
2 | 4 | 
 
  | 
3 | 5 | describe("HttpURLConnectionClient", () => {  | 
4 |  | -    let client: HttpURLConnectionClient;  | 
 | 6 | +  let client: HttpURLConnectionClient;  | 
5 | 7 | 
 
  | 
6 |  | -    beforeEach(() => {  | 
7 |  | -        client = new HttpURLConnectionClient();  | 
 | 8 | +  beforeEach(() => {  | 
 | 9 | +    client = new HttpURLConnectionClient();  | 
 | 10 | +  });  | 
 | 11 | + | 
 | 12 | +  describe("verifyLocation", () => {  | 
 | 13 | +    test.each([  | 
 | 14 | +      "https://example.adyen.com/path",  | 
 | 15 | +      "https://sub.adyen.com",  | 
 | 16 | +      "http://another.adyen.com/a/b/c?q=1",  | 
 | 17 | +      "https://checkout-test.adyen.com",  | 
 | 18 | +    ])("should return true for valid adyen.com domain: %s", (location) => {  | 
 | 19 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 20 | +      // @ts-ignore - testing a private method  | 
 | 21 | +      expect(client.verifyLocation(location)).toBe(true);  | 
 | 22 | +    });  | 
 | 23 | + | 
 | 24 | +    test.each(["https://example.ADYEN.com/path", "HTTPS://sub.adyen.COM"])(  | 
 | 25 | +      "should be case-insensitive for valid adyen.com domain: %s",  | 
 | 26 | +      (location) => {  | 
 | 27 | +        // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 28 | +        // @ts-ignore - testing a private method  | 
 | 29 | +        expect(client.verifyLocation(location)).toBe(true);  | 
 | 30 | +      }  | 
 | 31 | +    );  | 
 | 32 | + | 
 | 33 | +    test.each([  | 
 | 34 | +      "https://adyen.com.evil.com/path",  | 
 | 35 | +      "https://evil-adyen.com",  | 
 | 36 | +      "http://adyen.co",  | 
 | 37 | +      "https://www.google.com",  | 
 | 38 | +      "https://adyen.com-scam.com",  | 
 | 39 | +    ])("should return false for invalid domain: %s", (location) => {  | 
 | 40 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 41 | +      // @ts-ignore - testing a private method  | 
 | 42 | +      expect(client.verifyLocation(location)).toBe(false);  | 
 | 43 | +    });  | 
 | 44 | + | 
 | 45 | +    test.each([  | 
 | 46 | +      "https://adyen.com.another.domain/path",  | 
 | 47 | +      "https://myadyen.com.org",  | 
 | 48 | +    ])(  | 
 | 49 | +      "should return false for domains that contain but do not end with adyen.com: %s",  | 
 | 50 | +      (location) => {  | 
 | 51 | +        // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 52 | +        // @ts-ignore - testing a private method  | 
 | 53 | +        expect(client.verifyLocation(location)).toBe(false);  | 
 | 54 | +      }  | 
 | 55 | +    );  | 
 | 56 | + | 
 | 57 | +    test.each(["not a url", "adyen.com", "//adyen.com/path"])(  | 
 | 58 | +      "should return false for malformed URLs: %s",  | 
 | 59 | +      (location) => {  | 
 | 60 | +        // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 61 | +        // @ts-ignore - testing a private method  | 
 | 62 | +        expect(client.verifyLocation(location)).toBe(false);  | 
 | 63 | +      }  | 
 | 64 | +    );  | 
 | 65 | +  });  | 
 | 66 | + | 
 | 67 | +  describe("selectAgent", () => {  | 
 | 68 | +    it("returns globalAgent if terminalCertificatePath is undefined", () => {  | 
 | 69 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 70 | +      // @ts-ignore - testing a private method  | 
 | 71 | +      expect(client.selectAgent()).toBe(globalAgent);  | 
8 | 72 |     });  | 
9 | 73 | 
 
  | 
10 |  | -    describe("verifyLocation", () => {  | 
11 |  | -        test.each([  | 
12 |  | -            "https://example.adyen.com/path",  | 
13 |  | -            "https://sub.adyen.com",  | 
14 |  | -            "http://another.adyen.com/a/b/c?q=1",  | 
15 |  | -            "https://checkout-test.adyen.com",  | 
16 |  | -        ])("should return true for valid adyen.com domain: %s", (location) => {  | 
17 |  | -            // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
18 |  | -            // @ts-ignore - testing a private method  | 
19 |  | -            expect(client.verifyLocation(location)).toBe(true);  | 
20 |  | -        });  | 
21 |  | - | 
22 |  | -        test.each([  | 
23 |  | -            "https://example.ADYEN.com/path",  | 
24 |  | -            "HTTPS://sub.adyen.COM",  | 
25 |  | -        ])("should be case-insensitive for valid adyen.com domain: %s", (location) => {  | 
26 |  | -            // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
27 |  | -            // @ts-ignore - testing a private method  | 
28 |  | -            expect(client.verifyLocation(location)).toBe(true);  | 
29 |  | -        });  | 
30 |  | - | 
31 |  | -        test.each([  | 
32 |  | -            "https://adyen.com.evil.com/path",  | 
33 |  | -            "https://evil-adyen.com",  | 
34 |  | -            "http://adyen.co",  | 
35 |  | -            "https://www.google.com",  | 
36 |  | -            "https://adyen.com-scam.com",  | 
37 |  | -        ])("should return false for invalid domain: %s", (location) => {  | 
38 |  | -            // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
39 |  | -            // @ts-ignore - testing a private method  | 
40 |  | -            expect(client.verifyLocation(location)).toBe(false);  | 
41 |  | -        });  | 
42 |  | - | 
43 |  | -        test.each([  | 
44 |  | -            "https://adyen.com.another.domain/path",  | 
45 |  | -            "https://myadyen.com.org",  | 
46 |  | -        ])("should return false for domains that contain but do not end with adyen.com: %s", (location) => {  | 
47 |  | -            // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
48 |  | -            // @ts-ignore - testing a private method  | 
49 |  | -            expect(client.verifyLocation(location)).toBe(false);  | 
50 |  | -        });  | 
51 |  | - | 
52 |  | -        test.each([  | 
53 |  | -            "not a url",  | 
54 |  | -            "adyen.com",  | 
55 |  | -            "//adyen.com/path",  | 
56 |  | -        ])("should return false for malformed URLs: %s", (location) => {  | 
57 |  | -            // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
58 |  | -            // @ts-ignore - testing a private method  | 
59 |  | -            expect(client.verifyLocation(location)).toBe(false);  | 
60 |  | -        });  | 
 | 74 | +    it("returns new Agent if terminalCertificatePath is 'unencrypted'", () => {  | 
 | 75 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 76 | +      // @ts-ignore - testing a private method  | 
 | 77 | +      const agent = client.selectAgent("unencrypted");  | 
 | 78 | +      expect(agent.options.keepAlive).toBe(true);  | 
 | 79 | +      expect(agent.options.scheduling).toBe("lifo");  | 
 | 80 | +      expect(agent.options.timeout).toBe(5000);  | 
 | 81 | +      expect(agent.options.rejectUnauthorized).toBe(false);  | 
 | 82 | + | 
 | 83 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 84 | +      // @ts-ignore - testing a private method  | 
 | 85 | +      expect(Object.keys(client.agents).length).toBe(1);  | 
 | 86 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 87 | +      // @ts-ignore - testing a private method  | 
 | 88 | +      expect(Object.keys(client.agents).at(0)).toBe("unencrypted");  | 
 | 89 | +    });  | 
 | 90 | + | 
 | 91 | +    it("returns new Agent if terminalCertificatePath is 'path/to/this/file'", () => {  | 
 | 92 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 93 | +      // @ts-ignore - testing a private method  | 
 | 94 | +      const agent = client.selectAgent(__filename);  | 
 | 95 | +      expect(agent.options.keepAlive).toBe(true);  | 
 | 96 | +      expect(agent.options.scheduling).toBe("lifo");  | 
 | 97 | +      expect(agent.options.timeout).toBe(5000);  | 
 | 98 | +      expect(agent.options.ca).toBeDefined();  | 
 | 99 | + | 
 | 100 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 101 | +      // @ts-ignore - testing a private method  | 
 | 102 | +      expect(Object.keys(client.agents).length).toBe(1);  | 
 | 103 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 104 | +      // @ts-ignore - testing a private method  | 
 | 105 | +      expect(Object.keys(client.agents).at(0)).toBe(__filename);  | 
 | 106 | +    });  | 
 | 107 | + | 
 | 108 | +    it("returns already existing Agent if terminalCertificatePath is asked twice", () => {  | 
 | 109 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 110 | +      // @ts-ignore - testing a private method  | 
 | 111 | +      const agent = client.selectAgent(__filename); // create and store an agent  | 
 | 112 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 113 | +      // @ts-ignore - testing a private method  | 
 | 114 | +      const agent2 = client.selectAgent(__filename); // should return the one already in memory  | 
 | 115 | + | 
 | 116 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 117 | +      // @ts-ignore - testing a private method  | 
 | 118 | +      expect(Object.keys(client.agents).length).toBe(1);  | 
 | 119 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 120 | +      // @ts-ignore - testing a private method  | 
 | 121 | +      expect(Object.keys(client.agents).at(0)).toBe(__filename);  | 
 | 122 | + | 
 | 123 | +      expect(agent).toBe(agent2);  | 
 | 124 | +    });  | 
 | 125 | + | 
 | 126 | +    it("throw an error if terminalCertificatePath is not a valid path", () => {  | 
 | 127 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 128 | +      // @ts-ignore - testing a private method  | 
 | 129 | +      expect(() => client.selectAgent("/invalid/path")).toThrow(  | 
 | 130 | +        HttpClientException  | 
 | 131 | +      );  | 
 | 132 | + | 
 | 133 | +      // eslint-disable-next-line @typescript-eslint/ban-ts-comment  | 
 | 134 | +      // @ts-ignore - testing a private method  | 
 | 135 | +      expect(Object.keys(client.agents).length).toBe(0);  | 
61 | 136 |     });  | 
 | 137 | +  });  | 
62 | 138 | });  | 
0 commit comments