Skip to content

Commit 35c403b

Browse files
bkukerbkuker
authored and
bkuker
committed
fix: Request defaults not set when using Agent, issue #214.
1 parent d9a8d1f commit 35c403b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: lib/request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ methods.forEach(function(method){
339339
// https://tools.ietf.org/html/rfc6454#section-3
340340
req.withCredentials();
341341
}
342-
342+
this._setDefaults(req);
343343
return req;
344344
};
345345
});

Diff for: test/request.js

+43
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,49 @@ describe('request', function () {
175175
.then(done, done);
176176
});
177177

178+
it('agent can be used to set default headers', function (done){
179+
var agent = request.agent('https://httpbin.org');
180+
agent.set("Header-Name", "header_value");
181+
182+
agent
183+
.get('/headers')
184+
.then(function (res){
185+
res.should.have.status(200);
186+
res.body.headers.should.have.property('Header-Name').that.equals("header_value");
187+
agent.close();
188+
})
189+
.then(done, done);
190+
});
191+
192+
it('agent can be used to set default bearer authentication', function (done){
193+
var agent = request.agent('https://httpbin.org');
194+
agent.set("Authorization", "Bearer test_bearer");
195+
196+
agent
197+
.get('/bearer')
198+
.then(function (res){
199+
res.should.have.status(200);
200+
res.should.be.json;
201+
res.body.should.have.property('authenticated').that.equals(true);
202+
res.body.should.have.property('token').that.equals("test_bearer");
203+
agent.close();
204+
})
205+
.then(done, done);
206+
});
207+
208+
it('agent can be used to set default basic authentication', function (done){
209+
var agent = request.agent('https://httpbin.org');
210+
agent.auth("user", "passwd");
211+
212+
agent
213+
.get('/basic-auth/user/passwd')
214+
.then(function (res){
215+
res.should.have.status(200);
216+
agent.close();
217+
})
218+
.then(done, done);
219+
});
220+
178221
it('automatically closes the server down once done with it', function (done) {
179222
var server = require('http').createServer(function (req, res) {
180223
res.writeHeader(200, { 'content-type' : 'text/plain' });

0 commit comments

Comments
 (0)