Skip to content

Commit

Permalink
test: add test for default response >=400
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Jan 17, 2021
1 parent bd01be4 commit 90061b5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/resources/response.validation.defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Sample
servers:
- url: /v1
paths:
/default_inline:
get:
parameters:
- name: q
in: 'query'
required: true
schema:
type: string
responses:
'200':
description: Success
content:
'application/json':
schema:
type: object
properties:
data:
description: Some data
type: string
required:
- data
default:
description: Unexpected error
content:
'application/json':
schema:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
56 changes: 56 additions & 0 deletions test/response.validation.defaults.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as path from 'path';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const apiSpecPath = path.join(
'test',
'resources',
'response.validation.defaults.yaml',
);

describe('response validation with type coercion', () => {
let app = null;

before(async () => {
// set up express app
app = await createApp(
{
apiSpec: apiSpecPath,
validateResponses: true,
},
3005,
(app) => {
app.get(`${app.basePath}/default_inline`, (req, res) => {
const q = req.query.q;

if (q === '200') {
return res.status(200).json({ data: 'good' });
} else if (q === '400') {
return res.status(400).json({ message: 'message', code: 400 });
} else if (q === '400_bad') {
return res.status(400).json({ bad: 'malformed' });
}
});
},
);
});

after(() => {
app.server.close();
});

it('should validate 200 using explicit response', async () =>
request(app).get(`${app.basePath}/default_inline?q=200`).expect(200));

it('should validate undeclared 400 using default response', async () =>
request(app).get(`${app.basePath}/default_inline?q=400`).expect(400));

it('should validate undeclared 400 using default response', async () =>
request(app)
.get(`${app.basePath}/default_inline?q=400_bad`)
.expect(500)
.then((r) => {
expect(r.body.message).to.include('should have required property');
}));
});

0 comments on commit 90061b5

Please sign in to comment.