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

Support setting other defaults in the re-usable agent besides cookies. #453

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ describe('request.agent(app)', function() {
.expect('hey', done);
});
})

Another use could be to automatically send an Authorization header by default:


var apiAgent = request.agent(app).set('Authorization', 'Bearer SOMETOKEN');

// Now run lots of tests against API that requires authetnication.
apiAgent.get(...)


```
There is another example that is introduced by the file [agency.js](https://github.com/visionmedia/superagent/blob/master/test/node/agency.js)

Expand Down
1 change: 1 addition & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ methods.forEach(function(method) {
req.on('redirect', this._saveCookies.bind(this));
req.on('redirect', this._attachCookies.bind(this, req));
this._attachCookies(req);
this._setDefaults(req);

return req;
};
Expand Down
18 changes: 18 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,15 @@ describe('request.agent(app)', function () {
else res.send(':(');
});

// Echo back the authorization header we receive to test set() defaults
app.get('/set', function(req, res) {
if (req.get('authorization')) {
res.send(req.get('authorization'));
} else {
res.send(':(');
}
});

it('should save cookies', function (done) {
agent
.get('/')
Expand All @@ -858,6 +867,15 @@ describe('request.agent(app)', function () {
.get('/return')
.expect('hey', done);
});


it('should support set() defaults', function(done) {
var defaultsAgent = request.agent(app).set('Authorization', 'Bearer');

defaultsAgent
.get('/set')
.expect('Bearer', done);
});
});

describe('agent.host(host)', function () {
Expand Down