N°6204 - REST API: Add unit test for callback parameter

This commit is contained in:
Molkobain
2023-04-18 22:34:11 +02:00
parent 096ed9a63a
commit 778118cfb4

View File

@@ -27,7 +27,7 @@ class RestTest extends ItopDataTestCase
private $sTmpFile = "";
/** @var int $iJsonDataMode */
private $iJsonDataMode;
private $iJsonDataMode = self::ENUM_JSONDATA_AS_STRING;
private $sUrl;
private $sLogin;
private $sPassword = "Iuytrez9876543ç_è-(";
@@ -57,6 +57,28 @@ class RestTest extends ItopDataTestCase
}
}
public function testJSONPCallback()
{
$sCallbackName = 'fooCallback';
$sJsonData = <<<JSON
{
"operation": "list_operations"
}
JSON;
// Test regular JSON result
$sJSONResult = $this->CallRestApi($sJsonData);
// - Try to decode JSON to array to check if it is well-formed
$aJSONResultAsArray = json_decode($sJSONResult, true);
if (false === is_array($aJSONResultAsArray)) {
$this->fail('JSON result could not be decoded as array, it might be malformed');
}
// Test JSONP with callback by checking that it is the same as the regular JSON but within the JS callback
$sJSONPResult = $this->CallRestApi($sJsonData, $sCallbackName);
$this->assertEquals($sCallbackName.'('.$sJSONResult.')', $sJSONPResult, 'JSONP response callback does not match expected result');
}
/**
* @dataProvider BasicProvider
* @param int $iJsonDataMode
@@ -250,7 +272,7 @@ JSON;
}
private function CallRestApi($sJsonDataContent){
private function CallRestApi(string $sJsonDataContent, string $sCallbackName = null){
$ch = curl_init();
$aPostFields = [
'version' => '1.3',
@@ -268,6 +290,10 @@ JSON;
$aPostFields['json_data'] = $sJsonDataContent;
}
if (utils::IsNotNullOrEmptyString($sCallbackName)) {
$aPostFields['callback'] = $sCallbackName;
}
curl_setopt($ch, CURLOPT_URL, "$this->sUrl/webservices/rest.php");
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);