Skip to content

Commit a703be1

Browse files
committed
fix: test https proxy-authorization header
1 parent 0d400e9 commit a703be1

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

src/classes/HttpsProxyAgent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ class HttpsProxyAgent extends Agent {
4141

4242
connectMessage += 'CONNECT ' + configuration.host + ':' + configuration.port + ' HTTP/1.1\r\n';
4343
connectMessage += 'Host: ' + configuration.host + ':' + configuration.port + '\r\n';
44+
4445
if (configuration.proxy.authorization) {
4546
connectMessage += 'Proxy-Authorization: Basic ' + Buffer.from(configuration.proxy.authorization).toString('base64') + '\r\n';
4647
}
48+
4749
connectMessage += '\r\n';
4850

4951
socket.write(connectMessage);

test/global-agent/factories/createGlobalProxyAgent.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ import test, {
1919
} from 'ava';
2020
import createGlobalProxyAgent from '../../../src/factories/createGlobalProxyAgent';
2121

22+
const anyproxyDefaultRules = {
23+
beforeDealHttpsRequest: async () => {
24+
return true;
25+
},
26+
beforeSendRequest: () => {
27+
return {
28+
response: {
29+
body: 'OK',
30+
header: {
31+
'content-type': 'text/plain',
32+
},
33+
statusCode: 200,
34+
},
35+
};
36+
},
37+
};
38+
2239
const defaultHttpAgent = http.globalAgent;
2340
const defaultHttpsAgent = https.globalAgent;
2441

@@ -87,31 +104,14 @@ const createHttpResponseResolver = (resolve) => {
87104
};
88105
};
89106

90-
const createProxyServer = async (maybeBeforeSendRequest) => {
107+
const createProxyServer = async (anyproxyRules) => {
91108
const port = await getNextPort();
92109

93-
let beforeSendRequest = () => {
94-
return {
95-
response: {
96-
body: 'OK',
97-
header: {
98-
'content-type': 'text/plain',
99-
},
100-
statusCode: 200,
101-
},
102-
};
103-
};
104-
105-
if (maybeBeforeSendRequest) {
106-
beforeSendRequest = maybeBeforeSendRequest;
107-
}
108-
109110
const localProxyServer = await new Promise((resolve) => {
110111
const proxyServer = new ProxyServer({
111-
forceProxyHttps: true,
112112
port,
113113
rule: {
114-
beforeSendRequest,
114+
...anyproxyRules ? anyproxyRules : anyproxyDefaultRules,
115115
},
116116
});
117117

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

176-
const beforeSendRequest = sinon.stub().callsFake(() => {
177-
return {
178-
response: {
179-
body: 'OK',
180-
header: {
181-
'content-type': 'text/plain',
182-
},
183-
statusCode: 200,
184-
},
185-
};
186-
});
176+
const beforeSendRequest = sinon.stub().callsFake(anyproxyDefaultRules.beforeSendRequest);
187177

188-
const proxyServer = await createProxyServer(beforeSendRequest);
178+
const proxyServer = await createProxyServer({
179+
beforeSendRequest,
180+
});
189181

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

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

207+
test('proxies HTTPS request with proxy-authorization header', async (t) => {
208+
const globalProxyAgent = createGlobalProxyAgent();
209+
210+
const beforeDealHttpsRequest = sinon.stub().callsFake(async () => {
211+
return true;
212+
});
213+
214+
const proxyServer = await createProxyServer({
215+
beforeDealHttpsRequest,
216+
beforeSendRequest: anyproxyDefaultRules.beforeSendRequest,
217+
});
218+
219+
globalProxyAgent.HTTP_PROXY = 'http://foo@127.0.0.1:' + proxyServer.port;
220+
221+
const response = await new Promise((resolve) => {
222+
https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
223+
});
224+
225+
t.assert(response.body === 'OK');
226+
227+
t.is(beforeDealHttpsRequest.firstCall.args[0]._req.headers['proxy-authorization'], 'Basic Zm9v');
228+
});
229+
215230
test('does not produce unhandled rejection when cannot connect to proxy', async (t) => {
216231
const globalProxyAgent = createGlobalProxyAgent();
217232

0 commit comments

Comments
 (0)