Compare commits
2 Commits
d1c84cf6f6
...
a08adf3c03
| Author | SHA1 | Date | |
|---|---|---|---|
|
a08adf3c03
|
|||
|
2e594d09f5
|
@@ -52,6 +52,7 @@ export type RouterRequest<ReqExt> = ReqExt & {
|
||||
formData(): Promise<FormData>
|
||||
blob(): Promise<Blob>
|
||||
bearer(): string | undefined
|
||||
basic(): [string, string]
|
||||
cf?: IncomingRequestCfProperties
|
||||
}
|
||||
|
||||
@@ -395,7 +396,8 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
|
||||
json: async <T>(): Promise<T> => buffer.json ? buffer.json : buffer.json = await request.clone().json<T>(),
|
||||
formData: async (): Promise<FormData> => buffer.formData ? buffer.formData : buffer.formData = await request.clone().formData(),
|
||||
blob: async (): Promise<Blob> => buffer.blob ? buffer.blob : buffer.blob = await request.clone().blob(),
|
||||
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
|
||||
bearer: () => request.headers.get('Authorization')?.replace(/^[Bb]earer\s/, '').trim(),
|
||||
basic: () => (atob(request.headers.get('Authorization')?.replace(/^[Bb]asic\s/, '').trim() ?? '') || undefined)?.split(':')
|
||||
} as RouterRequest<ReqExt>
|
||||
|
||||
if (this.corsEnabled && req.method === 'OPTIONS') {
|
||||
|
||||
@@ -261,6 +261,21 @@ describe('Router', () => {
|
||||
|
||||
expect(await response.text()).toBe(testToken)
|
||||
})
|
||||
|
||||
test('basic', async () => {
|
||||
const router = new Router()
|
||||
const testCredentials = ['user', 'password']
|
||||
const testBasicAuth = btoa(testCredentials.join(':'))
|
||||
const testRequest = new Request('https://example.com/', { headers: { 'Authorization': `Basic ${testBasicAuth}` }})
|
||||
|
||||
router.get('/', ({ req }) => Response.json(req.basic()))
|
||||
|
||||
const response = await router.handle(testRequest, {})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
expect(await response.json()).toMatchObject(testCredentials)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Middleware', () => {
|
||||
|
||||
Reference in New Issue
Block a user