Skip to content

Commit

Permalink
fix: test https proxy-authorization header
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 18, 2019
1 parent 0d400e9 commit a703be1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/classes/HttpsProxyAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ class HttpsProxyAgent extends Agent {

connectMessage += 'CONNECT ' + configuration.host + ':' + configuration.port + ' HTTP/1.1\r\n';
connectMessage += 'Host: ' + configuration.host + ':' + configuration.port + '\r\n';

if (configuration.proxy.authorization) {
connectMessage += 'Proxy-Authorization: Basic ' + Buffer.from(configuration.proxy.authorization).toString('base64') + '\r\n';
}

connectMessage += '\r\n';

socket.write(connectMessage);
Expand Down
77 changes: 46 additions & 31 deletions test/global-agent/factories/createGlobalProxyAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ import test, {
} from 'ava';
import createGlobalProxyAgent from '../../../src/factories/createGlobalProxyAgent';

const anyproxyDefaultRules = {
beforeDealHttpsRequest: async () => {
return true;
},
beforeSendRequest: () => {
return {
response: {
body: 'OK',
header: {
'content-type': 'text/plain',
},
statusCode: 200,
},
};
},
};

const defaultHttpAgent = http.globalAgent;
const defaultHttpsAgent = https.globalAgent;

Expand Down Expand Up @@ -87,31 +104,14 @@ const createHttpResponseResolver = (resolve) => {
};
};

const createProxyServer = async (maybeBeforeSendRequest) => {
const createProxyServer = async (anyproxyRules) => {
const port = await getNextPort();

let beforeSendRequest = () => {
return {
response: {
body: 'OK',
header: {
'content-type': 'text/plain',
},
statusCode: 200,
},
};
};

if (maybeBeforeSendRequest) {
beforeSendRequest = maybeBeforeSendRequest;
}

const localProxyServer = await new Promise((resolve) => {
const proxyServer = new ProxyServer({
forceProxyHttps: true,
port,
rule: {
beforeSendRequest,
...anyproxyRules ? anyproxyRules : anyproxyDefaultRules,
},
});

Expand Down Expand Up @@ -173,19 +173,11 @@ test('proxies HTTP request', async (t) => {
test('proxies HTTP request with proxy-authorization header', async (t) => {
const globalProxyAgent = createGlobalProxyAgent();

const beforeSendRequest = sinon.stub().callsFake(() => {
return {
response: {
body: 'OK',
header: {
'content-type': 'text/plain',
},
statusCode: 200,
},
};
});
const beforeSendRequest = sinon.stub().callsFake(anyproxyDefaultRules.beforeSendRequest);

const proxyServer = await createProxyServer(beforeSendRequest);
const proxyServer = await createProxyServer({
beforeSendRequest,
});

globalProxyAgent.HTTP_PROXY = 'http://foo@127.0.0.1:' + proxyServer.port;

Expand All @@ -212,6 +204,29 @@ test('proxies HTTPS request', async (t) => {
t.assert(response.body === 'OK');
});

test('proxies HTTPS request with proxy-authorization header', async (t) => {
const globalProxyAgent = createGlobalProxyAgent();

const beforeDealHttpsRequest = sinon.stub().callsFake(async () => {
return true;
});

const proxyServer = await createProxyServer({
beforeDealHttpsRequest,
beforeSendRequest: anyproxyDefaultRules.beforeSendRequest,
});

globalProxyAgent.HTTP_PROXY = 'http://foo@127.0.0.1:' + proxyServer.port;

const response = await new Promise((resolve) => {
https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
});

t.assert(response.body === 'OK');

t.is(beforeDealHttpsRequest.firstCall.args[0]._req.headers['proxy-authorization'], 'Basic Zm9v');
});

test('does not produce unhandled rejection when cannot connect to proxy', async (t) => {
const globalProxyAgent = createGlobalProxyAgent();

Expand Down

0 comments on commit a703be1

Please sign in to comment.