Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #214, request defaults not set when using Agent. #215

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 bearer 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