-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carmine DiMascio
committed
Aug 17, 2019
1 parent
e2746d7
commit 3126ef9
Showing
4 changed files
with
142 additions
and
2 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,71 @@ | ||
import * as path from 'path'; | ||
import * as express from 'express'; | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
import { createApp } from './common/app'; | ||
|
||
const packageJson = require('../package.json'); | ||
|
||
describe.only(packageJson.name, () => { | ||
let app = null; | ||
let basePath = null; | ||
|
||
before(async () => { | ||
// Set up the express app | ||
const apiSpec = path.join('test', 'resources', 'coercion.yaml'); | ||
app = await createApp({ apiSpec }, 3005); | ||
basePath = app.basePath; | ||
|
||
// Define new coercion routes | ||
app.use( | ||
`${basePath}/coercion`, | ||
express | ||
.Router() | ||
.post(`/pets`, (req, res) => res.json(req.body)) | ||
.post(`/pets_string_boolean`, (req, res) => res.json(req.body)), | ||
); | ||
}); | ||
|
||
after(() => { | ||
app.server.close(); | ||
}); | ||
|
||
it('should coerce is_cat to boolean since it is defined as a boolean in the spec', async () => | ||
request(app) | ||
.post(`${basePath}/coercion/pets`) | ||
.send({ | ||
name: 'test', | ||
is_cat: 'true', | ||
}) | ||
.expect(200) | ||
.then(r => { | ||
expect(r.body.is_cat).to.be.a('boolean'); | ||
expect(r.body.is_cat).to.be.equal(true); | ||
})); | ||
|
||
it('should keep is_cat as boolean', async () => | ||
request(app) | ||
.post(`${basePath}/coercion/pets`) | ||
.send({ | ||
name: 'test', | ||
is_cat: true, | ||
}) | ||
.expect(200) | ||
.then(r => { | ||
expect(r.body.is_cat).to.be.a('boolean'); | ||
expect(r.body.is_cat).to.be.equal(true); | ||
})); | ||
|
||
it('should coerce a is_cat from boolean to string since it is defined as such in the spec', async () => | ||
request(app) | ||
.post(`${basePath}/coercion/pets_string_boolean`) | ||
.send({ | ||
name: 'test', | ||
is_cat: true, | ||
}) | ||
.expect(200) | ||
.then(r => { | ||
expect(r.body.is_cat).to.be.a('string'); | ||
expect(r.body.is_cat).to.be.equal('true'); | ||
})); | ||
}); |
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
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,67 @@ | ||
openapi: '3.0.1' | ||
info: | ||
version: 1.0.0 | ||
title: Swagger Petstore | ||
description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification | ||
|
||
servers: | ||
- url: /v1/ | ||
|
||
paths: | ||
/coercion/pets: | ||
post: | ||
description: Creates a new pet in the store. Duplicates are allowed | ||
operationId: addPet | ||
requestBody: | ||
description: Pet to add to the store | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Pet' | ||
responses: | ||
'200': | ||
description: pet response | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Pet' | ||
|
||
/coercion/pets_string_boolean: | ||
post: | ||
description: Creates a new pet in the store. Duplicates are allowed | ||
operationId: addPet | ||
requestBody: | ||
description: Pet to add to the store | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/PetStringBoolean' | ||
responses: | ||
'200': | ||
description: pet response | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/PetStringBoolean' | ||
|
||
components: | ||
schemas: | ||
Pet: | ||
required: | ||
- name | ||
properties: | ||
name: | ||
type: string | ||
is_cat: | ||
type: boolean | ||
|
||
PetStringBoolean: | ||
required: | ||
- name | ||
properties: | ||
name: | ||
type: string | ||
is_cat: | ||
type: string |