mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-24 22:34:24 +02:00
parent
96e7411e25
commit
6d593620d6
@ -213,15 +213,12 @@ class RestRequest
|
|||||||
'Expect:'
|
'Expect:'
|
||||||
);
|
);
|
||||||
|
|
||||||
$ch = curl_init();
|
|
||||||
|
|
||||||
$options = array(
|
$options = array(
|
||||||
CURLOPT_URL => $this->uri,
|
CURLOPT_URL => $this->uri,
|
||||||
CURLOPT_TIMEOUT => $this->timeout,
|
CURLOPT_TIMEOUT => $this->timeout,
|
||||||
// Ignore proxy settings
|
// Ignore proxy settings
|
||||||
CURLOPT_PROXY => '',
|
CURLOPT_PROXY => '',
|
||||||
CURLOPT_CUSTOMREQUEST => $this->method,
|
CURLOPT_CUSTOMREQUEST => $this->method
|
||||||
CURLOPT_RETURNTRANSFER => true
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Record cURL command line for debugging
|
// Record cURL command line for debugging
|
||||||
@ -250,27 +247,20 @@ class RestRequest
|
|||||||
$options[CURLOPT_HTTPHEADER] = $headers;
|
$options[CURLOPT_HTTPHEADER] = $headers;
|
||||||
|
|
||||||
$stream = null;
|
$stream = null;
|
||||||
if (Logger::getInstance()->getLevel() === Logger::DEBUG) {
|
$logger = Logger::getInstance();
|
||||||
|
if ($logger !== null && $logger->getLevel() === Logger::DEBUG) {
|
||||||
$stream = fopen('php://temp', 'w');
|
$stream = fopen('php://temp', 'w');
|
||||||
$options[CURLOPT_VERBOSE] = true;
|
$options[CURLOPT_VERBOSE] = true;
|
||||||
$options[CURLOPT_STDERR] = $stream;
|
$options[CURLOPT_STDERR] = $stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_setopt_array($ch, $options);
|
|
||||||
|
|
||||||
Logger::debug(
|
Logger::debug(
|
||||||
'Executing %s %s',
|
'Executing %s %s',
|
||||||
implode(' ', $curlCmd),
|
implode(' ', $curlCmd),
|
||||||
escapeshellarg($this->uri)
|
escapeshellarg($this->uri)
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = curl_exec($ch);
|
$result = $this->curlExec($options);
|
||||||
|
|
||||||
if ($result === false) {
|
|
||||||
throw new CurlException('%s', curl_error($ch));
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
if (is_resource($stream)) {
|
if (is_resource($stream)) {
|
||||||
rewind($stream);
|
rewind($stream);
|
||||||
@ -280,4 +270,28 @@ class RestRequest
|
|||||||
|
|
||||||
return Json::decode($result, true);
|
return Json::decode($result, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up a new cURL handle with the given options and call {@link curl_exec()}
|
||||||
|
*
|
||||||
|
* @param array $options
|
||||||
|
*
|
||||||
|
* @return string The response
|
||||||
|
*
|
||||||
|
* @throws CurlException
|
||||||
|
*/
|
||||||
|
protected function curlExec(array $options)
|
||||||
|
{
|
||||||
|
$ch = curl_init();
|
||||||
|
$options[CURLOPT_RETURNTRANSFER] = true;
|
||||||
|
curl_setopt_array($ch, $options);
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
|
||||||
|
if ($result === false) {
|
||||||
|
throw new CurlException('%s', curl_error($ch));
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_close($ch);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
|
namespace Tests\Icinga\Modules\Monitoring\Web\Rest;
|
||||||
|
|
||||||
|
use Icinga\Exception\Json\JsonDecodeException;
|
||||||
|
use Icinga\Module\Monitoring\Web\Rest\RestRequest;
|
||||||
|
use Icinga\Test\BaseTestCase;
|
||||||
|
|
||||||
|
class MockedRestRequest extends RestRequest
|
||||||
|
{
|
||||||
|
protected function curlExec(array $options)
|
||||||
|
{
|
||||||
|
return '<h1>Unauthorized</h1>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RestRequestTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @expectedException JsonDecodeException
|
||||||
|
*/
|
||||||
|
public function testInvalidServerResponseHandling()
|
||||||
|
{
|
||||||
|
MockedRestRequest::get('http://localhost')->send();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user