|
| 1 | +import { NestFastifyApplication } from '@nestjs/platform-fastify'; |
| 2 | +import { Test } from '@nestjs/testing'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import * as request from 'supertest'; |
| 5 | +import { FastifyModule } from '../src/fastify.module'; |
| 6 | + |
| 7 | +describe('Raw body (Fastify Application)', () => { |
| 8 | + let app: NestFastifyApplication; |
| 9 | + const body = '{ "amount":0.0 }'; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + const moduleFixture = await Test.createTestingModule({ |
| 13 | + imports: [FastifyModule], |
| 14 | + }).compile(); |
| 15 | + |
| 16 | + app = moduleFixture.createNestApplication<NestFastifyApplication>(null, { |
| 17 | + rawBody: true, |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should return exact post body', async () => { |
| 22 | + await app.init(); |
| 23 | + const response = await request(app.getHttpServer()) |
| 24 | + .post('/') |
| 25 | + .set('Content-Type', 'application/json') |
| 26 | + .set('Accept', 'application/json') |
| 27 | + .send(body) |
| 28 | + .expect(201); |
| 29 | + |
| 30 | + expect(response.body).to.eql({ |
| 31 | + parsed: { |
| 32 | + amount: 0, |
| 33 | + }, |
| 34 | + raw: '{ "amount":0.0 }', |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should work if post body is empty', async () => { |
| 39 | + await app.init(); |
| 40 | + await request(app.getHttpServer()) |
| 41 | + .post('/') |
| 42 | + .set('Content-Type', 'application/json') |
| 43 | + .expect(201); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(async () => { |
| 47 | + await app.close(); |
| 48 | + }); |
| 49 | +}); |
0 commit comments