fix: subdomains should be [] if the host is an ip (#808)

Closes: #775
master
Adam Lau 2016-09-07 16:21:32 +08:00 committed by Yiyu He
parent e4c0a53421
commit 21c0d823dd
2 changed files with 11 additions and 2 deletions

View File

@ -5,6 +5,7 @@
* Module dependencies.
*/
const net = require('net');
const contentType = require('content-type');
const stringify = require('url').format;
const parse = require('parseurl');
@ -404,7 +405,9 @@ module.exports = {
get subdomains() {
const offset = this.app.subdomainOffset;
return (this.host || '')
const hostname = this.hostname;
if (net.isIP(hostname)) return [];
return hostname
.split('.')
.reverse()
.slice(offset);

View File

@ -14,8 +14,14 @@ describe('req.subdomains', () => {
req.subdomains.should.eql(['tobi']);
});
describe('with no host present', () => {
it('should work with no host present', () => {
const req = request();
req.subdomains.should.eql([]);
});
it('should check if the host is an ip address, even with a port', () => {
const req = request();
req.header.host = '127.0.0.1:3000';
req.subdomains.should.eql([]);
});
});