diff --git a/Readme.md b/Readme.md index bf8194ed..4e0352b9 100644 --- a/Readme.md +++ b/Readme.md @@ -180,6 +180,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) diff --git a/lib/agent.js b/lib/agent.js index 5f16ae3d..6caa0e54 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -52,6 +52,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; }; diff --git a/package.json b/package.json index 20984045..28000eca 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "test": "eslint lib/**/*.js test/**/*.js && mocha --require should --reporter spec --check-leaks" }, "dependencies": { - "superagent": "^3.0.0", + "superagent": "^3.8.0", "methods": "~1.1.2" }, "devDependencies": { diff --git a/test/supertest.js b/test/supertest.js index c9a4927b..651ad274 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -802,6 +802,16 @@ 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('/') @@ -813,6 +823,14 @@ 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('. works as expected', function() {