-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(open_api3.1): ensure that methods with non-explicit semantics al…
…low request body
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
test/openapi_3.1/non_defined_semantics_request_body.spec.ts
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,48 @@ | ||
import * as request from 'supertest'; | ||
import * as express from 'express'; | ||
import { createApp } from "../common/app"; | ||
import { join } from "path"; | ||
|
||
describe('Request body in operations without well defined semantics - OpenAPI 3.1', () => { | ||
let app; | ||
|
||
before(async () => { | ||
const apiSpec = join('test', 'openapi_3.1', 'resources', 'non_defined_semantics_request_body.yaml'); | ||
app = await createApp( | ||
{ apiSpec, validateRequests: true, validateResponses: true }, | ||
3005, | ||
(app) => app.use( | ||
express | ||
.Router() | ||
.get(`/v1/entity`, (req, res) => | ||
res.status(200).json({ | ||
property: null | ||
}), | ||
), | ||
) | ||
); | ||
}); | ||
|
||
after(() => { | ||
app.server.close(); | ||
}); | ||
|
||
// In OpenAPI 3.0, methods that RFC7231 does not have explicitly defined semantics for request body (GET, HEAD, DELETE) do not allow request body | ||
// In OpenAPI 3.1, request body is allowed for these methods. This test ensures that GET it is correctly handled | ||
it('should validate a request body on GET', async () => { | ||
return request(app) | ||
.get(`${app.basePath}/entity`) | ||
.set('Content-Type', 'application/json') | ||
.send({request: 123}) | ||
.expect(400); | ||
}); | ||
|
||
// Ensures that DELETE it is correctly handled | ||
it('should validate a request body on DELETE', async () => { | ||
return request(app) | ||
.delete(`${app.basePath}/entity`) | ||
.set('Content-Type', 'application/json') | ||
.send({request: 123}) | ||
.expect(400); | ||
}); | ||
}) |
55 changes: 55 additions & 0 deletions
55
test/openapi_3.1/resources/non_defined_semantics_request_body.yaml
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,55 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: API | ||
version: 1.0.0 | ||
servers: | ||
- url: /v1 | ||
components: | ||
schemas: | ||
EntityRequest: | ||
type: object | ||
properties: | ||
request: | ||
type: string | ||
paths: | ||
/entity: | ||
get: | ||
description: GETS my entity | ||
requestBody: | ||
description: Request body for entity | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/EntityRequest' | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
title: Entity | ||
type: object | ||
properties: | ||
property: | ||
type: ['string', 'null'] | ||
delete: | ||
description: DELETE my entity | ||
requestBody: | ||
description: Request body for entity | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/EntityRequest' | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
title: Entity | ||
type: object | ||
properties: | ||
property: | ||
type: ['string', 'null'] |