Skip to content

Commit

Permalink
Add tests to cover nested routers and body parameters (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Jun 16, 2023
2 parents 351e95a + 0bc74a2 commit cdda926
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/integration/nested-routers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'isomorphic-fetch'

import { OpenAPIRoute } from '../../src/route'
import { Path } from '../../src/parameters'
import { OpenAPIRouter } from '../../src/openapi'
import { buildRequest } from '../utils'

const innerRouter = OpenAPIRouter({ base: '/api/v1' })
class ToDoGet extends OpenAPIRoute {
static schema = {
tags: ['ToDo'],
summary: 'Get a single ToDo',
parameters: {
id: Path(Number),
},
responses: {
'200': {
schema: {
todo: {
lorem: String,
ipsum: String,
},
},
},
},
}

async handle(request: Request, env: any, context: any, data: Record<string, any>) {
return {
todo: {
lorem: 'lorem',
ipsum: 'ipsum',
},
}
}
}

innerRouter.get('/todo/:id', ToDoGet)
innerRouter.all('*', () => Response.json({ message: 'Not Found' }, { status: 404 }))

const router = OpenAPIRouter({
schema: {
info: {
title: 'Radar Worker API',
version: '1.0',
},
},
})

router.all('/api/v1/*', innerRouter)
router.all('*', () => new Response('Not Found.', { status: 404 }))

describe('innerRouter', () => {
it('simpleSuccessfulCall', async () => {
const request = await router.handle(buildRequest({ method: 'GET', path: `/api/v1/todo/1` }))
const resp = await request.json()

expect(request.status).toEqual(200)
expect(resp).toEqual({
todo: {
lorem: 'lorem',
ipsum: 'ipsum',
},
})
})

it('innerCatchAll', async () => {
const request = await router.handle(buildRequest({ method: 'GET', path: `/api/v1/asd` }))
const resp = await request.json()

expect(request.status).toEqual(404)
expect(resp).toEqual({ message: 'Not Found' })
})

it('outerCatchAll', async () => {
const request = await router.handle(buildRequest({ method: 'GET', path: `/asd` }))
const resp = await request.text()

expect(request.status).toEqual(404)
expect(resp).toEqual('Not Found.')
})
})
85 changes: 85 additions & 0 deletions tests/integration/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,88 @@ describe('queryParametersValidation', () => {
expect(resp.errors.p_optional).toBeUndefined()
})
})

describe('bodyParametersValidation', () => {
test('requiredFieldTitle', async () => {
const request = await todoRouter.handle(
buildRequest({
method: 'POST',
path: '/todos',
json: () => {
return {}
},
})
)
const resp = await request.json()

expect(request.status).toEqual(400)

// the current body implementation only validates 1 field at time
expect(resp.errors['body.title']).toEqual('is required')
})

test('requiredFieldTipe', async () => {
const request = await todoRouter.handle(
buildRequest({
method: 'POST',
path: '/todos',
json: () => {
return {
title: 'my todo',
}
},
})
)
const resp = await request.json()

expect(request.status).toEqual(400)

// the current body implementation only validates 1 field at time
expect(resp.errors['body.type']).toEqual('is required')
})

test('validRequest', async () => {
const request = await todoRouter.handle(
buildRequest({
method: 'POST',
path: '/todos',
json: () => {
return {
title: 'my todo',
type: 'nextWeek',
}
},
}),
{},
{}
)
const resp = await request.json()

expect(request.status).toEqual(200)

expect(resp).toEqual({ todo: { title: 'my todo', type: 'nextWeek' } })
})

test('validRequestWithOptionalParameters', async () => {
const request = await todoRouter.handle(
buildRequest({
method: 'POST',
path: '/todos',
json: () => {
return {
title: 'my todo',
description: 'this will be done',
type: 'nextWeek',
}
},
}),
{},
{}
)
const resp = await request.json()

expect(request.status).toEqual(200)

expect(resp).toEqual({ todo: { title: 'my todo', description: 'this will be done', type: 'nextWeek' } })
})
})
35 changes: 35 additions & 0 deletions tests/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,44 @@ export class ToDoGet extends OpenAPIRoute {
}
}

export class ToDoCreate extends OpenAPIRoute {
static schema = {
tags: ['ToDo'],
summary: 'Create a new ToDo',
requestBody: {
title: String,
description: new Str({ required: false }),
type: new Enumeration({
values: {
nextWeek: 'nextWeek',
nextMonth: 'nextMonth',
},
}),
},
responses: {
'200': {
schema: {
todo: {
title: 'My new todo',
description: 'This really needs to be done next week',
type: 'nextWeek',
},
},
},
},
}

async handle(request: Request, env: any, context: any, data: Record<string, any>) {
return {
todo: data.body,
}
}
}

export const todoRouter = OpenAPIRouter()
todoRouter.get('/todos', ToDoList)
todoRouter.get('/todos/:id', ToDoGet)
todoRouter.post('/todos', ToDoCreate)

// 404 for everything else
todoRouter.all('*', () => new Response('Not Found.', { status: 404 }))

0 comments on commit cdda926

Please sign in to comment.