Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix request should failed when invaild proxy provided #9

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface XHROptions {
data?: string;
strictSSL?: boolean;
followRedirects?: number;
agent?: HttpProxyAgent | HttpsProxyAgent;
}

export interface XHRResponse {
Expand All @@ -29,6 +30,10 @@ export interface XHRConfigure {
(proxyUrl: string, strictSSL: boolean): void;
}

export type HttpProxyAgent = import('http-proxy-agent').HttpProxyAgent;

export type HttpsProxyAgent = import('https-proxy-agent').HttpsProxyAgent;

export type Headers = { [header: string]: string | string[] | undefined };

export declare const configure: XHRConfigure;
Expand Down
4 changes: 3 additions & 1 deletion src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function request(options: XHROptions): Promise<RequestResult> {

let opts: https.RequestOptions = {
hostname: endpoint.hostname,
agent: options.agent ? options.agent : false,
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
path: endpoint.path,
method: options.type || 'GET',
Expand Down Expand Up @@ -248,7 +249,8 @@ function getProxyAgent(rawRequestURL: string, options: ProxyOptions = {}): any {
host: proxyEndpoint.hostname,
port: Number(proxyEndpoint.port),
auth: proxyEndpoint.auth,
rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true,
protocol: proxyEndpoint.protocol
};

return requestURL.protocol === 'http:' ? createHttpProxyAgent(opts) : createHttpsProxyAgent(opts);
Expand Down
30 changes: 26 additions & 4 deletions src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ test('proxy http to http', async t => {
proxy.close();
});

test('invalid proxy http to http', async t => {
const server = await createServer();
const proxy = await createProxy();
server.on('request', (req, res) => res.end('ok'));

const invalidProxyAddress = {
address: '192.168.29.165',
port: '808'
}

configure(`http://${invalidProxyAddress.address}:${invalidProxyAddress.port}`, false);

const serverAddress = server.address() as AddressInfo;

await xhr({ url: `http://${serverAddress.address}:${serverAddress.port}` }).catch((response) => {
t.deepEqual(response.responseText, 'Unable to connect to http://' + `${serverAddress.address}` + ':' + `${serverAddress.port}` + ' through a proxy . Error: connect ECONNREFUSED 192.168.29.165:808');
t.is(response.status, 404);
});
server.close();
proxy.close();
});


test('proxy https to https', async t => {
const server = await createSecureServer();
Expand All @@ -72,7 +94,7 @@ test('proxy https to https', async t => {

const proxyAddress = proxy.address() as AddressInfo;

configure(`http://${proxyAddress.address}:${proxyAddress.port}`, false);
configure(`https://${proxyAddress.address}:${proxyAddress.port}`, false);

const serverAddress = server.address() as AddressInfo;

Expand All @@ -88,13 +110,13 @@ test('proxy https to https', async t => {
test('relative redirect', async t => {
const server = await createServer();

server.on('request', (req:IncomingMessage, res: ServerResponse) => {
if(req.url.includes('/foo')) {
server.on('request', (req: IncomingMessage, res: ServerResponse) => {
if (req.url.includes('/foo')) {
res.setHeader('Location', '/bar');
res.statusCode = 301;
res.end();
}
if(req.url.includes('/bar')){
if (req.url.includes('/bar')) {
res.end('Bar');
}
});
Expand Down