test(zod-openapi): additional headers test for `Authorization` (#192)

* include test for array of headers

* merge route and routeWithAuth from 3 tests into 2 tests
pull/201/head
Callum 2023-10-10 19:45:05 +08:00 committed by GitHub
parent 1d316c188f
commit d2398a4c90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -37,7 +37,6 @@
"zod": "3.*" "zod": "3.*"
}, },
"devDependencies": { "devDependencies": {
"@hono/zod-validator": "^0.1.9",
"hono": "^3.7.2", "hono": "^3.7.2",
"zod": "^3.22.1" "zod": "^3.22.1"
}, },

View File

@ -263,18 +263,20 @@ describe('Query', () => {
describe('Header', () => { describe('Header', () => {
const HeaderSchema = z.object({ const HeaderSchema = z.object({
authorization: z.string(),
'x-request-id': z.string().uuid(), 'x-request-id': z.string().uuid(),
}) })
const PingSchema = z const PongSchema = z
.object({ .object({
'x-request-id': z.string().uuid(), 'x-request-id': z.string().uuid(),
authorization: z.string(),
}) })
.openapi('Post') .openapi('Post')
const route = createRoute({ const route = createRoute({
method: 'get', method: 'get',
path: '/ping', path: '/pong',
request: { request: {
headers: HeaderSchema, headers: HeaderSchema,
}, },
@ -282,40 +284,42 @@ describe('Header', () => {
200: { 200: {
content: { content: {
'application/json': { 'application/json': {
schema: PingSchema, schema: PongSchema,
}, },
}, },
description: 'Ping', description: 'Pong',
}, },
}, },
}) })
const app = new OpenAPIHono() const app = new OpenAPIHono()
app.openapi(route, (c) => { const controller = (c) => {
const headerData = c.req.valid('header') const headerData = c.req.valid('header')
const xRequestId = headerData['x-request-id'] return c.jsonT(headerData)
return c.jsonT({ }
'x-request-id': xRequestId,
}) app.openapi(route, controller)
})
it('Should return 200 response with correct contents', async () => { it('Should return 200 response with correct contents', async () => {
const res = await app.request('/ping', { const res = await app.request('/pong', {
headers: { headers: {
'x-request-id': '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b', 'x-request-id': '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b',
Authorization: 'Bearer helloworld',
}, },
}) })
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(await res.json()).toEqual({ expect(await res.json()).toEqual({
'x-request-id': '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b', 'x-request-id': '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b',
'authorization': 'Bearer helloworld',
}) })
}) })
it('Should return 400 response with correct contents', async () => { it('Should return 400 response with correct contents', async () => {
const res = await app.request('/ping', { const res = await app.request('/pong', {
headers: { headers: {
'x-request-id': 'invalid-strings', 'x-request-id': 'invalid-strings',
Authorization: 'Bearer helloworld',
}, },
}) })
expect(res.status).toBe(400) expect(res.status).toBe(400)

View File

@ -1765,11 +1765,16 @@
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f" resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f"
integrity sha512-1P1OROm/rdubP5aFDSZQILU0vrLCJ4fvHt6EoqHEM+2D/G5MK3bIaymUKLit8Js9gbns5UyJnkP/TZROLw4tUA== 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" version "0.25.24"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.25.24.tgz#8c7688559979f7079aacaf31aa881c3aa410b718" resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.25.24.tgz#8c7688559979f7079aacaf31aa881c3aa410b718"
integrity sha512-XJfwUVUKDHF5ugKwIcxEgc9k8b7HbznCp6eUfWgu710hMPNIO4aw4/zB5RogDQz8nd6gyCDpU9O/m6qYEWY6yQ== 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": "@sindresorhus/is@^0.14.0":
version "0.14.0" version "0.14.0"
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea" resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea"