/
subdomains.test.js
107 lines (97 loc) · 2.84 KB
/
subdomains.test.js
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
// Dependencies
var assert = require('assert')
, subdomains = require(__dirname +'/../lib/subdomains')
// Run tests
describe('express-subdomains', function () {
it('should convert a basic subdomain 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.use('api')
subdomains.middleware.apply({}, args)
})
it('should convert a multi level subdomain into a route', function (done) {
var req = { url: '/', headers: { host: 'api.01.vanity.domain.com' } },
args = [
req,
{},
function () {
assert.strictEqual(req.url, '/api.01.vanity', 'route should be `/api.01.vanity`')
done()
}
]
subdomains.use('api.01.vanity')
subdomains.middleware.apply({}, args)
})
it('should convert a subdomain into a route and combine the path', function (done) {
var req = { url: '/users', headers: { host: 'api.domain.com/users' } },
args = [
req,
{},
function () {
assert.strictEqual(req.url, '/api/users', 'route should be `/api/users`')
done()
}
]
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)
})
})