Skip to content

Commit

Permalink
test: Add Test Case for Allow Header
Browse files Browse the repository at this point in the history
The Allow header must be sent if the server responds with 405 to
indicate which request methods can be used.

See cdimascio#467
  • Loading branch information
ahilke committed Mar 12, 2021
1 parent b15e796 commit 207f0dd
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions test/allow.header.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { expect } from 'chai';
import * as express from 'express';
import { Server } from 'http';
import * as request from 'supertest';
import * as packageJson from '../package.json';
import * as OpenApiValidator from '../src';
import { OpenAPIV3 } from '../src/framework/types';
import { startServer } from './common/app.common';

describe(packageJson.name, () => {
let app = null;

before(async () => {
app = await createApp();
});

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

it('adds allow header to 405 - Method Not Allowed', async () =>
request(app)
.put('/v1/pets/greebo')
.expect(405)
.then((response) => {
expect(response.header).to.include({ allow: 'POST, GET' });
}));
});

async function createApp(): Promise<express.Express & { server?: Server }> {
const app = express();

app.use(
OpenApiValidator.middleware({
apiSpec: createApiSpec(),
validateRequests: true,
}),
);
app.use(
express
.Router()
.get('/v1/pets/:petId', () => ['cat', 'dog'])
.post('/v1/pets/:petId', (req, res) => res.json(req.body)),
);

await startServer(app, 3001);
return app;
}

function createApiSpec(): OpenAPIV3.Document {
return {
openapi: '3.0.3',
info: {
title: 'Petstore API',
version: '1.0.0',
},
servers: [
{
url: '/v1/',
},
],
paths: {
'/pets/{petId}': {
parameters: [
{
in: 'path',
name: 'petId',
required: true,
schema: { type: 'string' },
},
],
get: {
responses: {
'200': { description: 'GET Pet' },
},
},
post: {
responses: {
'200': { description: 'POST Pet' },
},
},
},
},
};
}

0 comments on commit 207f0dd

Please sign in to comment.