Skip to content

Commit

Permalink
Skip failings https tests only on older Node versions
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 28, 2023
1 parent 6ac21fc commit 8f60cdb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 56 deletions.
23 changes: 17 additions & 6 deletions packages/https-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ import parseProxyResponse from './parse-proxy-response';

const debug = createDebug('https-proxy-agent');

export type HttpsProxyAgentOptions = Omit<
net.TcpNetConnectOpts & tls.ConnectionOptions,
'host' | 'port'
> & {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;

type ConnectOptsMap = {
http: Omit<net.TcpNetConnectOpts, 'host' | 'port'>;
https: Omit<tls.ConnectionOptions, 'host' | 'port'>;
}

type ConnectOpts<T> = {
[P in keyof ConnectOptsMap]: Protocol<T> extends P
? ConnectOptsMap[P]
: never;
}[keyof ConnectOptsMap];

export type HttpsProxyAgentOptions<T> = ConnectOpts<T> & {
headers?: OutgoingHttpHeaders;
};

Expand All @@ -28,7 +39,7 @@ export type HttpsProxyAgentOptions = Omit<
* `https:` requests have their socket connection upgraded to TLS once
* the connection to the proxy server has been established.
*/
export class HttpsProxyAgent extends Agent {
export class HttpsProxyAgent<Uri extends string> extends Agent {
readonly proxy: URL;
proxyHeaders: OutgoingHttpHeaders;
connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;
Expand All @@ -37,7 +48,7 @@ export class HttpsProxyAgent extends Agent {
return isHTTPS(this.proxy.protocol);
}

constructor(proxy: string | URL, opts?: HttpsProxyAgentOptions) {
constructor(proxy: Uri | URL, opts?: HttpsProxyAgentOptions<Uri>) {
super();
this.proxy = typeof proxy === 'string' ? new URL(proxy) : proxy;
this.proxyHeaders = opts?.headers ?? {};
Expand Down
117 changes: 67 additions & 50 deletions packages/https-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const sslOptions = {
cert: fs.readFileSync(`${__dirname}/ssl-cert-snakeoil.pem`),
};

const testIf = (condition: boolean, ...args: Parameters<typeof test>) =>
condition ? test(...args) : test.skip(...args);

const req = (
url: string,
opts: https.RequestOptions
Expand Down Expand Up @@ -243,57 +246,71 @@ describe('HttpsProxyAgent', () => {
});

describe('"https" module', () => {
it.skip('should work over an HTTP proxy', async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const proxy = `http://localhost:${proxyPort}`;
const agent = new HttpsProxyAgent(proxy);

const res = await req(`https://localhost:${sslServerPort}`, {
rejectUnauthorized: false,
agent,
});
const body = await json(res);
assert.equal(`localhost:${sslServerPort}`, body.host);
});

it.skip('should work over an HTTPS proxy', async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const proxy = `https://localhost:${sslProxyPort}`;
const agent = new HttpsProxyAgent(proxy, {
rejectUnauthorized: false,
});

const res = await req(`https://localhost:${sslServerPort}`, {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert.equal(`localhost:${sslServerPort}`, body.host);
});

it.skip('should not send a port number for the default port', async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const nodeVersion = parseFloat(process.versions.node);

testIf(
nodeVersion >= 18,
'should work over an HTTP proxy',
async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const proxy = `http://localhost:${proxyPort}`;
const agent = new HttpsProxyAgent(proxy);

const res = await req(`https://localhost:${sslServerPort}`, {
rejectUnauthorized: false,
agent,
});
const body = await json(res);
assert.equal(`localhost:${sslServerPort}`, body.host);
}
);

testIf(
nodeVersion >= 18,
'should work over an HTTPS proxy',
async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const proxy = `https://localhost:${sslProxyPort}`;
const agent = new HttpsProxyAgent(proxy, {
rejectUnauthorized: false,
});

const res = await req(`https://localhost:${sslServerPort}`, {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert.equal(`localhost:${sslServerPort}`, body.host);
}
);

const agent = new HttpsProxyAgent(
`https://localhost:${sslProxyPort}`,
{ rejectUnauthorized: false }
);
agent.defaultPort = sslServerPort;
testIf(
nodeVersion >= 18,
'should not send a port number for the default port',
async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const res = await req(`https://localhost:${sslServerPort}`, {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert.equal('localhost', body.host);
});
const agent = new HttpsProxyAgent(
`https://localhost:${sslProxyPort}`,
{ rejectUnauthorized: false }
);
agent.defaultPort = sslServerPort;

const res = await req(`https://localhost:${sslServerPort}`, {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert.equal('localhost', body.host);
}
);
});
});

0 comments on commit 8f60cdb

Please sign in to comment.