@@ -8,7 +8,6 @@ import { FastifyModule } from '../src/fastify.module';
88
99describe ( 'Raw body (Fastify Application)' , ( ) => {
1010 let app : NestFastifyApplication ;
11- const body = '{ "amount":0.0 }' ;
1211
1312 beforeEach ( async ( ) => {
1413 const moduleFixture = await Test . createTestingModule ( {
@@ -21,43 +20,79 @@ describe('Raw body (Fastify Application)', () => {
2120 rawBody : true ,
2221 } ,
2322 ) ;
24- } ) ;
2523
26- it ( 'should return exact post body' , async ( ) => {
2724 await app . init ( ) ;
28- const response = await app . inject ( {
29- method : 'POST' ,
30- url : '/' ,
31- headers : { 'content-type' : 'application/json' } ,
32- payload : body ,
25+ } ) ;
26+
27+ afterEach ( async ( ) => {
28+ await app . close ( ) ;
29+ } ) ;
30+
31+ describe ( 'application/json' , ( ) => {
32+ const body = '{ "amount":0.0 }' ;
33+
34+ it ( 'should return exact post body' , async ( ) => {
35+ const response = await app . inject ( {
36+ method : 'POST' ,
37+ url : '/' ,
38+ headers : { 'content-type' : 'application/json' } ,
39+ payload : body ,
40+ } ) ;
41+
42+ expect ( JSON . parse ( response . body ) ) . to . eql ( {
43+ parsed : {
44+ amount : 0 ,
45+ } ,
46+ raw : body ,
47+ } ) ;
3348 } ) ;
3449
35- expect ( JSON . parse ( response . body ) ) . to . eql ( {
36- parsed : {
37- amount : 0 ,
38- } ,
39- raw : '{ "amount":0.0 }' ,
50+ it ( 'should fail if post body is empty' , async ( ) => {
51+ const response = await app . inject ( {
52+ method : 'POST' ,
53+ url : '/' ,
54+ headers : {
55+ 'content-type' : 'application/json' ,
56+ accept : 'application/json' ,
57+ } ,
58+ } ) ;
59+
60+ // Unlike Express, when you send a POST request without a body
61+ // with Fastify, Fastify will throw an error because it isn't valid
62+ // JSON. See fastify/fastify#297.
63+ expect ( response . statusCode ) . to . equal ( 400 ) ;
4064 } ) ;
4165 } ) ;
4266
43- it ( 'should fail if post body is empty' , async ( ) => {
44- await app . init ( ) ;
45- const response = await app . inject ( {
46- method : 'POST' ,
47- url : '/' ,
48- headers : {
49- 'content-type' : 'application/json' ,
50- accept : 'application/json' ,
51- } ,
67+ describe ( 'application/x-www-form-urlencoded' , ( ) => {
68+ const body = 'content=this is a post\'s content by "Nest"' ;
69+
70+ it ( 'should return exact post body' , async ( ) => {
71+ const response = await app . inject ( {
72+ method : 'POST' ,
73+ url : '/' ,
74+ headers : { 'content-type' : 'application/x-www-form-urlencoded' } ,
75+ payload : body ,
76+ } ) ;
77+
78+ expect ( JSON . parse ( response . body ) ) . to . eql ( {
79+ parsed : {
80+ content : 'this is a post\'s content by "Nest"' ,
81+ } ,
82+ raw : body ,
83+ } ) ;
5284 } ) ;
5385
54- // Unlike Express, when you send a POST request without a body
55- // with Fastify, Fastify will throw an error because it isn't valid
56- // JSON. See fastify/fastify#297.
57- expect ( response . statusCode ) . to . equal ( 400 ) ;
58- } ) ;
86+ it ( 'should work if post body is empty' , async ( ) => {
87+ const response = await app . inject ( {
88+ method : 'POST' ,
89+ url : '/' ,
90+ headers : {
91+ 'content-type' : 'application/x-www-form-urlencoded' ,
92+ } ,
93+ } ) ;
5994
60- afterEach ( async ( ) => {
61- await app . close ( ) ;
95+ expect ( response . statusCode ) . to . equal ( 201 ) ;
96+ } ) ;
6297 } ) ;
6398} ) ;
0 commit comments