Skip to content

Commit

Permalink
Merge pull request #35 from taralx/patch-1
Browse files Browse the repository at this point in the history
Two small fixes
  • Loading branch information
kwhitley authored May 30, 2021
2 parents f9a00dc + e152b70 commit 64c6e35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"rimraf": "^3.0.2",
"terser": "^5.7.0",
"vite": "^2.3.0",
"yarn": "^1.22.10",
"yarn-release": "^1.10.3"
}
}
44 changes: 22 additions & 22 deletions src/itty-router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ describe('Router', () => {

describe('.handle({ method = \'GET\', url })', () => {

it('returns { path, query } from match', () => {
it('returns { path, query } from match', async () => {
const route = routes.find(r => r.path === '/foo/:id')
router.handle(buildRequest({ path: '/foo/13?foo=bar&cat=dog' }))
await router.handle(buildRequest({ path: '/foo/13?foo=bar&cat=dog' }))

expect(route.callback).toHaveReturnedWith({
params: { id: '13' },
query: { foo: 'bar', cat: 'dog' },
})
})

it('requires exact route match', () => {
it('requires exact route match', async () => {
const route = routes.find(r => r.path === '/')

router.handle(buildRequest({ path: '/foo' }))
await router.handle(buildRequest({ path: '/foo' }))

expect(route.callback).not.toHaveBeenCalled()
})
Expand Down Expand Up @@ -166,53 +166,53 @@ describe('Router', () => {
expect(handler).toHaveReturnedWith('domain.dev')
})

it('match earliest routes that match', () => {
it('match earliest routes that match', async () => {
const route = routes.find(r => r.path === '/foo/first')
router.handle(buildRequest({ path: '/foo/first' }))
await router.handle(buildRequest({ path: '/foo/first' }))

expect(route.callback).toHaveBeenCalled()
})

it('honors correct method (e.g. GET, POST, etc)', () => {
it('honors correct method (e.g. GET, POST, etc)', async () => {
const route = routes.find(r => r.path === '/foo' && r.method === 'post')
router.handle(buildRequest({ method: 'POST', path: '/foo' }))
await router.handle(buildRequest({ method: 'POST', path: '/foo' }))

expect(route.callback).toHaveBeenCalled()
})

it('handles optional params (e.g. /foo/:id?)', () => {
it('handles optional params (e.g. /foo/:id?)', async () => {
const route = routes.find(r => r.path === '/optional/:id?')

router.handle(buildRequest({ path: '/optional' }))
await router.handle(buildRequest({ path: '/optional' }))
expect(route.callback).toHaveBeenCalled()

router.handle(buildRequest({ path: '/optional/13' }))
await router.handle(buildRequest({ path: '/optional/13' }))
expect(route.callback).toHaveBeenCalledTimes(2)
})

it('passes the entire original request through to the handler', () => {
it('passes the entire original request through to the handler', async () => {
const route = routes.find(r => r.path === '/passthrough')
router.handle(buildRequest({ path: '/passthrough', name: 'miffles' }))
await router.handle(buildRequest({ path: '/passthrough', name: 'miffles' }))

expect(route.callback).toHaveReturnedWith({
path: '/passthrough',
name: 'miffles',
})
})

it('accepts * as a wildcard route (e.g. for use in 404)', () => {
it('accepts * as a wildcard route (e.g. for use in 404)', async () => {
const route = routes.find(r => r.path === '*')
router.handle(buildRequest({ path: '/missing' }))
await router.handle(buildRequest({ path: '/missing' }))

expect(route.callback).toHaveBeenCalled()

const route2 = routes.find(r => r.path === '/wildcards/*')
router.handle(buildRequest({ path: '/wildcards/missing' }))
await router.handle(buildRequest({ path: '/wildcards/missing' }))

expect(route2.callback).toHaveBeenCalled()
})

it('allows missing handler later in flow with "all" channel', () => {
it('allows missing handler later in flow with "all" channel', async () => {
const missingHandler = jest.fn()
const matchHandler = jest.fn()

Expand All @@ -224,16 +224,16 @@ describe('Router', () => {
.all('/nested/*', router2.handle)
.all('*', missingHandler)

router1.handle(buildRequest({ path: '/foo' }))
await router1.handle(buildRequest({ path: '/foo' }))
expect(missingHandler).toHaveBeenCalled()

router1.handle(buildRequest({ path: '/nested/foo' }))
await router1.handle(buildRequest({ path: '/nested/foo' }))
expect(matchHandler).toHaveBeenCalled()
})

it('defaults to GET assumption when handling new requests without { method: \'METHOD\' }', () => {
it('defaults to GET assumption when handling new requests without { method: \'METHOD\' }', async () => {
const route = routes.find(r => r.path === '/foo')
router.handle({ url: 'https://example.com/foo' }) // no method listed
await router.handle({ url: 'https://example.com/foo' }) // no method listed

expect(route.callback).toHaveBeenCalled()
})
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('Router', () => {
const handler = jest.fn()
router.get('/foo/:id?', handler)

router.handle(buildRequest({ path: '/api/foo' }))
await router.handle(buildRequest({ path: '/api/foo' }))
expect(handler).toHaveBeenCalled()

await router.handle(buildRequest({ path: '/api/foo/13' }))
Expand Down

0 comments on commit 64c6e35

Please sign in to comment.