Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add req.auth test coverage

  • Loading branch information...
commit 7bf4ad30fce7a203e62221888591194ddbd785de 1 parent b3936b9
@tj tj authored
Showing with 52 additions and 3 deletions.
  1. +3 −3 lib/request.js
  2. +49 −0 test/req.auth.js
View
6 lib/request.js
@@ -377,18 +377,18 @@ req.__defineGetter__('ips', function(){
* req.auth
* // => { username: 'tobi', password: 'hello' }
*
- * @return {Object}
+ * @return {Object} or undefined
* @api public
*/
req.__defineGetter__('auth', function(){
// missing
var auth = this.get('Authorization');
- if (!auth) return {};
+ if (!auth) return;
// malformed
auth = auth.split(' ')[1];
- if (!auth) return {};
+ if (!auth) return;
// credentials
auth = new Buffer(auth, 'base64').toString().split(':');
View
49 test/req.auth.js
@@ -0,0 +1,49 @@
+
+var express = require('../')
+ , request = require('./support/http');
+
+describe('req', function(){
+ describe('.auth', function(){
+ describe('when Authorization is missing', function(){
+ it('should return undefined', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send(req.auth || 'none');
+ });
+
+ request(app)
+ .get('/')
+ .expect('none', done)
+ })
+ })
+
+ describe('when Authorization is malformed', function(){
+ it('should return undefined', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send(req.auth || 'none');
+ });
+
+ request(app)
+ .get('/')
+ .set('Authorization', 'meow')
+ .expect('none', done)
+ })
+ })
+
+ it('should return .username and .password', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send(req.auth || 'none');
+ });
+
+ request(app)
+ .get('/')
+ .set('Authorization', 'Basic dG9iaTpmZXJyZXQ=')
+ .expect('{"username":"tobi","password":"ferret"}', done)
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.