|
| 1 | +import { Hono } from '../../hono' |
| 2 | +import { bearerAuth } from '../bearer-auth' |
| 3 | +import { routeCheck } from '.' |
| 4 | + |
| 5 | +describe('Route Check Middleware', () => { |
| 6 | + let app: Hono |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + app = new Hono() |
| 10 | + }) |
| 11 | + |
| 12 | + describe('Basic functionality', () => { |
| 13 | + it('Should allow access to existing routes', async () => { |
| 14 | + app.use('/admin/*', routeCheck()) |
| 15 | + app.get('/admin/dashboard', (c) => c.text('Admin Dashboard')) |
| 16 | + |
| 17 | + const res = await app.request('/admin/dashboard') |
| 18 | + expect(res.status).toBe(200) |
| 19 | + expect(await res.text()).toBe('Admin Dashboard') |
| 20 | + }) |
| 21 | + |
| 22 | + it('Should return 404 for non-existent routes', async () => { |
| 23 | + app.use('/admin/*', routeCheck()) |
| 24 | + app.get('/admin/dashboard', (c) => c.text('Admin Dashboard')) |
| 25 | + |
| 26 | + const res = await app.request('/admin/non-existent') |
| 27 | + expect(res.status).toBe(404) |
| 28 | + expect(await res.text()).toBe('404 Not Found') |
| 29 | + }) |
| 30 | + |
| 31 | + it('Should work with POST routes', async () => { |
| 32 | + app.use('/api/*', routeCheck()) |
| 33 | + app.post('/api/users', (c) => c.text('User Created')) |
| 34 | + |
| 35 | + const res = await app.request('/api/users', { method: 'POST' }) |
| 36 | + expect(res.status).toBe(200) |
| 37 | + expect(await res.text()).toBe('User Created') |
| 38 | + }) |
| 39 | + |
| 40 | + it('Should return 404 for non-existent POST routes', async () => { |
| 41 | + app.use('/api/*', routeCheck()) |
| 42 | + app.post('/api/users', (c) => c.text('User Created')) |
| 43 | + |
| 44 | + const res = await app.request('/api/posts', { method: 'POST' }) |
| 45 | + expect(res.status).toBe(404) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + describe('Integration with authentication middleware', () => { |
| 50 | + it('Should skip authentication for non-existent routes', async () => { |
| 51 | + let authExecuted = false |
| 52 | + |
| 53 | + app.use('/admin/*', routeCheck()) |
| 54 | + app.use('/admin/*', async (c, next) => { |
| 55 | + authExecuted = true |
| 56 | + await next() |
| 57 | + }) |
| 58 | + app.get('/admin/dashboard', (c) => c.text('Admin Dashboard')) |
| 59 | + |
| 60 | + const res = await app.request('/admin/non-existent') |
| 61 | + expect(res.status).toBe(404) |
| 62 | + expect(authExecuted).toBe(false) |
| 63 | + }) |
| 64 | + |
| 65 | + it('Should execute authentication for existing routes', async () => { |
| 66 | + let authExecuted = false |
| 67 | + |
| 68 | + app.use('/admin/*', routeCheck()) |
| 69 | + app.use('/admin/*', async (c, next) => { |
| 70 | + authExecuted = true |
| 71 | + await next() |
| 72 | + }) |
| 73 | + app.get('/admin/dashboard', (c) => c.text('Admin Dashboard')) |
| 74 | + |
| 75 | + const res = await app.request('/admin/dashboard') |
| 76 | + expect(res.status).toBe(200) |
| 77 | + expect(authExecuted).toBe(true) |
| 78 | + }) |
| 79 | + |
| 80 | + it('Should work with bearerAuth middleware', async () => { |
| 81 | + app.use('/admin/*', routeCheck()) |
| 82 | + app.use('/admin/*', bearerAuth({ token: 'my-secret' })) |
| 83 | + app.get('/admin/dashboard', (c) => c.text('Admin Dashboard')) |
| 84 | + |
| 85 | + // Non-existent route should return 404 without auth |
| 86 | + const res1 = await app.request('/admin/non-existent') |
| 87 | + expect(res1.status).toBe(404) |
| 88 | + |
| 89 | + // Existing route without auth should return 401 |
| 90 | + const res2 = await app.request('/admin/dashboard') |
| 91 | + expect(res2.status).toBe(401) |
| 92 | + |
| 93 | + // Existing route with auth should return 200 |
| 94 | + const res3 = await app.request('/admin/dashboard', { |
| 95 | + headers: { Authorization: 'Bearer my-secret' }, |
| 96 | + }) |
| 97 | + expect(res3.status).toBe(200) |
| 98 | + expect(await res3.text()).toBe('Admin Dashboard') |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('Support for different route definitions', () => { |
| 103 | + it('Should work with app.all()', async () => { |
| 104 | + app.use('/admin/*', routeCheck()) |
| 105 | + app.all('/admin/settings', (c) => c.text('Settings')) |
| 106 | + |
| 107 | + const res1 = await app.request('/admin/settings', { method: 'GET' }) |
| 108 | + expect(res1.status).toBe(200) |
| 109 | + expect(await res1.text()).toBe('Settings') |
| 110 | + |
| 111 | + const res2 = await app.request('/admin/settings', { method: 'POST' }) |
| 112 | + expect(res2.status).toBe(200) |
| 113 | + expect(await res2.text()).toBe('Settings') |
| 114 | + }) |
| 115 | + |
| 116 | + it('Should distinguish between handlers and middleware', async () => { |
| 117 | + app.use('/api/*', routeCheck()) |
| 118 | + app.use('/api/*', async (c, next) => { |
| 119 | + // This is middleware, not a handler |
| 120 | + await next() |
| 121 | + }) |
| 122 | + app.get('/api/users', (c) => c.text('Users')) |
| 123 | + |
| 124 | + // Should return 200 because handler exists |
| 125 | + const res1 = await app.request('/api/users') |
| 126 | + expect(res1.status).toBe(200) |
| 127 | + |
| 128 | + // Should return 404 because no handler exists |
| 129 | + const res2 = await app.request('/api/posts') |
| 130 | + expect(res2.status).toBe(404) |
| 131 | + }) |
| 132 | + |
| 133 | + it('Should work with multiple HTTP methods on same path', async () => { |
| 134 | + app.use('/api/*', routeCheck()) |
| 135 | + app.get('/api/users', (c) => c.text('Get Users')) |
| 136 | + app.post('/api/users', (c) => c.text('Create User')) |
| 137 | + |
| 138 | + const res1 = await app.request('/api/users', { method: 'GET' }) |
| 139 | + expect(res1.status).toBe(200) |
| 140 | + expect(await res1.text()).toBe('Get Users') |
| 141 | + |
| 142 | + const res2 = await app.request('/api/users', { method: 'POST' }) |
| 143 | + expect(res2.status).toBe(200) |
| 144 | + expect(await res2.text()).toBe('Create User') |
| 145 | + |
| 146 | + // PUT should return 404 as no handler exists |
| 147 | + const res3 = await app.request('/api/users', { method: 'PUT' }) |
| 148 | + expect(res3.status).toBe(404) |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + describe('Custom onNotFound handler', () => { |
| 153 | + it('Should use custom handler when route not found', async () => { |
| 154 | + app.use( |
| 155 | + '/api/*', |
| 156 | + routeCheck({ |
| 157 | + onNotFound: (c) => c.json({ error: 'API endpoint not found' }, 404), |
| 158 | + }) |
| 159 | + ) |
| 160 | + app.get('/api/users', (c) => c.text('Users')) |
| 161 | + |
| 162 | + const res = await app.request('/api/posts') |
| 163 | + expect(res.status).toBe(404) |
| 164 | + expect(await res.json()).toEqual({ error: 'API endpoint not found' }) |
| 165 | + }) |
| 166 | + |
| 167 | + it('Should use default handler when onNotFound not specified', async () => { |
| 168 | + app.use('/api/*', routeCheck()) |
| 169 | + app.get('/api/users', (c) => c.text('Users')) |
| 170 | + |
| 171 | + const res = await app.request('/api/posts') |
| 172 | + expect(res.status).toBe(404) |
| 173 | + expect(await res.text()).toBe('404 Not Found') |
| 174 | + }) |
| 175 | + |
| 176 | + it('Should not call onNotFound when route exists', async () => { |
| 177 | + let onNotFoundCalled = false |
| 178 | + |
| 179 | + app.use( |
| 180 | + '/api/*', |
| 181 | + routeCheck({ |
| 182 | + onNotFound: (c) => { |
| 183 | + onNotFoundCalled = true |
| 184 | + return c.json({ error: 'Not found' }, 404) |
| 185 | + }, |
| 186 | + }) |
| 187 | + ) |
| 188 | + app.get('/api/users', (c) => c.text('Users')) |
| 189 | + |
| 190 | + const res = await app.request('/api/users') |
| 191 | + expect(res.status).toBe(200) |
| 192 | + expect(onNotFoundCalled).toBe(false) |
| 193 | + }) |
| 194 | + }) |
| 195 | + |
| 196 | + describe('Edge cases', () => { |
| 197 | + it('Should work without any routes defined', async () => { |
| 198 | + app.use('/admin/*', routeCheck()) |
| 199 | + |
| 200 | + const res = await app.request('/admin/anything') |
| 201 | + expect(res.status).toBe(404) |
| 202 | + }) |
| 203 | + |
| 204 | + it('Should work with nested paths', async () => { |
| 205 | + app.use('/api/v1/*', routeCheck()) |
| 206 | + app.get('/api/v1/users/:id', (c) => c.text(`User ${c.req.param('id')}`)) |
| 207 | + |
| 208 | + const res1 = await app.request('/api/v1/users/123') |
| 209 | + expect(res1.status).toBe(200) |
| 210 | + expect(await res1.text()).toBe('User 123') |
| 211 | + |
| 212 | + const res2 = await app.request('/api/v1/posts/123') |
| 213 | + expect(res2.status).toBe(404) |
| 214 | + }) |
| 215 | + |
| 216 | + it('Should work when placed after some middleware', async () => { |
| 217 | + let middleware1Executed = false |
| 218 | + |
| 219 | + app.use('/admin/*', async (c, next) => { |
| 220 | + middleware1Executed = true |
| 221 | + await next() |
| 222 | + }) |
| 223 | + app.use('/admin/*', routeCheck()) |
| 224 | + app.get('/admin/dashboard', (c) => c.text('Admin Dashboard')) |
| 225 | + |
| 226 | + const res = await app.request('/admin/non-existent') |
| 227 | + expect(res.status).toBe(404) |
| 228 | + expect(middleware1Executed).toBe(true) |
| 229 | + }) |
| 230 | + }) |
| 231 | +}) |
0 commit comments