routing-controllers-openapi
Version:
Runtime OpenAPI v3 spec generation for routing-controllers
90 lines (77 loc) • 2.03 kB
text/typescript
import { ResponsesObject } from 'openapi3-ts'
import {
ContentType,
Controller,
Get,
getMetadataArgsStorage,
HttpCode,
JsonController,
} from 'routing-controllers'
import { getResponses, parseRoutes } from '../src'
describe('responses', () => {
let responses: ResponsesObject[]
beforeEach(() => {
getMetadataArgsStorage().reset()
// @ts-ignore: not referenced
class DefaultController {
getDefault() {
return
}
getCVS() {
return
}
}
// @ts-ignore: not referenced
class JSONController {
getDefault() {
return
}
getPlain() {
return
}
}
responses = parseRoutes(getMetadataArgsStorage()).map(getResponses)
})
it('sets success status code to 200 by default', () => {
expect(responses[0]).toMatchObject({ '200': expect.any(Object) })
expect(responses[2]).toMatchObject({ '200': expect.any(Object) })
})
it('overrides success status code as per @HttpCode decorator', () => {
expect(responses[1]).toMatchObject({ '201': expect.any(Object) })
expect(responses[3]).toMatchObject({ '204': expect.any(Object) })
})
it('sets default content type as per controller class type', () => {
expect(responses[0]).toMatchObject({
'200': {
content: { 'text/html; charset=utf-8': {} },
},
})
expect(responses[2]).toMatchObject({
'200': {
content: { 'application/json': {} },
},
})
})
it('overrides content type as per @ContentType decorator', () => {
expect(responses[1]).toMatchObject({
'201': {
content: { 'text/cvs': {} },
},
})
expect(responses[3]).toMatchObject({
'204': {
content: { 'text/plain': {} },
},
})
})
})