Skip to content

Commit 8ea6337

Browse files
committed
test(platform-fastify): add tests for raw body option in fastify adapter
1 parent c8bd058 commit 8ea6337

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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({ raw: '{ "amount":0.0 }' });
31+
});
32+
33+
afterEach(async () => {
34+
await app.close();
35+
});
36+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Controller, Post, Req } from '@nestjs/common';
2+
import { FastifyRequest } from 'fastify';
3+
4+
@Controller()
5+
export class FastifyController {
6+
@Post()
7+
getRawBody(@Req() req: FastifyRequest) {
8+
return { raw: req.rawBody.toString() };
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { FastifyController } from './fastify.controller';
3+
4+
@Module({
5+
controllers: [FastifyController],
6+
})
7+
export class FastifyModule {}

0 commit comments

Comments
 (0)