Skip to content

Commit a0e8f15

Browse files
authored
fix: clearing slash prefixes before adding uriSegment within url builder's processPattern (#77)
1 parent 644ad49 commit a0e8f15

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/router/lookup_store/url_builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ export class UrlBuilder {
110110
* we must break out from the loop
111111
*/
112112
if (token.type === 0) {
113-
uriSegments.push(`${token.val}${token.end}`)
113+
const value = token.val.startsWith('/') ? token.val.substring(1) : token.val
114+
uriSegments.push(`${value}${token.end}`)
114115
} else if (token.type === 2) {
115116
const values: string[] = paramsArray ? paramsArray.slice(paramsIndex) : paramsObject['*']
116117
this.#ensureHasWildCardValues(pattern, values)

tests/router/url_builder.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ test.group('URL builder', () => {
5252
assert.equal(lookupStore.builder().params([1]).make('users.show'), '/users/1')
5353
})
5454

55+
test('create url for a route by its name for the home path', ({ assert }) => {
56+
const app = new AppFactory().create(BASE_URL, () => {})
57+
const encryption = new EncryptionFactory().create()
58+
const lookupStore = new LookupStore(encryption, new QsParserFactory().create())
59+
60+
const route = new Route(app, [], {
61+
pattern: '/',
62+
globalMatchers: {},
63+
handler: () => {},
64+
methods: ['GET'],
65+
})
66+
route.as('home')
67+
68+
lookupStore.register(route.toJSON())
69+
assert.equal(lookupStore.builder().make('home'), '/')
70+
})
71+
5572
test('create url for a route by the handler name', ({ assert }) => {
5673
const app = new AppFactory().create(BASE_URL, () => {})
5774
const encryption = new EncryptionFactory().create()
@@ -68,6 +85,22 @@ test.group('URL builder', () => {
6885
assert.equal(lookupStore.builder().params([1]).make('#controllers/posts'), '/users/1')
6986
})
7087

88+
test('create url for a route by the handler name for the home path', ({ assert }) => {
89+
const app = new AppFactory().create(BASE_URL, () => {})
90+
const encryption = new EncryptionFactory().create()
91+
const lookupStore = new LookupStore(encryption, new QsParserFactory().create())
92+
93+
const route = new Route(app, [], {
94+
pattern: '/',
95+
globalMatchers: {},
96+
handler: '#controllers/home',
97+
methods: ['GET'],
98+
})
99+
100+
lookupStore.register(route.toJSON())
101+
assert.equal(lookupStore.builder().params([1]).make('#controllers/home'), '/')
102+
})
103+
71104
test('raise error when unable to lookup route', ({ assert }) => {
72105
const encryption = new EncryptionFactory().create()
73106
const lookupStore = new LookupStore(encryption, new QsParserFactory().create())

0 commit comments

Comments
 (0)