Skip to content

Commit

Permalink
add coercion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Aug 17, 2019
1 parent e2746d7 commit 3126ef9
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
71 changes: 71 additions & 0 deletions test/coercion.spec.ts
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');
}));
});
4 changes: 3 additions & 1 deletion test/common/app.common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as http from 'http'
import * as http from 'http';
import * as express from 'express';
const BASE_PATH = '/v1';

Expand Down Expand Up @@ -45,11 +45,13 @@ export function routes(app) {
app.get(`${basePath}/pets`, function(req, res, next) {
res.json({
test: 'hi',
...req.body,
});
});

app.post(`${basePath}/pets`, function(req, res, next) {
res.json({
...req.body,
id: 'new-id',
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/common/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function createApp(opts?: any, port: number = 3000) {

// Register error handler
app.use((err, req, res, next) => {
res.status(err.status).json({
res.status(err.status || 500).json({
errors: err.errors,
});
});
Expand Down
67 changes: 67 additions & 0 deletions test/resources/coercion.yaml
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

0 comments on commit 3126ef9

Please sign in to comment.