test(zod-openapi): additional headers test for `Authorization` (#192)
* include test for array of headers * merge route and routeWithAuth from 3 tests into 2 testspull/201/head
parent
1d316c188f
commit
d2398a4c90
|
@ -37,7 +37,6 @@
|
|||
"zod": "3.*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@hono/zod-validator": "^0.1.9",
|
||||
"hono": "^3.7.2",
|
||||
"zod": "^3.22.1"
|
||||
},
|
||||
|
|
|
@ -263,18 +263,20 @@ describe('Query', () => {
|
|||
|
||||
describe('Header', () => {
|
||||
const HeaderSchema = z.object({
|
||||
authorization: z.string(),
|
||||
'x-request-id': z.string().uuid(),
|
||||
})
|
||||
|
||||
const PingSchema = z
|
||||
const PongSchema = z
|
||||
.object({
|
||||
'x-request-id': z.string().uuid(),
|
||||
authorization: z.string(),
|
||||
})
|
||||
.openapi('Post')
|
||||
|
||||
const route = createRoute({
|
||||
method: 'get',
|
||||
path: '/ping',
|
||||
path: '/pong',
|
||||
request: {
|
||||
headers: HeaderSchema,
|
||||
},
|
||||
|
@ -282,40 +284,42 @@ describe('Header', () => {
|
|||
200: {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: PingSchema,
|
||||
schema: PongSchema,
|
||||
},
|
||||
},
|
||||
description: 'Ping',
|
||||
description: 'Pong',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const app = new OpenAPIHono()
|
||||
|
||||
app.openapi(route, (c) => {
|
||||
const controller = (c) => {
|
||||
const headerData = c.req.valid('header')
|
||||
const xRequestId = headerData['x-request-id']
|
||||
return c.jsonT({
|
||||
'x-request-id': xRequestId,
|
||||
})
|
||||
})
|
||||
return c.jsonT(headerData)
|
||||
}
|
||||
|
||||
app.openapi(route, controller)
|
||||
|
||||
it('Should return 200 response with correct contents', async () => {
|
||||
const res = await app.request('/ping', {
|
||||
const res = await app.request('/pong', {
|
||||
headers: {
|
||||
'x-request-id': '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b',
|
||||
Authorization: 'Bearer helloworld',
|
||||
},
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({
|
||||
'x-request-id': '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b',
|
||||
'authorization': 'Bearer helloworld',
|
||||
})
|
||||
})
|
||||
|
||||
it('Should return 400 response with correct contents', async () => {
|
||||
const res = await app.request('/ping', {
|
||||
const res = await app.request('/pong', {
|
||||
headers: {
|
||||
'x-request-id': 'invalid-strings',
|
||||
Authorization: 'Bearer helloworld',
|
||||
},
|
||||
})
|
||||
expect(res.status).toBe(400)
|
||||
|
|
|
@ -1765,11 +1765,16 @@
|
|||
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f"
|
||||
integrity sha512-1P1OROm/rdubP5aFDSZQILU0vrLCJ4fvHt6EoqHEM+2D/G5MK3bIaymUKLit8Js9gbns5UyJnkP/TZROLw4tUA==
|
||||
|
||||
"@sinclair/typebox@^0.25.16", "@sinclair/typebox@^0.25.24":
|
||||
"@sinclair/typebox@^0.25.16":
|
||||
version "0.25.24"
|
||||
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.25.24.tgz#8c7688559979f7079aacaf31aa881c3aa410b718"
|
||||
integrity sha512-XJfwUVUKDHF5ugKwIcxEgc9k8b7HbznCp6eUfWgu710hMPNIO4aw4/zB5RogDQz8nd6gyCDpU9O/m6qYEWY6yQ==
|
||||
|
||||
"@sinclair/typebox@^0.31.15":
|
||||
version "0.31.17"
|
||||
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.31.17.tgz#f9ceed480957b919b203bb0b3e27bc559d1e8e19"
|
||||
integrity sha512-GVYVHHOGINx+DT2DwjXoCQO0mRpztYKyb3d48tucuqhjhHpQYGp7Xx7dhhQGzILx/beuBrzfITMC7/5X7fw+UA==
|
||||
|
||||
"@sindresorhus/is@^0.14.0":
|
||||
version "0.14.0"
|
||||
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"
|
||||
|
|
Loading…
Reference in New Issue