2020-01-17 04:49:18 +00:00
|
|
|
import * as http from 'http'
|
|
|
|
import * as httpm from '../_out';
|
2020-01-09 23:32:59 +00:00
|
|
|
import * as pm from '../_out/proxy';
|
2020-01-17 04:49:18 +00:00
|
|
|
import * as proxy from 'proxy'
|
2020-01-09 23:32:59 +00:00
|
|
|
import * as url from 'url';
|
|
|
|
|
2020-01-17 04:49:18 +00:00
|
|
|
let _proxyConnects: string[]
|
|
|
|
let _proxyServer: http.Server
|
|
|
|
let _proxyUrl = 'http://127.0.0.1:8080'
|
|
|
|
|
2020-01-09 23:32:59 +00:00
|
|
|
describe('proxy', () => {
|
2020-01-17 04:49:18 +00:00
|
|
|
beforeAll(async () => {
|
|
|
|
// Start proxy server
|
|
|
|
_proxyServer = proxy()
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
const port = Number(_proxyUrl.split(':')[2])
|
|
|
|
_proxyServer.listen(port, () => resolve())
|
|
|
|
})
|
|
|
|
_proxyServer.on('connect', (req) => {
|
|
|
|
_proxyConnects.push(req.url)
|
|
|
|
});
|
|
|
|
})
|
2020-01-09 23:32:59 +00:00
|
|
|
|
2020-01-17 04:49:18 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
_proxyConnects = []
|
|
|
|
_clearVars()
|
2020-01-09 23:32:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-01-17 04:49:18 +00:00
|
|
|
})
|
2020-01-09 23:32:59 +00:00
|
|
|
|
2020-01-17 04:49:18 +00:00
|
|
|
afterAll(async() => {
|
|
|
|
_clearVars()
|
|
|
|
|
|
|
|
// Stop proxy server
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
_proxyServer.once('close', () => resolve())
|
|
|
|
_proxyServer.close()
|
|
|
|
})
|
2020-01-09 23:32:59 +00:00
|
|
|
})
|
2020-01-17 04:49:18 +00:00
|
|
|
|
2020-01-09 23:32:59 +00:00
|
|
|
it('does not return proxyUrl if variables not set', () => {
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
|
|
|
|
expect(proxyUrl).toBeUndefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns proxyUrl if https_proxy set for https url', () => {
|
|
|
|
process.env["https_proxy"] = "https://myproxysvr";
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
|
|
|
|
expect(proxyUrl).toBeDefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not return proxyUrl if http_proxy set for https url', () => {
|
|
|
|
process.env["http_proxy"] = "https://myproxysvr";
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
|
|
|
|
expect(proxyUrl).toBeUndefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns proxyUrl if http_proxy set for http url', () => {
|
|
|
|
process.env["http_proxy"] = "http://myproxysvr";
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('http://github.com'));
|
|
|
|
expect(proxyUrl).toBeDefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not return proxyUrl if only host as no_proxy list', () => {
|
|
|
|
process.env["https_proxy"] = "https://myproxysvr";
|
|
|
|
process.env["no_proxy"] = "myserver"
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver'));
|
|
|
|
expect(proxyUrl).toBeUndefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not return proxyUrl if host in no_proxy list', () => {
|
|
|
|
process.env["https_proxy"] = "https://myproxysvr";
|
|
|
|
process.env["no_proxy"] = "otherserver,myserver,anotherserver:8080"
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver'));
|
|
|
|
expect(proxyUrl).toBeUndefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not return proxyUrl if host in no_proxy list with spaces', () => {
|
|
|
|
process.env["https_proxy"] = "https://myproxysvr";
|
|
|
|
process.env["no_proxy"] = "otherserver, myserver ,anotherserver:8080"
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver'));
|
|
|
|
expect(proxyUrl).toBeUndefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not return proxyUrl if host in no_proxy list with ports', () => {
|
|
|
|
process.env["https_proxy"] = "https://myproxysvr";
|
|
|
|
process.env["no_proxy"] = "otherserver, myserver:8080 ,anotherserver"
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver:8080'));
|
|
|
|
expect(proxyUrl).toBeUndefined();
|
2020-01-17 04:49:18 +00:00
|
|
|
})
|
2020-01-09 23:32:59 +00:00
|
|
|
|
|
|
|
it('returns proxyUrl if https_proxy set and not in no_proxy list', () => {
|
|
|
|
process.env["https_proxy"] = "https://myproxysvr";
|
|
|
|
process.env["no_proxy"] = "otherserver, myserver ,anotherserver:8080"
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
|
|
|
|
expect(proxyUrl).toBeDefined();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns proxyUrl if https_proxy set empty no_proxy set', () => {
|
|
|
|
process.env["https_proxy"] = "https://myproxysvr";
|
|
|
|
process.env["no_proxy"] = ""
|
|
|
|
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
|
|
|
|
expect(proxyUrl).toBeDefined();
|
2020-01-17 04:49:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does basic http get request through proxy', async () => {
|
|
|
|
process.env['http_proxy'] = _proxyUrl
|
|
|
|
const httpClient = new httpm.HttpClient();
|
|
|
|
let res: httpm.HttpClientResponse = await httpClient.get('http://httpbin.org/get');
|
|
|
|
expect(res.message.statusCode).toBe(200);
|
|
|
|
let body: string = await res.readBody();
|
|
|
|
let obj: any = JSON.parse(body);
|
|
|
|
expect(obj.url).toBe("https://httpbin.org/get");
|
|
|
|
expect(_proxyConnects).toEqual(['httpbin.org:80'])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does basic http get request when bypass proxy', async () => {
|
|
|
|
process.env['http_proxy'] = _proxyUrl
|
|
|
|
process.env['no_proxy'] = 'httpbin.org'
|
|
|
|
const httpClient = new httpm.HttpClient();
|
|
|
|
let res: httpm.HttpClientResponse = await httpClient.get('http://httpbin.org/get');
|
|
|
|
expect(res.message.statusCode).toBe(200);
|
|
|
|
let body: string = await res.readBody();
|
|
|
|
let obj: any = JSON.parse(body);
|
|
|
|
expect(obj.url).toBe("https://httpbin.org/get");
|
|
|
|
expect(_proxyConnects).toHaveLength(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does basic https get request through proxy', async () => {
|
|
|
|
process.env['https_proxy'] = _proxyUrl
|
|
|
|
const httpClient = new httpm.HttpClient();
|
|
|
|
let res: httpm.HttpClientResponse = await httpClient.get('https://httpbin.org/get');
|
|
|
|
expect(res.message.statusCode).toBe(200);
|
|
|
|
let body: string = await res.readBody();
|
|
|
|
let obj: any = JSON.parse(body);
|
|
|
|
expect(obj.url).toBe("https://httpbin.org/get");
|
|
|
|
expect(_proxyConnects).toEqual(['httpbin.org:443'])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does basic https get request when bypass proxy', async () => {
|
|
|
|
process.env['https_proxy'] = _proxyUrl
|
|
|
|
process.env['no_proxy'] = 'httpbin.org'
|
|
|
|
const httpClient = new httpm.HttpClient();
|
|
|
|
let res: httpm.HttpClientResponse = await httpClient.get('https://httpbin.org/get');
|
|
|
|
expect(res.message.statusCode).toBe(200);
|
|
|
|
let body: string = await res.readBody();
|
|
|
|
let obj: any = JSON.parse(body);
|
|
|
|
expect(obj.url).toBe("https://httpbin.org/get");
|
|
|
|
expect(_proxyConnects).toHaveLength(0)
|
|
|
|
})
|
2020-01-09 23:32:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function _clearVars() {
|
|
|
|
delete process.env.http_proxy;
|
|
|
|
delete process.env.HTTP_PROXY;
|
|
|
|
delete process.env.https_proxy;
|
|
|
|
delete process.env.HTTPS_PROXY;
|
|
|
|
delete process.env.no_proxy;
|
|
|
|
delete process.env.NO_PROXY;
|
|
|
|
}
|