forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReqSubdomains.mjs
174 lines (140 loc) · 4.14 KB
/
ReqSubdomains.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
import { describe, it } from 'node:test'
import express from '../lib/express.js'
import request from 'supertest'
describe('req', () => {
describe('.subdomains', () => {
describe('when present', () => {
it('should return an array', (t, done) => {
const app = express()
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', 'tobi.ferrets.example.com')
.expect(200, ['ferrets', 'tobi'], done)
})
it('should work with IPv4 address', (t, done) => {
const app = express()
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', '127.0.0.1')
.expect(200, [], done)
})
it('should work with IPv6 address', (t, done) => {
const app = express()
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', '[::1]')
.expect(200, [], done)
})
})
describe('otherwise', () => {
it('should return an empty array', (t, done) => {
const app = express()
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', 'example.com')
.expect(200, [], done)
})
})
describe('with no host', () => {
it('should return an empty array', (t, done) => {
const app = express()
app.use((req, res) => {
req.headers.host = null
res.send(req.subdomains)
})
request(app)
.get('/')
.expect(200, [], done)
})
})
describe('with trusted X-Forwarded-Host', () => {
it('should return an array', (t, done) => {
const app = express()
app.set('trust proxy', true)
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('X-Forwarded-Host', 'tobi.ferrets.example.com')
.expect(200, ['ferrets', 'tobi'], done)
})
})
describe('when subdomain offset is set', () => {
describe('when subdomain offset is zero', () => {
it('should return an array with the whole domain', (t, done) => {
const app = express()
app.set('subdomain offset', 0)
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', 'tobi.ferrets.sub.example.com')
.expect(200, ['com', 'example', 'sub', 'ferrets', 'tobi'], done)
})
it('should return an array with the whole IPv4', (t, done) => {
const app = express()
app.set('subdomain offset', 0)
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', '127.0.0.1')
.expect(200, ['127.0.0.1'], done)
})
it('should return an array with the whole IPv6', (t, done) => {
const app = express()
app.set('subdomain offset', 0)
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', '[::1]')
.expect(200, ['[::1]'], done)
})
})
describe('when present', () => {
it('should return an array', (t, done) => {
const app = express()
app.set('subdomain offset', 3)
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', 'tobi.ferrets.sub.example.com')
.expect(200, ['ferrets', 'tobi'], done)
})
})
describe('otherwise', () => {
it('should return an empty array', (t, done) => {
const app = express()
app.set('subdomain offset', 3)
app.use((req, res) => {
res.send(req.subdomains)
})
request(app)
.get('/')
.set('Host', 'sub.example.com')
.expect(200, [], done)
})
})
})
})
})