File tree Expand file tree Collapse file tree
integration/nest-application/raw-body Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ import { Module } from '@nestjs/common' ;
2+ import { FastifyController } from './fastify.controller' ;
3+
4+ @Module ( {
5+ controllers : [ FastifyController ] ,
6+ } )
7+ export class FastifyModule { }
You can’t perform that action at this time.
0 commit comments