forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReqBaseUrl.mjs
89 lines (76 loc) · 2.22 KB
/
ReqBaseUrl.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
'use strict'
import { describe, it } from 'node:test'
import express from '../lib/express.js'
import request from 'supertest'
describe('req', () => {
describe('.baseUrl', () => {
it('should be empty for top-level route', (t, done) => {
const app = express()
app.get('/:a', (req, res) => {
res.end(req.baseUrl)
})
request(app)
.get('/foo')
.expect(200, '', done)
})
it('should contain lower path', (t, done) => {
const app = express()
const sub = express.Router()
sub.get('/:b', (req, res) => {
res.end(req.baseUrl)
})
app.use('/:a', sub.handle.bind(sub))
request(app)
.get('/foo/bar')
.expect(200, '/foo', done)
})
it('should contain full lower path', (t, done) => {
const app = express()
const sub1 = express.Router()
const sub2 = express.Router()
const sub3 = express.Router()
sub3.get('/:d', (req, res) => {
res.end(req.baseUrl)
})
sub2.use('/:c', sub3.handle.bind(sub3))
sub1.use('/:b', sub2.handle.bind(sub2))
app.use('/:a', sub1.handle.bind(sub1))
request(app)
.get('/foo/bar/baz/zed')
.expect(200, '/foo/bar/baz', done)
})
it('should travel through routers correctly', (t, done) => {
const urls = []
const app = express()
const sub1 = express.Router()
const sub2 = express.Router()
const sub3 = express.Router()
sub3.get('/:d', (req, res, next) => {
urls.push('0@' + req.baseUrl)
next()
})
sub2.use('/:c', sub3.handle.bind(sub3))
sub1.use('/', (req, res, next) => {
urls.push('1@' + req.baseUrl)
next()
})
sub1.use('/bar', sub2.handle.bind(sub2))
sub1.use('/bar', (req, res, next) => {
urls.push('2@' + req.baseUrl)
next()
})
app.use((req, res, next) => {
urls.push('3@' + req.baseUrl)
next()
})
app.use('/:a', sub1.handle.bind(sub1))
app.use((req, res, next) => {
urls.push('4@' + req.baseUrl)
res.end(urls.join(','))
})
request(app)
.get('/foo/bar/baz/zed')
.expect(200, '3@,1@/foo,0@/foo/bar/baz,2@/foo/bar,4@', done)
})
})
})