83
83
*/
84
84
class REST extends CodeceptionModule implements DependsOnModule, PartedModule, API , ConflictsWithModule
85
85
{
86
- const QUERY_PARAMS_AWARE_METHODS = ['GET ' , 'HEAD ' , 'DELETE ' ];
86
+ const QUERY_PARAMS_AWARE_METHODS = ['GET ' , 'HEAD ' , 'DELETE ' , ' OPTIONS ' ];
87
87
88
88
protected $ config = [
89
89
'url ' => '' ,
@@ -400,7 +400,7 @@ public function amNTLMAuthenticated($username, $password)
400
400
* ?>
401
401
* ```
402
402
* @param array $additionalAWSConfig
403
- * @throws ModuleException
403
+ * @throws ConfigurationException
404
404
*/
405
405
public function amAWSAuthenticated ($ additionalAWSConfig = [])
406
406
{
@@ -453,7 +453,7 @@ public function amAWSAuthenticated($additionalAWSConfig = [])
453
453
* ```
454
454
*
455
455
* @param $url
456
- * @param array|\JsonSerializable $params
456
+ * @param array|string| \JsonSerializable $params
457
457
* @param array $files A list of filenames or "mocks" of $_FILES (each entry being an array with the following
458
458
* keys: name, type, error, size, tmp_name (pointing to the real file path). Each key works
459
459
* as the "name" attribute of a file input field.
@@ -511,7 +511,7 @@ public function sendGet($url, $params = [])
511
511
* Sends PUT request to given uri.
512
512
*
513
513
* @param $url
514
- * @param array $params
514
+ * @param array|string|\JsonSerializable $params
515
515
* @param array $files
516
516
* @part json
517
517
* @part xml
@@ -525,7 +525,7 @@ public function sendPut($url, $params = [], $files = [])
525
525
* Sends PATCH request to given uri.
526
526
*
527
527
* @param $url
528
- * @param array $params
528
+ * @param array|string|\JsonSerializable $params
529
529
* @param array $files
530
530
* @part json
531
531
* @part xml
@@ -554,14 +554,14 @@ public function sendDelete($url, $params = [], $files = [])
554
554
*
555
555
* @param $method
556
556
* @param $url
557
- * @param array|\JsonSerializable $params
557
+ * @param array|string| \JsonSerializable $params
558
558
* @param array $files
559
559
* @part json
560
560
* @part xml
561
561
*/
562
562
public function send ($ method , $ url , $ params = [], $ files = [])
563
563
{
564
- $ this ->execute ($ method , $ url , $ params , $ files );
564
+ $ this ->execute (strtoupper ( $ method) , $ url , $ params , $ files );
565
565
}
566
566
567
567
/**
@@ -632,25 +632,34 @@ protected function execute($method, $url, $parameters = [], $files = [])
632
632
// allow full url to be requested
633
633
if (!$ url ) {
634
634
$ url = $ this ->config ['url ' ];
635
+ } elseif (!is_string ($ url )) {
636
+ throw new ModuleException (__CLASS__ , 'URL must be string ' );
635
637
} elseif (strpos ($ url , ':// ' ) === false && $ this ->config ['url ' ]) {
636
638
$ url = rtrim ($ this ->config ['url ' ], '/ ' ) . '/ ' . ltrim ($ url , '/ ' );
637
639
}
638
640
639
641
$ this ->params = $ parameters ;
640
642
641
- $ parameters = $ this ->encodeApplicationJson ($ method , $ parameters );
642
643
$ isQueryParamsAwareMethod = in_array ($ method , self ::QUERY_PARAMS_AWARE_METHODS , true );
643
644
644
- if (is_array ($ parameters ) || $ isQueryParamsAwareMethod ) {
645
- if (!empty ($ parameters ) && $ isQueryParamsAwareMethod ) {
646
- if (strpos ($ url , '? ' ) !== false ) {
647
- $ url .= '& ' ;
648
- } else {
649
- $ url .= '? ' ;
650
- }
651
- $ url .= http_build_query ($ parameters );
645
+ if ($ isQueryParamsAwareMethod ) {
646
+ if (!is_array ($ parameters )) {
647
+ throw new ModuleException (__CLASS__ , $ method . ' parameters must be passed in array format ' );
652
648
}
649
+ } else {
650
+ $ parameters = $ this ->encodeApplicationJson ($ method , $ parameters );
651
+ }
652
+
653
+ if (is_array ($ parameters ) || $ isQueryParamsAwareMethod ) {
653
654
if ($ isQueryParamsAwareMethod ) {
655
+ if (!empty ($ parameters )) {
656
+ if (strpos ($ url , '? ' ) !== false ) {
657
+ $ url .= '& ' ;
658
+ } else {
659
+ $ url .= '? ' ;
660
+ }
661
+ $ url .= http_build_query ($ parameters );
662
+ }
654
663
$ this ->debugSection ("Request " , "$ method $ url " );
655
664
$ files = [];
656
665
} else {
@@ -707,7 +716,6 @@ protected function encodeApplicationJson($method, $parameters)
707
716
{
708
717
if (
709
718
array_key_exists ('Content-Type ' , $ this ->connectionModule ->headers )
710
- && !in_array ($ method , self ::QUERY_PARAMS_AWARE_METHODS , true )
711
719
&& ($ this ->connectionModule ->headers ['Content-Type ' ] === 'application/json '
712
720
|| preg_match ('!^application/.+\+json$! ' , $ this ->connectionModule ->headers ['Content-Type ' ])
713
721
)
@@ -720,6 +728,15 @@ protected function encodeApplicationJson($method, $parameters)
720
728
return json_encode ($ parameters );
721
729
}
722
730
}
731
+
732
+ if ($ parameters instanceof \JsonSerializable) {
733
+ throw new ModuleException (__CLASS__ , $ method . ' parameters is JsonSerializable object, but Content-Type header is not set to application/json ' );
734
+ }
735
+
736
+ if (!is_string ($ parameters ) && !is_array ($ parameters )) {
737
+ throw new ModuleException (__CLASS__ , $ method . ' parameters must be array, string or object implementing JsonSerializable interface ' );
738
+ }
739
+
723
740
return $ parameters ;
724
741
}
725
742
@@ -982,7 +999,6 @@ protected function decodeAndValidateJson($jsonString, $errorFormat="Invalid json
982
999
* @return string
983
1000
* @part json
984
1001
* @part xml
985
- * @version 1.1
986
1002
*/
987
1003
public function grabResponse ()
988
1004
{
@@ -1006,7 +1022,6 @@ public function grabResponse()
1006
1022
* @return array Array of matching items
1007
1023
* @throws \Exception
1008
1024
* @part json
1009
- * @version 2.0.9
1010
1025
*/
1011
1026
public function grabDataFromResponseByJsonPath ($ jsonPath )
1012
1027
{
@@ -1052,7 +1067,6 @@ public function grabDataFromResponseByJsonPath($jsonPath)
1052
1067
* ```
1053
1068
* @param string $xpath
1054
1069
* @part json
1055
- * @version 2.0.9
1056
1070
*/
1057
1071
public function seeResponseJsonMatchesXpath ($ xpath )
1058
1072
{
@@ -1119,7 +1133,6 @@ public function dontSeeResponseJsonMatchesXpath($xpath)
1119
1133
*
1120
1134
* @param string $jsonPath
1121
1135
* @part json
1122
- * @version 2.0.9
1123
1136
*/
1124
1137
public function seeResponseJsonMatchesJsonPath ($ jsonPath )
1125
1138
{
@@ -1244,7 +1257,6 @@ public function dontSeeResponseContainsJson($json = [])
1244
1257
* @param array $jsonType
1245
1258
* @param string $jsonPath
1246
1259
* @see JsonType
1247
- * @version 2.1.3
1248
1260
*/
1249
1261
public function seeResponseMatchesJsonType (array $ jsonType , $ jsonPath = null )
1250
1262
{
@@ -1260,12 +1272,11 @@ public function seeResponseMatchesJsonType(array $jsonType, $jsonPath = null)
1260
1272
* Opposite to `seeResponseMatchesJsonType`.
1261
1273
*
1262
1274
* @part json
1263
- * @param $jsonType jsonType structure
1264
- * @param null $jsonPath optionally set specific path to structure with JsonPath
1275
+ * @param array $jsonType JsonType structure
1276
+ * @param string $jsonPath
1265
1277
* @see seeResponseMatchesJsonType
1266
- * @version 2.1.3
1267
1278
*/
1268
- public function dontSeeResponseMatchesJsonType ($ jsonType , $ jsonPath = null )
1279
+ public function dontSeeResponseMatchesJsonType (array $ jsonType , $ jsonPath = null )
1269
1280
{
1270
1281
$ jsonArray = new JsonArray ($ this ->connectionModule ->_getResponseContent ());
1271
1282
if ($ jsonPath ) {
@@ -1566,8 +1577,8 @@ public function dontSeeXmlResponseIncludes($xml)
1566
1577
* ?>
1567
1578
* ```
1568
1579
*
1569
- * @param $hash the hashed data response expected
1570
- * @param $algo the hash algorithm to use. Default md5.
1580
+ * @param string $hash the hashed data response expected
1581
+ * @param string $algo the hash algorithm to use. Default md5.
1571
1582
* @part json
1572
1583
* @part xml
1573
1584
*/
@@ -1587,8 +1598,8 @@ public function seeBinaryResponseEquals($hash, $algo = 'md5')
1587
1598
* ```
1588
1599
* Opposite to `seeBinaryResponseEquals`
1589
1600
*
1590
- * @param $hash the hashed data response expected
1591
- * @param $algo the hash algorithm to use. Default md5.
1601
+ * @param string $hash the hashed data response expected
1602
+ * @param string $algo the hash algorithm to use. Default md5.
1592
1603
* @part json
1593
1604
* @part xml
1594
1605
*/
0 commit comments