-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to cover nested routers and body parameters (#62)
- Loading branch information
Showing
3 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters