@@ -156,14 +156,15 @@ const APIModelObject = {
156156 }]
157157 },
158158 " request_body_equal" : { // this property will work with PUT, POST, DELETE, PATCH only
159- " status" : 404 ,
160- " not_equal_response" : { // this field is optional, default is {"data": "invalid request"}
161- " success" : false
162- },
163- " expected_body" : { // request body should equal expected_body property
164- " username" : " test_" ,
165- " password" : " test_pass"
166- }
159+ " allow_partial_request_body" : true , // allow send not all keys and get success response
160+ " status" : 404 ,
161+ " not_equal_response" : { // this field is optional, default is {"data": "invalid request"}
162+ " success" : false
163+ },
164+ " expected_body" : { // request body should equal expected_body property
165+ " username" : " test" ,
166+ " password" : " test_pass"
167+ }
167168 },
168169 " response" : { // response is option field, default is {ok: 'OK'}
169170 " example" : " example GET"
@@ -472,7 +473,10 @@ const indexHtmlText = await fetch('http://localhost:8081/', {method: 'GET'}).the
472473` ` ` js
473474const fakeServer = require (' test-fake-server' )
474475const fetch = require (' node-fetch' )
475- const model_obj = {
476+
477+ // full equalty check
478+ async function callToServer () {
479+ const model_obj = {
476480 " port" : " 8081" ,
477481 " debug" : true , // if this prop exists log will show all results in console, defailt is false
478482 " api" : [{
@@ -491,18 +495,109 @@ const model_obj = {
491495 " response" : {
492496 " success" : true
493497 }
494- }]
498+ }]
499+ }
500+
501+ const serser = await fakeServer (model_obj)
502+
503+ const body_equal_success = await fetch (' http://localhost:8081/test' , {
504+ method: ' POST' ,
505+ headers: {' Content-Type' : ' application/json' },
506+ body: JSON .stringify ({" username" : " test_" , " password" : " test_pass" })
507+ }).then ((res ) => res .text ())
508+ // {"success":true}
509+ console .log (body_equal_success)
510+
511+ const body_not_equal = await fetch (' http://localhost:8081/test' , {
512+ method: ' POST' ,
513+ headers: {' Content-Type' : ' application/json' },
514+ body: JSON .stringify ({
515+ " username" : " test_1" ,
516+ " password" : " test_pass"
517+ })
518+ }).then ((res ) => res .text ())
519+ // {"success": false}
520+ console .log (body_not_equal)
521+ await serser .stop ()
495522}
496523
524+ async function callToServerPartialRequest () {
525+ const model_obj = {
526+ " port" : " 8081" ,
527+ " debug" : true , // if this prop exists log will show all results in console, defailt is false
528+ " api" : [{
529+ " method" : " POST" ,
530+ " path" : " /user" ,
531+ " request_body_equal" : {
532+ " status" : 404 ,
533+ " not_equal_response" : { // this field is optional, default is {"data": "invalid request"}
534+ " success" : false
535+ },
536+ " allow_partial_request_body" : true ,
537+ " expected_body" : {
538+ " username" : " test_" ,
539+ " password" : " test_pass"
540+ }
541+ },
542+ " response" : {
543+ " success" : true
544+ }
545+ }]
546+ }
547+
548+ const serser = await fakeServer (model_obj)
497549
550+ const body_equal_success = await fetch (' http://localhost:8081/test' , {
551+ method: ' POST' ,
552+ headers: {' Content-Type' : ' application/json' },
553+ body: JSON .stringify ({" username" : " test_" ,}) // partial body matches
554+ }).then ((res ) => res .text ())
555+ // {"success":true}
556+ console .log (body_equal_success)
557+
558+ const body_not_equal = await fetch (' http://localhost:8081/test' , {
559+ method: ' POST' ,
560+ headers: {' Content-Type' : ' application/json' },
561+ body: JSON .stringify ({
562+ " username" : " test_1" ,
563+ " password" : " test_pass"
564+ })
565+ }).then ((res ) => res .text ())
566+ // {"success": false}
567+ console .log (body_not_equal)
568+ await serser .stop ()
569+ }
570+
571+ async function callToServerPartialExpected () {
572+ const model_obj = {
573+ " port" : " 8081" ,
574+ " debug" : true , // if this prop exists log will show all results in console, defailt is false
575+ " api" : [{
576+ " method" : " POST" ,
577+ " path" : " /user" ,
578+ " request_body_equal" : {
579+ " status" : 404 ,
580+ " not_equal_response" : { // this field is optional, default is {"data": "invalid request"}
581+ " success" : false
582+ },
583+ " allow_partial_expected_body" : true ,
584+ " expected_body" : {
585+ " username" : " test_" ,
586+ " password" : " test_pass"
587+ }
588+ },
589+ " response" : {
590+ " success" : true
591+ }
592+ }]
593+ }
498594
499- async function callToServer () {
500595 const serser = await fakeServer (model_obj)
501596
502597 const body_equal_success = await fetch (' http://localhost:8081/test' , {
503598 method: ' POST' ,
504599 headers: {' Content-Type' : ' application/json' },
505- body: JSON .stringify ({" username" : " test_" , " password" : " test_pass" })
600+ body: JSON .stringify ({" username" : " test_" , " password" : " test_pass" , " x " : 2 , " Y " : 2 }) // more that required keys are in request body
506601 }).then ((res ) => res .text ())
507602 // {"success":true}
508603 console .log (body_equal_success)
0 commit comments