3 var express
= require('../')
4 , request
= require('supertest');
6 describe('req', function(){
7 describe('.subdomains', function(){
8 describe('when present', function(){
9 it('should return an array', function(done
){
12 app
.use(function(req
, res
){
13 res
.send(req
.subdomains
);
18 .set('Host', 'tobi.ferrets.example.com')
19 .expect(200, ['ferrets', 'tobi'], done
);
22 it('should work with IPv4 address', function(done
){
25 app
.use(function(req
, res
){
26 res
.send(req
.subdomains
);
31 .set('Host', '127.0.0.1')
32 .expect(200, [], done
);
35 it('should work with IPv6 address', function(done
){
38 app
.use(function(req
, res
){
39 res
.send(req
.subdomains
);
45 .expect(200, [], done
);
49 describe('otherwise', function(){
50 it('should return an empty array', function(done
){
53 app
.use(function(req
, res
){
54 res
.send(req
.subdomains
);
59 .set('Host', 'example.com')
60 .expect(200, [], done
);
64 describe('with no host', function(){
65 it('should return an empty array', function(done
){
68 app
.use(function(req
, res
){
69 req
.headers
.host
= null;
70 res
.send(req
.subdomains
);
75 .expect(200, [], done
);
79 describe('with trusted X-Forwarded-Host', function () {
80 it('should return an array', function (done
) {
83 app
.set('trust proxy', true);
84 app
.use(function (req
, res
) {
85 res
.send(req
.subdomains
);
90 .set('X-Forwarded-Host', 'tobi.ferrets.example.com')
91 .expect(200, ['ferrets', 'tobi'], done
);
95 describe('when subdomain offset is set', function(){
96 describe('when subdomain offset is zero', function(){
97 it('should return an array with the whole domain', function(done
){
99 app
.set('subdomain offset', 0);
101 app
.use(function(req
, res
){
102 res
.send(req
.subdomains
);
107 .set('Host', 'tobi.ferrets.sub.example.com')
108 .expect(200, ['com', 'example', 'sub', 'ferrets', 'tobi'], done
);
111 it('should return an array with the whole IPv4', function (done
) {
113 app
.set('subdomain offset', 0);
115 app
.use(function(req
, res
){
116 res
.send(req
.subdomains
);
121 .set('Host', '127.0.0.1')
122 .expect(200, ['127.0.0.1'], done
);
125 it('should return an array with the whole IPv6', function (done
) {
127 app
.set('subdomain offset', 0);
129 app
.use(function(req
, res
){
130 res
.send(req
.subdomains
);
135 .set('Host', '[::1]')
136 .expect(200, ['[::1]'], done
);
140 describe('when present', function(){
141 it('should return an array', function(done
){
143 app
.set('subdomain offset', 3);
145 app
.use(function(req
, res
){
146 res
.send(req
.subdomains
);
151 .set('Host', 'tobi.ferrets.sub.example.com')
152 .expect(200, ['ferrets', 'tobi'], done
);
156 describe('otherwise', function(){
157 it('should return an empty array', function(done
){
159 app
.set('subdomain offset', 3);
161 app
.use(function(req
, res
){
162 res
.send(req
.subdomains
);
167 .set('Host', 'sub.example.com')
168 .expect(200, [], done
);