mirror of
https://github.com/honojs/hono.git
synced 2024-11-24 19:26:56 +01:00
refactor: parse headers and params using Headers and URLSearchParams
This commit is contained in:
parent
b6aee690c2
commit
76f5e875c9
@ -149,6 +149,48 @@ describe('createRequest', () => {
|
|||||||
const text = await request.text()
|
const text = await request.text()
|
||||||
expect(text).toEqual('Request Body')
|
expect(text).toEqual('Request Body')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Should handle escaped paths in request URL correctly', () => {
|
||||||
|
const event: AlibabaCloudFC3Event = {
|
||||||
|
version: 'v1',
|
||||||
|
rawPath: '/my/path%E4%BD%A0%E5%A5%BD',
|
||||||
|
headers: {
|
||||||
|
Accept: '*/*',
|
||||||
|
'User-Agent': 'curl/7.81.0',
|
||||||
|
},
|
||||||
|
queryParameters: {
|
||||||
|
你好: '世界',
|
||||||
|
},
|
||||||
|
body: '',
|
||||||
|
isBase64Encoded: true,
|
||||||
|
requestContext: {
|
||||||
|
accountId: '1234567890123456',
|
||||||
|
domainName: 'hono-al-fc-test.us-east-1.fcapp.run',
|
||||||
|
domainPrefix: 'hono-al-fc-test',
|
||||||
|
requestId: '1-12345678-12345678-123456789012',
|
||||||
|
time: '2024-11-07T07:52:56Z',
|
||||||
|
timeEpoch: '1730965976961',
|
||||||
|
http: {
|
||||||
|
method: 'GET',
|
||||||
|
path: '/my/path你好',
|
||||||
|
protocol: 'HTTP/1.1',
|
||||||
|
sourceIp: '1.2.3.4',
|
||||||
|
userAgent: 'curl/7.81.0',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const request = createRequest(event)
|
||||||
|
|
||||||
|
expect(request.method).toEqual('GET')
|
||||||
|
const requestUrl = new URL(request.url)
|
||||||
|
expect(requestUrl.pathname).toEqual('/my/path%E4%BD%A0%E5%A5%BD')
|
||||||
|
expect(requestUrl.searchParams.get('你好')).toEqual('世界')
|
||||||
|
expect(Object.fromEntries(request.headers)).toEqual({
|
||||||
|
accept: '*/*',
|
||||||
|
'user-agent': 'curl/7.81.0',
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('createResponse', () => {
|
describe('createResponse', () => {
|
||||||
|
@ -31,20 +31,11 @@ export const handle = <E extends Env = Env, S extends Schema = {}, BasePath exte
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const createRequest = (event: AlibabaCloudFC3Event): Request => {
|
export const createRequest = (event: AlibabaCloudFC3Event): Request => {
|
||||||
const queryString = Object.entries(event.queryParameters || {})
|
const url = new URL(`https://${event.requestContext.domainName}${event.rawPath}`)
|
||||||
.map(([key, value]) => `${key}=${value}`)
|
|
||||||
.join('&')
|
|
||||||
|
|
||||||
const url = `https://${event.requestContext.domainName}${event.rawPath}${
|
const params = new URLSearchParams(event.queryParameters)
|
||||||
queryString ? `?${queryString}` : ''
|
url.search = params.toString()
|
||||||
}`
|
const headers = new Headers(event.headers)
|
||||||
|
|
||||||
const headers = new Headers()
|
|
||||||
if (event.headers) {
|
|
||||||
for (const [key, value] of Object.entries(event.headers)) {
|
|
||||||
headers.set(key, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const method = event.requestContext.http.method
|
const method = event.requestContext.http.method
|
||||||
const requestInit: RequestInit = {
|
const requestInit: RequestInit = {
|
||||||
|
Loading…
Reference in New Issue
Block a user