Skip to content

Commit 2502b5d

Browse files
committed
COLDBOX-1389 #resolve
Allow for a new `body` argument to all tests methods so you can mock incoming body requests
1 parent 2164a7a commit 2502b5d

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

system/testing/BaseTestCase.cfc

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
386386
struct eventArguments = {},
387387
boolean renderResults = false,
388388
boolean withExceptionHandling = false,
389-
domain = cgi.SERVER_NAME
389+
string domain = cgi.SERVER_NAME
390390
){
391391
var handlerResults = "";
392392
var requestContext = getRequestContext();
@@ -585,6 +585,7 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
585585
* @renderResults If true, then it will try to do the normal rendering procedures and store the rendered content in the RC as cbox_rendered_content
586586
* @withExceptionHandling If true, then ColdBox will process any errors through the exception handling framework instead of just throwing the error. Default: false.
587587
* @domain Override the domain of execution of the request. Default is to use the cgi.server_name variable.
588+
* @body The body content to be passed in the request, useful for POST/PUT/PATCH requests.
588589
*/
589590
function request(
590591
string route = "",
@@ -593,23 +594,35 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
593594
string method = "GET",
594595
boolean renderResults = true,
595596
boolean withExceptionHandling = false,
596-
domain = cgi.SERVER_NAME
597+
string domain = cgi.SERVER_NAME,
598+
any body = ""
597599
){
598-
var mockedEvent = prepareMock( getRequestContext() ).$( "getHTTPMethod", uCase( arguments.method ) );
600+
// Mock the event context
601+
var mockedEvent = prepareMock( getRequestContext() )
602+
// Mock the HTTP method
603+
.$( "getHTTPMethod", uCase( arguments.method ) )
604+
// Mock the body content
605+
.$( "getHttpContent", arguments.body )
606+
607+
// Add params to the request collection
599608
arguments.params
600609
.keyArray()
601-
.each( function( name ){
602-
mockedEvent.setValue( arguments.name, params[ arguments.name ] );
603-
} );
610+
.each( ( name ) => {
611+
mockedEvent.setValue( arguments.name, params[ arguments.name ] )
612+
} )
613+
614+
// Add headers to the request collection
604615
arguments.headers
605616
.keyArray()
606-
.each( function( name ){
617+
.each( ( name ) => {
607618
mockedEvent
608619
.$( "getHTTPHeader" )
609620
.$args( arguments.name )
610-
.$results( headers[ arguments.name ] );
611-
} );
612-
return this.execute( argumentCollection = arguments );
621+
.$results( headers[ arguments.name ] )
622+
} )
623+
624+
// Funnel through the main execute method
625+
return this.execute( argumentCollection: arguments )
613626
}
614627

615628
/**
@@ -621,14 +634,16 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
621634
* @renderResults If true, then it will try to do the normal rendering procedures and store the rendered content in the RC as cbox_rendered_content
622635
* @withExceptionHandling If true, then ColdBox will process any errors through the exception handling framework instead of just throwing the error. Default: false.
623636
* @domain Override the domain of execution of the request. Default is to use the cgi.server_name variable.
637+
* @body The body content to be passed in the request, useful for PUT/PATCH requests.
624638
*/
625639
function get(
626640
string route = "",
627641
struct params = {},
628642
struct headers = {},
629643
boolean renderResults = true,
630644
boolean withExceptionHandling = false,
631-
domain = cgi.SERVER_NAME
645+
string domain = cgi.SERVER_NAME,
646+
any body = ""
632647
){
633648
arguments.method = "GET";
634649
return variables.request( argumentCollection = arguments );
@@ -643,14 +658,16 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
643658
* @renderResults If true, then it will try to do the normal rendering procedures and store the rendered content in the RC as cbox_rendered_content
644659
* @withExceptionHandling If true, then ColdBox will process any errors through the exception handling framework instead of just throwing the error. Default: false.
645660
* @domain Override the domain of execution of the request. Default is to use the cgi.server_name variable.
661+
* @body The body content to be passed in the request, useful for POST/PUT/PATCH requests.
646662
*/
647663
function post(
648664
string route = "",
649665
struct params = {},
650666
struct headers = {},
651667
boolean renderResults = true,
652668
boolean withExceptionHandling = false,
653-
domain = cgi.SERVER_NAME
669+
string domain = cgi.SERVER_NAME,
670+
any body = ""
654671
){
655672
arguments.method = "POST";
656673
return variables.request( argumentCollection = arguments );
@@ -665,14 +682,16 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
665682
* @renderResults If true, then it will try to do the normal rendering procedures and store the rendered content in the RC as cbox_rendered_content
666683
* @withExceptionHandling If true, then ColdBox will process any errors through the exception handling framework instead of just throwing the error. Default: false.
667684
* @domain Override the domain of execution of the request. Default is to use the cgi.server_name variable.
685+
* @body The body content to be passed in the request, useful for POST/PUT/PATCH requests.
668686
*/
669687
function put(
670688
string route = "",
671689
struct params = {},
672690
struct headers = {},
673691
boolean renderResults = true,
674692
boolean withExceptionHandling = false,
675-
domain = cgi.SERVER_NAME
693+
string domain = cgi.SERVER_NAME,
694+
any body = ""
676695
){
677696
arguments.method = "PUT";
678697
return variables.request( argumentCollection = arguments );
@@ -687,14 +706,16 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
687706
* @renderResults If true, then it will try to do the normal rendering procedures and store the rendered content in the RC as cbox_rendered_content
688707
* @withExceptionHandling If true, then ColdBox will process any errors through the exception handling framework instead of just throwing the error. Default: false.
689708
* @domain Override the domain of execution of the request. Default is to use the cgi.server_name variable.
709+
* @body The body content to be passed in the request, useful for POST/PUT/PATCH requests.
690710
*/
691711
function patch(
692712
string route = "",
693713
struct params = {},
694714
struct headers = {},
695715
boolean renderResults = true,
696716
boolean withExceptionHandling = false,
697-
domain = cgi.SERVER_NAME
717+
string domain = cgi.SERVER_NAME,
718+
any body = ""
698719
){
699720
arguments.method = "PATCH";
700721
return variables.request( argumentCollection = arguments );
@@ -709,14 +730,16 @@ component extends="testbox.system.compat.framework.TestCase" accessors="true" {
709730
* @renderResults If true, then it will try to do the normal rendering procedures and store the rendered content in the RC as cbox_rendered_content
710731
* @withExceptionHandling If true, then ColdBox will process any errors through the exception handling framework instead of just throwing the error. Default: false.
711732
* @domain Override the domain of execution of the request. Default is to use the cgi.server_name variable.
733+
* @body The body content to be passed in the request, useful for POST/PUT/PATCH requests.
712734
*/
713735
function delete(
714736
string route = "",
715737
struct params = {},
716738
struct headers = {},
717739
boolean renderResults = true,
718740
boolean withExceptionHandling = false,
719-
domain = cgi.SERVER_NAME
741+
string domain = cgi.SERVER_NAME,
742+
any body = ""
720743
){
721744
arguments.method = "DELETE";
722745
return variables.request( argumentCollection = arguments );

0 commit comments

Comments
 (0)