Skip to content

Commit

Permalink
Added req.subdomains
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 23, 2012
1 parent e2f43df commit 4e33245
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,23 @@ req.__defineGetter__('secure', function(){
return this.connection.encrypted;
});

/**
* Return subdomains as an array.
*
* For example "tobi.ferrets.example.com"
* would provide `["ferrets", "tobi"]`.
*
* @return {Array}
* @api public
*/

req.__defineGetter__('subdomains', function(){
return this.get('Host')
.split('.')
.slice(0, -2)
.reverse();
});

/**
* Short-hand for `require('url').parse(req.url).pathname`.
*
Expand Down
43 changes: 43 additions & 0 deletions test/req.subdomains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

var express = require('../')
, request = require('./support/http');

describe('req', function(){
describe('.subdomains', function(){
describe('when present', function(){
it('should return an array', function(done){
var app = express();

app.use(function(req, res){
res.send(req.subdomains);
});

request(app)
.get('/')
.set('Host', 'tobi.ferrets.example.com')
.end(function(res){
res.body.should.equal('["ferrets","tobi"]');
done();
})
})
})

describe('otherwise', function(){
it('should return an empty array', function(done){
var app = express();

app.use(function(req, res){
res.send(req.subdomains);
});

request(app)
.get('/')
.set('Host', 'example.com')
.end(function(res){
res.body.should.equal('[]');
done();
})
})
})
})
})

0 comments on commit 4e33245

Please sign in to comment.