Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 29, 2023
1 parent 9ff3f38 commit 22431fd
Showing 1 changed file with 55 additions and 96 deletions.
151 changes: 55 additions & 96 deletions packages/proxy-agent/test/test.ts
Expand Up @@ -26,6 +26,7 @@ describe('ProxyAgent', () => {
let httpProxyServerUrl: URL;
let httpsProxyServer: ProxyServer;
let httpsProxyServerUrl: URL;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let socksServer: any;
let socksPort: number;

Expand Down Expand Up @@ -72,38 +73,41 @@ describe('ProxyAgent', () => {
httpsProxyServer.close();
});

describe('"http" module', () => {
beforeAll(() => {
delete process.env.HTTP_PROXY;
delete process.env.NO_PROXY;
});
beforeEach(() => {
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
delete process.env.NO_PROXY;
});

it('should work over "http" proxy', async () => {
describe('"http" module', () => {
it('should work with no proxy from env', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});

// `NO_PROXY` should take precedence
process.env.NO_PROXY = '*';
process.env.HTTP_PROXY = httpProxyServerUrl.href;
const agent = new ProxyAgent();

const res = await req(new URL('/test', httpServerUrl), { agent });
const body = await json(res);
assert.equal(httpServerUrl.host, body.host);
assert('via' in body);
assert(!('via' in body));
});

it('should work with no proxy from env', async () => {
it('should work over "http" proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});

process.env.NO_PROXY = '*';
process.env.HTTP_PROXY = httpProxyServerUrl.href;
const agent = new ProxyAgent();

const res = await req(new URL('/test', httpServerUrl), { agent });
const body = await json(res);
assert.equal(httpServerUrl.host, body.host);
assert(!('via' in body));
assert('via' in body);
});

it('should work over "https" proxy', async () => {
Expand Down Expand Up @@ -133,90 +137,45 @@ describe('ProxyAgent', () => {
});
});

//describe('"https" module', () => {
// describe('over "http" proxy', () => {
// it('should work', async () => {
// httpsServer.once('request', function (req, res) {
// res.end(JSON.stringify(req.headers));
// });

// const uri = 'http://localhost:' + proxyPort;
// const agent = new ProxyAgent(uri);

// const opts = url.parse('https://localhost:' + httpsPort + '/test');
// opts.agent = agent;
// opts.rejectUnauthorized = false;

// const req = https.get(opts, function (res) {
// toBuffer(res, function (err, buf) {
// if (err) return done(err);
// const data = JSON.parse(buf.toString('utf8'));
// assert.equal('localhost:' + httpsPort, data.host);
// done();
// });
// });
// req.once('error', done);
// });
// });

// describe('over "https" proxy', () => {
// it('should work', async () => {
// let gotReq = false;
// httpsServer.once('request', function (req, res) {
// gotReq = true;
// res.end(JSON.stringify(req.headers));
// });

// const agent = new ProxyAgent({
// protocol: 'https:',
// host: 'localhost',
// port: proxyHttpsPort,
// rejectUnauthorized: false
// });

// const opts = url.parse('https://localhost:' + httpsPort + '/test');
// opts.agent = agent;
// opts.rejectUnauthorized = false;

// const req = https.get(opts, function (res) {
// toBuffer(res, function (err, buf) {
// if (err) return done(err);
// const data = JSON.parse(buf.toString('utf8'));
// assert.equal('localhost:' + httpsPort, data.host);
// assert(gotReq);
// done();
// });
// });
// req.once('error', done);
// });
// });

// describe('over "socks" proxy', () => {
// it('should work', async () => {
// let gotReq = false;
// httpsServer.once('request', function (req, res) {
// gotReq = true;
// res.end(JSON.stringify(req.headers));
// });

// const uri = 'socks://localhost:' + socksPort;
// const agent = new ProxyAgent(uri);

// const opts = url.parse('https://localhost:' + httpsPort + '/test');
// opts.agent = agent;
// opts.rejectUnauthorized = false;

// const req = https.get(opts, function (res) {
// toBuffer(res, function (err, buf) {
// if (err) return done(err);
// const data = JSON.parse(buf.toString('utf8'));
// assert.equal('localhost:' + httpsPort, data.host);
// assert(gotReq);
// done();
// });
// });
// req.once('error', done);
// });
// });
//});
describe('"https" module', () => {
it('should work over "https" proxy', async () => {
let gotReq = false;
httpsServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
gotReq = true;
});

process.env.HTTPS_PROXY = httpsProxyServerUrl.href;
const agent = new ProxyAgent({ rejectUnauthorized: false });

const res = await req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert(gotReq);
assert.equal(httpsServerUrl.host, body.host);
});

describe('over "socks" proxy', () => {
it('should work', async () => {
let gotReq = false;
httpsServer.once('request', function (req, res) {
gotReq = true;
res.end(JSON.stringify(req.headers));
});

process.env.HTTP_PROXY = `socks://localhost:${socksPort}`;
const agent = new ProxyAgent();

const res = await req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert(gotReq);
assert.equal(httpsServerUrl.host, body.host);
});
});
});
});

0 comments on commit 22431fd

Please sign in to comment.