-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(platform-express) respect existing parser middlewares when using …
…Express 5 Express 5 made the router public API again and renamed the field from app._router to app.router. This broke the detection mechanism whether a middleware named "jsonParser" or "urlencodedParser" is already registered or not. Unfortunately, #14574 only fixed the issue partially. This commit now uses app.router everywhere. To avoid future regressions a test was added to verify the expected behavior.
- Loading branch information
Showing
2 changed files
with
47 additions
and
1 deletion.
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
46 changes: 46 additions & 0 deletions
46
packages/platform-express/test/adapters/express-adapter.spec.ts
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,46 @@ | ||
import { ExpressAdapter } from '@nestjs/platform-express'; | ||
import { expect } from 'chai'; | ||
import * as express from 'express'; | ||
import * as sinon from 'sinon'; | ||
|
||
afterEach(() => sinon.restore()); | ||
|
||
describe.only('ExpressAdapter', () => { | ||
describe('registerParserMiddleware', () => { | ||
it('should register the express built-in parsers for json and urlencoded payloads', () => { | ||
const expressInstance = express(); | ||
const jsonParserInstance = express.json(); | ||
const urlencodedInstance = express.urlencoded(); | ||
const jsonParserSpy = sinon | ||
.stub(express, 'json') | ||
.returns(jsonParserInstance); | ||
const urlencodedParserSpy = sinon | ||
.stub(express, 'urlencoded') | ||
.returns(urlencodedInstance); | ||
const useSpy = sinon.spy(expressInstance, 'use'); | ||
const expressAdapter = new ExpressAdapter(expressInstance); | ||
|
||
expressAdapter.registerParserMiddleware(); | ||
|
||
expect(useSpy.calledTwice).to.be.true; | ||
expect(useSpy.calledWith(sinon.match.same(jsonParserInstance))).to.be | ||
.true; | ||
expect(useSpy.calledWith(sinon.match.same(urlencodedInstance))).to.be | ||
.true; | ||
expect(jsonParserSpy.calledOnceWith({})).to.be.true; | ||
expect(urlencodedParserSpy.calledOnceWith({ extended: true })).to.be.true; | ||
}); | ||
|
||
it('should not register default parsers if custom parsers have already been registered', () => { | ||
const expressInstance = express(); | ||
expressInstance.use(function jsonParser() {}); | ||
expressInstance.use(function urlencodedParser() {}); | ||
const useSpy = sinon.spy(expressInstance, 'use'); | ||
const expressAdapter = new ExpressAdapter(expressInstance); | ||
|
||
expressAdapter.registerParserMiddleware(); | ||
|
||
expect(useSpy.called).to.be.false; | ||
}); | ||
}); | ||
}); |