Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: multi spec tests #552

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions test/api.v3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API
termsOfService: http://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
paths:
/users:
get:
description: |
Returns all pets
operationId: findUser
parameters:
- name: type
in: query
description: maximum number of results to return
required: true
schema:
type: string
enum:
- dog
- cat
- name: tags
in: query
description: tags to filter by
required: false
style: form
schema:
type: array
items:
type: string
- name: limit
in: query
description: maximum number of results to return
required: true
schema:
type: integer
format: int32
minimum: 1
maximum: 20
responses:
'200':
description: pet response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

components:
schemas:
Pet:
required:
- id
- name
- type
properties:
id:
readOnly: true
type: number
name:
type: string
tag:
type: string
type:
$ref: '#/components/schemas/PetType'

PetType:
type: string
enum:
- dog
- cat

Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
75 changes: 75 additions & 0 deletions test/api.v4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API
termsOfService: http://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
paths:
/pets:
get:
description: |
Returns all pets
operationId: findPets
parameters:
- name: pet_type
in: query
description: maximum number of results to return
required: true
schema:
type: string
enum:
- doggy
- kitty
responses:
'200':
description: pet response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

components:
schemas:
Pet:
required:
- id
- name
- type
properties:
pet_id:
readOnly: true
type: number
pet_name:
type: string
pet_tag:
type: string
-pet_type:
$ref: '#/components/schemas/PetType'

PetType:
type: string
enum:
- doggy
- kitty

Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
104 changes: 104 additions & 0 deletions test/multi.spec.operation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { expect } from 'chai';
import * as request from 'supertest';

describe('multi-spec', () => {
let app = null;

before(async () => {
// Set up the express app
app = createServer();
});

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

it('should return users', async () => request(app).get(`/users`).expect(400));

it('should return pets', async () => request(app).get(`/pets`).expect(400));
});

function createServer() {
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const http = require('http');
const OpenApiValidator = require('../src');

const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text());
app.use(bodyParser.json());

const versions = [3, 4];

for (const v of versions) {
const apiSpec = path.join(__dirname, `api.v${v}.yaml`);
app.use(
OpenApiValidator.middleware({
apiSpec,
}),
);

routes(app, v);
}

const server = http.createServer(app);
server.listen(3000);
console.log('Listening on port 3000');

function routes(app, v) {
routesUsers(app);
routesPets(app);
}

function routesUsers(app) {
app.post(`/users`, (req, res, next) => {
res.json({ ...req.body });
});
app.get(`/users`, (req, res, next) => {
res.json([
{
id: 1,
name: 'happy',
type: 'cat',
},
]);
});

app.use((err, req, res, next) => {
// format error
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
}

function routesPets(app) {
app.get('/pets', (req, res, next) => {
res.json([
{
pet_id: 1,
pet_name: 'happy',
pet_type: 'kitty',
},
]);
});

app.post('/pets', (req, res, next) => {
res.json({ ...req.body });
});

app.use((err, req, res, next) => {
// format error
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
}

app.server = server;
return app;
}