Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added strict subdomain check, domain option and tests.

  • Loading branch information...
commit e518313d42d617960bfd3b9cc7725f234d1a9145 1 parent bb06b27
@gabmontes gabmontes authored
Showing with 103 additions and 36 deletions.
  1. +32 −23 lib/subdomains.js
  2. +71 −13 test/subdomains.test.js
View
55 lib/subdomains.js
@@ -5,40 +5,49 @@
* MIT Licensed
*/
-var subs = [{base:'.'}]
-var Sequence = require('sequence');
-
+var domain = "",
+ subs = [{ base: '.' }],
+ Sequence = require('sequence');
+
module.exports = {
use: function (base, options) {
- options = options || {}
subs.unshift({
- base:base,
- options:options
- })
- return this
+ base: base,
+ options: options || {}
+ });
+ return this;
+ },
+ domain: function (url) {
+ domain = url;
+ return this;
},
middleware: function (req, res, next) {
- forEachAsync(subs, function(fn, item, i, arr) {
- if (RegExp(item.base, "gi").test(req.headers.host)) {
+ forEachAsync(subs, function (fn, item, i, arr) {
+ var regexp = item.base;
+ if (regexp !== '.') { regexp = "^" + regexp + "\\."; }
+ if (domain) { regexp = regexp + domain + "$"; }
+ if (RegExp(regexp, "gi").test(req.headers.host)) {
if (item.base !== '.') {
- if (req.url === '/') req.url = ''
- req.url = '/' + item.base + req.url
+ if (req.url === '/') {
+ req.url = '';
+ }
+ req.url = '/' + item.base + req.url;
}
- next(req, res, next)
+ next();
+ } else {
+ fn();
}
- else fn()
- })
+ });
}
-}
+};
-function forEachAsync (arr, callback) {
- var sequence = Sequence()
+function forEachAsync(arr, callback) {
+ var sequence = Sequence();
function handleItem(item, i, arr) {
sequence.then(function (next) {
- callback(next, item, i, arr)
- })
+ callback(next, item, i, arr);
+ });
}
- arr.forEach(handleItem)
- return sequence
+ arr.forEach(handleItem);
+ return sequence;
}
-
View
84 test/subdomains.test.js
@@ -7,10 +7,11 @@ var assert = require('assert')
describe('express-subdomains', function () {
it('should convert a basic subdomain into a route', function (done) {
- var args = [
- { url: '/', headers: { host: 'api.domain.com' } },
+ var req = { url: '/', headers: { host: 'api.domain.com' } },
+ args = [
+ req,
{},
- function (req, res, next) {
+ function () {
assert.strictEqual(req.url, '/api', 'route should be `/api`')
done()
}
@@ -18,12 +19,13 @@ describe('express-subdomains', function () {
subdomains.use('api')
subdomains.middleware.apply({}, args)
})
-
+
it('should convert a multi level subdomain into a route', function (done) {
- var args = [
- { url: '/', headers: { host: 'api.01.vanity.domain.com' } },
+ var req = { url: '/', headers: { host: 'api.01.vanity.domain.com' } },
+ args = [
+ req,
{},
- function (req, res, next) {
+ function () {
assert.strictEqual(req.url, '/api.01.vanity', 'route should be `/api.01.vanity`')
done()
}
@@ -31,12 +33,13 @@ describe('express-subdomains', function () {
subdomains.use('api.01.vanity')
subdomains.middleware.apply({}, args)
})
-
+
it('should convert a subdomain into a route and combine the path', function (done) {
- var args = [
- { url: '/users', headers: { host: 'api.domain.com/users' } },
+ var req = { url: '/users', headers: { host: 'api.domain.com/users' } },
+ args = [
+ req,
{},
- function (req, res, next) {
+ function () {
assert.strictEqual(req.url, '/api/users', 'route should be `/api/users`')
done()
}
@@ -44,6 +47,61 @@ describe('express-subdomains', function () {
subdomains.use('api')
subdomains.middleware.apply({}, args)
})
-
-})
+ it('should not convert any start match that is not a subdomain into a route', function (done) {
+ var req = { url: '/', headers: { host: 'apinew.domain.com' } },
+ args = [
+ req,
+ {},
+ function () {
+ assert.strictEqual(req.url, '/', 'route should be `/`')
+ done()
+ }
+ ]
+ subdomains.use('api')
+ subdomains.middleware.apply({}, args)
+ })
+
+ it('should not convert any partial match into a route', function (done) {
+ var req = { url: '/', headers: { host: 'newapi.domain.com' } },
+ args = [
+ req,
+ {},
+ function () {
+ assert.strictEqual(req.url, '/', 'route should be `/`')
+ done()
+ }
+ ]
+ subdomains.use('api')
+ subdomains.middleware.apply({}, args)
+ })
+
+ it('should convert a subdomain and domain into a route', function (done) {
+ var req = { url: '/', headers: { host: 'api.domain.com' } },
+ args = [
+ req,
+ {},
+ function () {
+ assert.strictEqual(req.url, '/api', 'route should be `/api`')
+ done()
+ }
+ ]
+ subdomains.domain('domain.com').use('api')
+ subdomains.middleware.apply({}, args)
+ })
+
+ it('should not convert a subdomain with different domain into a route', function (done) {
+ var req = { url: '/', headers: { host: 'api.otherdomain.com' } },
+ args = [
+ req,
+ {},
+ function () {
+ assert.strictEqual(req.url, '/', 'route should be `/`')
+ done()
+ }
+ ]
+ subdomains.domain('domain.com').use('api')
+ subdomains.middleware.apply({}, args)
+ })
+
+})
Please sign in to comment.
Something went wrong with that request. Please try again.