docs: update homepage link http to https (#5920)
[express.git] / test / req.subdomains.js
blobe5600f2eb56ee46de020c52be06127bb21730fa6
1 'use strict'
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){
10 var app = express();
12 app.use(function(req, res){
13 res.send(req.subdomains);
14 });
16 request(app)
17 .get('/')
18 .set('Host', 'tobi.ferrets.example.com')
19 .expect(200, ['ferrets', 'tobi'], done);
22 it('should work with IPv4 address', function(done){
23 var app = express();
25 app.use(function(req, res){
26 res.send(req.subdomains);
27 });
29 request(app)
30 .get('/')
31 .set('Host', '127.0.0.1')
32 .expect(200, [], done);
35 it('should work with IPv6 address', function(done){
36 var app = express();
38 app.use(function(req, res){
39 res.send(req.subdomains);
40 });
42 request(app)
43 .get('/')
44 .set('Host', '[::1]')
45 .expect(200, [], done);
49 describe('otherwise', function(){
50 it('should return an empty array', function(done){
51 var app = express();
53 app.use(function(req, res){
54 res.send(req.subdomains);
55 });
57 request(app)
58 .get('/')
59 .set('Host', 'example.com')
60 .expect(200, [], done);
64 describe('with no host', function(){
65 it('should return an empty array', function(done){
66 var app = express();
68 app.use(function(req, res){
69 req.headers.host = null;
70 res.send(req.subdomains);
71 });
73 request(app)
74 .get('/')
75 .expect(200, [], done);
79 describe('with trusted X-Forwarded-Host', function () {
80 it('should return an array', function (done) {
81 var app = express();
83 app.set('trust proxy', true);
84 app.use(function (req, res) {
85 res.send(req.subdomains);
86 });
88 request(app)
89 .get('/')
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){
98 var app = express();
99 app.set('subdomain offset', 0);
101 app.use(function(req, res){
102 res.send(req.subdomains);
105 request(app)
106 .get('/')
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) {
112 var app = express();
113 app.set('subdomain offset', 0);
115 app.use(function(req, res){
116 res.send(req.subdomains);
119 request(app)
120 .get('/')
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) {
126 var app = express();
127 app.set('subdomain offset', 0);
129 app.use(function(req, res){
130 res.send(req.subdomains);
133 request(app)
134 .get('/')
135 .set('Host', '[::1]')
136 .expect(200, ['[::1]'], done);
140 describe('when present', function(){
141 it('should return an array', function(done){
142 var app = express();
143 app.set('subdomain offset', 3);
145 app.use(function(req, res){
146 res.send(req.subdomains);
149 request(app)
150 .get('/')
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){
158 var app = express();
159 app.set('subdomain offset', 3);
161 app.use(function(req, res){
162 res.send(req.subdomains);
165 request(app)
166 .get('/')
167 .set('Host', 'sub.example.com')
168 .expect(200, [], done);