test(jwt): add tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by Yusuke Wada · parent 0e6bff6 · Browse files · 1 file·+28 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+28 added−4 removed
- tests:+28 added−4 removed
Provenance
- Commit
4a906a8- Tree
c682ff6- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
0e6bff6
1 file changed+28 added−4 removed
modified:src/middleware/jwt/index.test.ts+28 added-4 removed
10 unmodified lines11121314151516171819202122192023242526272829303132333435363738394041423 unmodified lines4647484950515253544 unmodified lines59606162636465536667686970717273747576777879808182838485868710 unmodified lines })
const app = new Hono()
app.use('/auth/*', jwt({ secret: 'a-secret' })) app.use('/*', async (c, next) => { await next() c.header('x-foo', c.get('x-foo') || '') })
app.use('/auth/*', jwt({ secret: 'a-secret' })) app.use('/auth-unicode/*', jwt({ secret: 'a-secret' }))
app.get('/auth/*', () => new Response('auth')) app.get('/auth-unicode/*', () => new Response('auth')) app.get('/auth/*', (c) => { c.set('x-foo', 'bar') return new Response('auth') }) app.get('/auth-unicode/*', (c) => { c.set('x-foo', 'bar') return new Response('auth') })
it('Should not authorize', async () => { const req = new Request('http://localhost/auth/a') const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(await res.text()).toBe('Unauthorized') expect(res.headers.get('x-foo')).toBeFalsy() })
it('Should authorize', async () => { const credential =3 unmodified lines const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth') expect(res.headers.get('x-foo')).toBe('bar') })
it('Should authorize Unicode', async () => { const credential =4 unmodified lines const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth') expect(res.headers.get('x-foo')).toBe('bar') })
it('Should authorize Unicode', async () => { it('Should not authorize Unicode', async () => { const invalidToken = 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'
const req = new Request('http://localhost/auth-unicode/a') req.headers.set('Authorization', `Basic ${invalidToken}`) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(res.headers.get('x-foo')).toBeFalsy() })
it('Should not authorize', async () => { const invalid_token = 'invalid token' const req = new Request('http://localhost/auth/a') req.headers.set('Authorization', `Bearer ${invalid_token}`) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(res.headers.get('x-foo')).toBeFalsy() })})