-
-
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.
feat: Add Allow Header on 405 (#560)
* chore: Fix Typos * test: Add Test Case for Allow Header The Allow header must be sent if the server responds with 405 to indicate which request methods can be used. See #467 * feat: Add Allow Header for 405 - Method Not Allowed Adds Allow header to 405 error. This is required by RFC 7231 and indicates which request methods can be used. Resolves #467 * doc: Adjust Error Handler in NestJS Example When using a custom error handler like in the NestJS example, one needs to set the headers on the response explicitly. The same is true when using a custom express error handler.
- Loading branch information
Showing
8 changed files
with
127 additions
and
29 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
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
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
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,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' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} |