another phpunit optimiziation ..
This commit is contained in:
parent
4d6c8241fc
commit
8881882bce
3 changed files with 25 additions and 37 deletions
|
@ -76,7 +76,7 @@ class HttpClient implements ICanSendHttpRequests
|
||||||
if(!filter_var($host, FILTER_VALIDATE_IP) && !@dns_get_record($host . '.', DNS_A + DNS_AAAA) && !gethostbyname($host)) {
|
if(!filter_var($host, FILTER_VALIDATE_IP) && !@dns_get_record($host . '.', DNS_A + DNS_AAAA) && !gethostbyname($host)) {
|
||||||
$this->logger->debug('URL cannot be resolved.', ['url' => $url, 'callstack' => System::callstack(20)]);
|
$this->logger->debug('URL cannot be resolved.', ['url' => $url, 'callstack' => System::callstack(20)]);
|
||||||
$this->profiler->stopRecording();
|
$this->profiler->stopRecording();
|
||||||
return CurlResult::createErrorCurl($url);
|
return CurlResult::createErrorCurl($this->logger, $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Network::isLocalLink($url)) {
|
if (Network::isLocalLink($url)) {
|
||||||
|
@ -86,7 +86,7 @@ class HttpClient implements ICanSendHttpRequests
|
||||||
if (strlen($url) > 1000) {
|
if (strlen($url) > 1000) {
|
||||||
$this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
|
$this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
|
||||||
$this->profiler->stopRecording();
|
$this->profiler->stopRecording();
|
||||||
return CurlResult::createErrorCurl(substr($url, 0, 200));
|
return CurlResult::createErrorCurl($this->logger, substr($url, 0, 200));
|
||||||
}
|
}
|
||||||
|
|
||||||
$parts2 = [];
|
$parts2 = [];
|
||||||
|
@ -105,7 +105,7 @@ class HttpClient implements ICanSendHttpRequests
|
||||||
if (Network::isUrlBlocked($url)) {
|
if (Network::isUrlBlocked($url)) {
|
||||||
$this->logger->info('Domain is blocked.', ['url' => $url]);
|
$this->logger->info('Domain is blocked.', ['url' => $url]);
|
||||||
$this->profiler->stopRecording();
|
$this->profiler->stopRecording();
|
||||||
return CurlResult::createErrorCurl($url);
|
return CurlResult::createErrorCurl($this->logger, $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
$conf = [];
|
$conf = [];
|
||||||
|
@ -176,11 +176,11 @@ class HttpClient implements ICanSendHttpRequests
|
||||||
$exception->hasResponse()) {
|
$exception->hasResponse()) {
|
||||||
return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), '');
|
return new GuzzleResponse($exception->getResponse(), $url, $exception->getCode(), '');
|
||||||
} else {
|
} else {
|
||||||
return new CurlResult($url, '', ['http_code' => 500], $exception->getCode(), '');
|
return new CurlResult($this->logger, $url, '', ['http_code' => 500], $exception->getCode(), '');
|
||||||
}
|
}
|
||||||
} catch (InvalidArgumentException | \InvalidArgumentException $argumentException) {
|
} catch (InvalidArgumentException | \InvalidArgumentException $argumentException) {
|
||||||
$this->logger->info('Invalid Argument for HTTP call.', ['url' => $url, 'method' => $method, 'exception' => $argumentException]);
|
$this->logger->info('Invalid Argument for HTTP call.', ['url' => $url, 'method' => $method, 'exception' => $argumentException]);
|
||||||
return new CurlResult($url, '', ['http_code' => 500], $argumentException->getCode(), $argumentException->getMessage());
|
return new CurlResult($this->logger, $url, '', ['http_code' => 500], $argumentException->getCode(), $argumentException->getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
unlink($conf['sink']);
|
unlink($conf['sink']);
|
||||||
$this->logger->debug('Request stop.', ['url' => $url, 'method' => $method]);
|
$this->logger->debug('Request stop.', ['url' => $url, 'method' => $method]);
|
||||||
|
|
|
@ -25,6 +25,7 @@ use Friendica\Core\Logger;
|
||||||
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
|
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
|
||||||
use Friendica\Network\HTTPException\UnprocessableEntityException;
|
use Friendica\Network\HTTPException\UnprocessableEntityException;
|
||||||
use Friendica\Util\Network;
|
use Friendica\Util\Network;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A content class for Curl call results
|
* A content class for Curl call results
|
||||||
|
@ -96,6 +97,11 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
*/
|
*/
|
||||||
private $error;
|
private $error;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an errored CURL response
|
* Creates an errored CURL response
|
||||||
*
|
*
|
||||||
|
@ -104,9 +110,9 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
* @return ICanHandleHttpResponses a CURL with error response
|
* @return ICanHandleHttpResponses a CURL with error response
|
||||||
* @throws UnprocessableEntityException
|
* @throws UnprocessableEntityException
|
||||||
*/
|
*/
|
||||||
public static function createErrorCurl(string $url = '')
|
public static function createErrorCurl(LoggerInterface $logger, string $url = '')
|
||||||
{
|
{
|
||||||
return new CurlResult($url, '', ['http_code' => 0]);
|
return new CurlResult($logger, $url, '', ['http_code' => 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -120,8 +126,10 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
*
|
*
|
||||||
* @throws UnprocessableEntityException when HTTP code of the CURL response is missing
|
* @throws UnprocessableEntityException when HTTP code of the CURL response is missing
|
||||||
*/
|
*/
|
||||||
public function __construct(string $url, string $result, array $info, int $errorNumber = 0, string $error = '')
|
public function __construct(LoggerInterface $logger, string $url, string $result, array $info, int $errorNumber = 0, string $error = '')
|
||||||
{
|
{
|
||||||
|
$this->logger = $logger;
|
||||||
|
|
||||||
if (!array_key_exists('http_code', $info)) {
|
if (!array_key_exists('http_code', $info)) {
|
||||||
throw new UnprocessableEntityException('CURL response doesn\'t contains a response HTTP code');
|
throw new UnprocessableEntityException('CURL response doesn\'t contains a response HTTP code');
|
||||||
}
|
}
|
||||||
|
@ -132,7 +140,7 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
$this->errorNumber = $errorNumber;
|
$this->errorNumber = $errorNumber;
|
||||||
$this->error = $error;
|
$this->error = $error;
|
||||||
|
|
||||||
Logger::debug('construct', ['url' => $url, 'returncode' => $this->returnCode, 'result' => $result]);
|
$this->logger->debug('construct', ['url' => $url, 'returncode' => $this->returnCode, 'result' => $result]);
|
||||||
|
|
||||||
$this->parseBodyHeader($result);
|
$this->parseBodyHeader($result);
|
||||||
$this->checkSuccess();
|
$this->checkSuccess();
|
||||||
|
@ -172,7 +180,7 @@ class CurlResult implements ICanHandleHttpResponses
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->isSuccess) {
|
if (!$this->isSuccess) {
|
||||||
Logger::debug('debug', ['info' => $this->info]);
|
$this->logger->debug('debug', ['info' => $this->info]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
|
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
|
||||||
|
|
|
@ -21,32 +21,12 @@
|
||||||
|
|
||||||
namespace Friendica\Test\src\Network\HTTPClient\Response;
|
namespace Friendica\Test\src\Network\HTTPClient\Response;
|
||||||
|
|
||||||
use Dice\Dice;
|
|
||||||
use Friendica\DI;
|
|
||||||
use Friendica\Network\HTTPClient\Response\CurlResult;
|
use Friendica\Network\HTTPClient\Response\CurlResult;
|
||||||
use Mockery\MockInterface;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
use Psr\Log\NullLogger;
|
use Psr\Log\NullLogger;
|
||||||
|
|
||||||
class CurlResultTest extends TestCase
|
class CurlResultTest extends TestCase
|
||||||
{
|
{
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
/** @var Dice|MockInterface $dice */
|
|
||||||
$dice = \Mockery::mock(Dice::class)->makePartial();
|
|
||||||
$dice = $dice->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php');
|
|
||||||
|
|
||||||
$logger = new NullLogger();
|
|
||||||
$dice->shouldReceive('create')
|
|
||||||
->with(LoggerInterface::class)
|
|
||||||
->andReturn($logger);
|
|
||||||
|
|
||||||
DI::init($dice);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @small
|
* @small
|
||||||
*/
|
*/
|
||||||
|
@ -57,7 +37,7 @@ class CurlResultTest extends TestCase
|
||||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
|
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local', $header . $body, [
|
||||||
'http_code' => 200,
|
'http_code' => 200,
|
||||||
'content_type' => 'text/html; charset=utf-8',
|
'content_type' => 'text/html; charset=utf-8',
|
||||||
'url' => 'https://test.local'
|
'url' => 'https://test.local'
|
||||||
|
@ -85,7 +65,7 @@ class CurlResultTest extends TestCase
|
||||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
|
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local/test/it', $header . $body, [
|
||||||
'http_code' => 301,
|
'http_code' => 301,
|
||||||
'content_type' => 'text/html; charset=utf-8',
|
'content_type' => 'text/html; charset=utf-8',
|
||||||
'url' => 'https://test.local/test/it',
|
'url' => 'https://test.local/test/it',
|
||||||
|
@ -112,7 +92,7 @@ class CurlResultTest extends TestCase
|
||||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local/test/it', $header . $body, [
|
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local/test/it', $header . $body, [
|
||||||
'http_code' => 500,
|
'http_code' => 500,
|
||||||
'content_type' => 'text/html; charset=utf-8',
|
'content_type' => 'text/html; charset=utf-8',
|
||||||
'url' => 'https://test.local/test/it',
|
'url' => 'https://test.local/test/it',
|
||||||
|
@ -141,7 +121,7 @@ class CurlResultTest extends TestCase
|
||||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
$curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
|
$curlResult = new CurlResult(new NullLogger(),'https://test.local/test/it?key=value', $header . $body, [
|
||||||
'http_code' => 301,
|
'http_code' => 301,
|
||||||
'content_type' => 'text/html; charset=utf-8',
|
'content_type' => 'text/html; charset=utf-8',
|
||||||
'url' => 'https://test.local/test/it?key=value',
|
'url' => 'https://test.local/test/it?key=value',
|
||||||
|
@ -165,7 +145,7 @@ class CurlResultTest extends TestCase
|
||||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||||
|
|
||||||
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
|
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local', $header . $body, [
|
||||||
'http_code' => 200,
|
'http_code' => 200,
|
||||||
'content_type' => 'text/html; charset=utf-8',
|
'content_type' => 'text/html; charset=utf-8',
|
||||||
'url' => 'https://test.local'
|
'url' => 'https://test.local'
|
||||||
|
@ -182,7 +162,7 @@ class CurlResultTest extends TestCase
|
||||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||||
|
|
||||||
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult('https://test.local', $header . $body, [
|
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(), 'https://test.local', $header . $body, [
|
||||||
'http_code' => 200,
|
'http_code' => 200,
|
||||||
'content_type' => 'text/html; charset=utf-8',
|
'content_type' => 'text/html; charset=utf-8',
|
||||||
'url' => 'https://test.local'
|
'url' => 'https://test.local'
|
||||||
|
@ -202,7 +182,7 @@ class CurlResultTest extends TestCase
|
||||||
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
|
||||||
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
|
||||||
|
|
||||||
$curlResult = new CurlResult('https://test.local', $header . $body, [
|
$curlResult = new CurlResult(new NullLogger(),'https://test.local', $header . $body, [
|
||||||
'http_code' => 200,
|
'http_code' => 200,
|
||||||
'content_type' => 'text/html; charset=utf-8',
|
'content_type' => 'text/html; charset=utf-8',
|
||||||
'url' => 'https://test.local'
|
'url' => 'https://test.local'
|
||||||
|
|
Loading…
Reference in a new issue