Skip to content

Commit

Permalink
fix: Request defaults not set when using Agent, issue #214.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkuker authored and bkuker committed Aug 14, 2018
1 parent d9a8d1f commit 8c1e530
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ methods.forEach(function(method){
// https://tools.ietf.org/html/rfc6454#section-3
req.withCredentials();
}

this._setDefaults(req);
return req;
};
});
Expand Down
43 changes: 43 additions & 0 deletions test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,49 @@ describe('request', function () {
.then(done, done);
});

it('agent can be used to set default headers', function (done){
var agent = request.agent('https://httpbin.org');
agent.set("Header-Name", "header_value");

agent
.get('/headers')
.then(function (res){
res.should.have.status(200);
res.body.headers.should.have.property('Header-Name').that.equals("header_value");
agent.close();
})
.then(done, done);
});

it('agent can be used to set default baerer authentication', function (done){
var agent = request.agent('https://httpbin.org');
agent.set("Authorization", "Bearer test_bearer");

agent
.get('/bearer')
.then(function (res){
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('authenticated').that.equals(true);
res.body.should.have.property('token').that.equals("test_bearer");
agent.close();
})
.then(done, done);
});

it('agent can be used to set default basic authentication', function (done){
var agent = request.agent('https://httpbin.org');
agent.auth("user", "passwd");

agent
.get('/basic-auth/user/passwd')
.then(function (res){
res.should.have.status(200);
agent.close();
})
.then(done, done);
});

it('automatically closes the server down once done with it', function (done) {
var server = require('http').createServer(function (req, res) {
res.writeHeader(200, { 'content-type' : 'text/plain' });
Expand Down

0 comments on commit 8c1e530

Please sign in to comment.