|
| 1 | +import { ExpressAdapter } from '@nestjs/platform-express'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import * as express from 'express'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | + |
| 6 | +afterEach(() => sinon.restore()); |
| 7 | + |
| 8 | +describe.only('ExpressAdapter', () => { |
| 9 | + describe('registerParserMiddleware', () => { |
| 10 | + it('should register the express built-in parsers for json and urlencoded payloads', () => { |
| 11 | + const expressInstance = express(); |
| 12 | + const jsonParserInstance = express.json(); |
| 13 | + const urlencodedInstance = express.urlencoded(); |
| 14 | + const jsonParserSpy = sinon |
| 15 | + .stub(express, 'json') |
| 16 | + .returns(jsonParserInstance); |
| 17 | + const urlencodedParserSpy = sinon |
| 18 | + .stub(express, 'urlencoded') |
| 19 | + .returns(urlencodedInstance); |
| 20 | + const useSpy = sinon.spy(expressInstance, 'use'); |
| 21 | + const expressAdapter = new ExpressAdapter(expressInstance); |
| 22 | + |
| 23 | + expressAdapter.registerParserMiddleware(); |
| 24 | + |
| 25 | + expect(useSpy.calledTwice).to.be.true; |
| 26 | + expect(useSpy.calledWith(sinon.match.same(jsonParserInstance))).to.be |
| 27 | + .true; |
| 28 | + expect(useSpy.calledWith(sinon.match.same(urlencodedInstance))).to.be |
| 29 | + .true; |
| 30 | + expect(jsonParserSpy.calledOnceWith({})).to.be.true; |
| 31 | + expect(urlencodedParserSpy.calledOnceWith({ extended: true })).to.be.true; |
| 32 | + }); |
| 33 | + |
| 34 | + it('should not register default parsers if custom parsers have already been registered', () => { |
| 35 | + const expressInstance = express(); |
| 36 | + expressInstance.use(function jsonParser() {}); |
| 37 | + expressInstance.use(function urlencodedParser() {}); |
| 38 | + const useSpy = sinon.spy(expressInstance, 'use'); |
| 39 | + const expressAdapter = new ExpressAdapter(expressInstance); |
| 40 | + |
| 41 | + expressAdapter.registerParserMiddleware(); |
| 42 | + |
| 43 | + expect(useSpy.called).to.be.false; |
| 44 | + }); |
| 45 | + }); |
| 46 | +}); |
0 commit comments