|
| 1 | +/*! |
| 2 | + * Tests are based on work by Nathan Rajlich: |
| 3 | + * https://github.com/TooTallNate/node-http-proxy-agent/blob/65307ac8fe4e6ce1a2685d21ec4affa4c2a0a30d/test/test.js |
| 4 | + * Copyright (c) 2013 Nathan Rajlich <[email protected]> |
| 5 | + * Released under the MIT license |
| 6 | + * |
| 7 | + * and on work by Rafael Gonzaga (https://github.com/RafaelGSS) |
| 8 | + * |
| 9 | + * https://github.com/nodejs/undici/blob/512cdadc403874571cd5035a6c41debab1165310/test/proxy-agent.js#L370-L418 |
| 10 | + * Released under the MIT license |
| 11 | + */ |
| 12 | +import { Server, createServer } from "node:http"; |
| 13 | +import { type AddressInfo } from "node:net"; |
| 14 | +import { type ProxyServer, createProxy } from "proxy"; |
| 15 | +import { ProxyAgent, fetch as undiciFetch } from "undici"; |
| 16 | +import { Octokit } from "@octokit/core"; |
| 17 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 18 | + |
| 19 | +import { Snapshot, submitSnapshot } from './snapshot.js' |
| 20 | +import { context } from '@actions/github' |
| 21 | +import * as core from '@actions/core' |
| 22 | + |
| 23 | +describe("client proxy", () => { |
| 24 | + let server: Server; |
| 25 | + let proxyServer: ProxyServer; |
| 26 | + let serverUrl: string; |
| 27 | + let proxyUrl: string; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + server = createServer(); |
| 31 | + server.listen(0, () => {}); |
| 32 | + |
| 33 | + proxyServer = createProxy(); |
| 34 | + proxyServer.listen(0, () => {}); |
| 35 | + |
| 36 | + serverUrl = `http://localhost:${(server.address() as AddressInfo).port}`; |
| 37 | + proxyUrl = `http://localhost:${ |
| 38 | + (proxyServer.address() as AddressInfo).port |
| 39 | + }`; |
| 40 | + }); |
| 41 | + |
| 42 | + it("options.request.fetch = customFetch with dispatcher: new ProxyAgent(proxyUrl)", async () => { |
| 43 | + let proxyConnectionEstablished = false; |
| 44 | + |
| 45 | + // requests are not exposed to the proxy server, they are tunneled to |
| 46 | + // Reference: https://github.com/advisories/GHSA-pgw7-wx7w-2w33 |
| 47 | + // Commit: https://github.com/nodejs/undici/commit/df4f7e0e95f5112322a96fd7a666cb28c1d48327#diff-90964a82994d6c63f28161d5410c64406e6abdee4ac0759e83b1abbbe469cda4L35-R39 |
| 48 | + proxyServer.on("connect", () => { |
| 49 | + core.notice(`proxyServer.on("connect")`); |
| 50 | + proxyConnectionEstablished = true; |
| 51 | + }); |
| 52 | + |
| 53 | + server.on("request", (request, response) => { |
| 54 | + core.notice(`request: ${request}`); |
| 55 | + expect(request.method).toEqual("GET"); |
| 56 | + expect(request.url).toEqual("/"); |
| 57 | + expect(request.headers.accept).toBe("application/vnd.github.v3+json"); |
| 58 | + |
| 59 | + response.writeHead(200); |
| 60 | + // return a body containing the expected JSON: {"value": "foo"} |
| 61 | + response.write(JSON.stringify({ value: "foo" })); |
| 62 | + response.end(); |
| 63 | + }); |
| 64 | + |
| 65 | + // const myFetch: typeof undiciFetch = (url, opts) => { |
| 66 | + // return undiciFetch(url, { |
| 67 | + // ...opts, |
| 68 | + // dispatcher: new ProxyAgent({ |
| 69 | + // uri: proxyUrl, |
| 70 | + // keepAliveTimeout: 10, |
| 71 | + // keepAliveMaxTimeout: 10, |
| 72 | + // }), |
| 73 | + // }); |
| 74 | + // }; |
| 75 | + |
| 76 | + // const octokit = new Octokit({ |
| 77 | + // baseUrl: serverUrl, |
| 78 | + // request: { fetch: myFetch }, |
| 79 | + // }); |
| 80 | + |
| 81 | + // await octokit.request("/"); |
| 82 | + const snapshot = new Snapshot( |
| 83 | + {name: 'example-detector', url: 'http://example.com', version: '1.0.0'}, |
| 84 | + context, |
| 85 | + {id: 'job', correlator: 'correlator'}, |
| 86 | + new Date() |
| 87 | + ) |
| 88 | + |
| 89 | + await submitSnapshot(snapshot, context, |
| 90 | + new ProxyAgent({ |
| 91 | + uri: proxyUrl, |
| 92 | + keepAliveTimeout: 10, |
| 93 | + keepAliveMaxTimeout: 10, |
| 94 | + }) |
| 95 | + ); |
| 96 | + |
| 97 | + |
| 98 | + expect(proxyConnectionEstablished).toBeTruthy(); |
| 99 | + expect.assertions(4); |
| 100 | + }); |
| 101 | + |
| 102 | + afterEach(() => { |
| 103 | + server.close(); |
| 104 | + proxyServer.close(); |
| 105 | + }); |
| 106 | +}); |
0 commit comments