Skip to content

Commit 2eece52

Browse files
committed
test: more tests, including failing 407 proxy response test
1 parent 3736aad commit 2eece52

1 file changed

Lines changed: 57 additions & 15 deletions

File tree

test/test.js

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,63 @@ describe('HttpsProxyAgent', function () {
107107
assert.equal('127.0.0.1', agent.proxy.host);
108108
assert.equal(proxyPort, agent.proxy.port);
109109
});
110+
describe('secureEndpoint', function () {
111+
it('should default to `true`', function () {
112+
var agent = new HttpsProxyAgent('http://127.0.0.1:' + proxyPort);
113+
assert.equal(true, agent.secureEndpoint);
114+
});
115+
});
116+
});
117+
118+
describe('"http" module', function () {
119+
it('should receive the 407 authorization code on the `http.ClientResponse`', function (done) {
120+
// set a proxy authentication function for this test
121+
proxy.authenticate = function (req, fn) {
122+
// reject all requests
123+
fn(null, false);
124+
};
125+
126+
var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
127+
var agent = new HttpsProxyAgent(proxyUri);
128+
129+
var opts = {};
130+
// `host` and `port` don't really matter since the proxy will reject anyways
131+
opts.host = '127.0.0.1';
132+
opts.port = 80;
133+
opts.agent = agent;
134+
135+
var req = http.get(opts, function (res) {
136+
assert.equal(407, res.statusCode);
137+
assert('proxy-authenticate' in res.headers);
138+
res.on('data', function () {
139+
console.error('"data"', arguments);
140+
});
141+
res.on('end', function () {
142+
console.error('"end"', arguments);
143+
});
144+
delete proxy.authenticate;
145+
done();
146+
});
147+
148+
req.on('close', function () {
149+
console.error('"close"', arguments);
150+
});
151+
});
152+
it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function (done) {
153+
// port 4 is a reserved, but "unassigned" port
154+
var proxyUri = 'http://127.0.0.1:4';
155+
var agent = new HttpsProxyAgent(proxyUri);
156+
157+
var opts = url.parse('http://nodejs.org');
158+
opts.agent = agent;
159+
160+
var req = http.get(opts);
161+
req.once('error', function (err) {
162+
assert.equal('ECONNREFUSED', err.code);
163+
req.abort();
164+
done();
165+
});
166+
});
110167
});
111168

112169
describe('"https" module', function () {
@@ -173,21 +230,6 @@ describe('HttpsProxyAgent', function () {
173230
});
174231
});
175232
});
176-
it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function (done) {
177-
// port 4 is a reserved, but "unassigned" port
178-
var proxyUri = 'http://127.0.0.1:4';
179-
var agent = new HttpsProxyAgent(proxyUri);
180-
181-
var opts = url.parse('http://nodejs.org');
182-
opts.agent = agent;
183-
184-
var req = http.get(opts);
185-
req.once('error', function (err) {
186-
assert.equal('ECONNREFUSED', err.code);
187-
req.abort();
188-
done();
189-
});
190-
});
191233
});
192234

193235
});

0 commit comments

Comments
 (0)