Skip to content

Commit

Permalink
fix: clearing slash prefixes before adding uriSegment within url buil…
Browse files Browse the repository at this point in the history
…der's processPattern (#77)
  • Loading branch information
tomgobich authored Nov 22, 2023
1 parent 644ad49 commit a0e8f15
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/router/lookup_store/url_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export class UrlBuilder {
* we must break out from the loop
*/
if (token.type === 0) {
uriSegments.push(`${token.val}${token.end}`)
const value = token.val.startsWith('/') ? token.val.substring(1) : token.val
uriSegments.push(`${value}${token.end}`)
} else if (token.type === 2) {
const values: string[] = paramsArray ? paramsArray.slice(paramsIndex) : paramsObject['*']
this.#ensureHasWildCardValues(pattern, values)
Expand Down
33 changes: 33 additions & 0 deletions tests/router/url_builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ test.group('URL builder', () => {
assert.equal(lookupStore.builder().params([1]).make('users.show'), '/users/1')
})

test('create url for a route by its name for the home path', ({ assert }) => {
const app = new AppFactory().create(BASE_URL, () => {})
const encryption = new EncryptionFactory().create()
const lookupStore = new LookupStore(encryption, new QsParserFactory().create())

const route = new Route(app, [], {
pattern: '/',
globalMatchers: {},
handler: () => {},
methods: ['GET'],
})
route.as('home')

lookupStore.register(route.toJSON())
assert.equal(lookupStore.builder().make('home'), '/')
})

test('create url for a route by the handler name', ({ assert }) => {
const app = new AppFactory().create(BASE_URL, () => {})
const encryption = new EncryptionFactory().create()
Expand All @@ -68,6 +85,22 @@ test.group('URL builder', () => {
assert.equal(lookupStore.builder().params([1]).make('#controllers/posts'), '/users/1')
})

test('create url for a route by the handler name for the home path', ({ assert }) => {
const app = new AppFactory().create(BASE_URL, () => {})
const encryption = new EncryptionFactory().create()
const lookupStore = new LookupStore(encryption, new QsParserFactory().create())

const route = new Route(app, [], {
pattern: '/',
globalMatchers: {},
handler: '#controllers/home',
methods: ['GET'],
})

lookupStore.register(route.toJSON())
assert.equal(lookupStore.builder().params([1]).make('#controllers/home'), '/')
})

test('raise error when unable to lookup route', ({ assert }) => {
const encryption = new EncryptionFactory().create()
const lookupStore = new LookupStore(encryption, new QsParserFactory().create())
Expand Down

0 comments on commit a0e8f15

Please sign in to comment.