2018-10-11 15:19:38 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\Network;
|
|
|
|
|
2019-07-26 09:54:14 -04:00
|
|
|
use Dice\Dice;
|
|
|
|
use Friendica\BaseObject;
|
2018-10-11 15:19:38 -04:00
|
|
|
use Friendica\Network\CurlResult;
|
2019-07-26 09:54:14 -04:00
|
|
|
use Mockery\MockInterface;
|
2018-10-11 15:19:38 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-07-26 09:54:14 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
2018-10-11 15:19:38 -04:00
|
|
|
|
|
|
|
class CurlResultTest extends TestCase
|
|
|
|
{
|
2019-03-03 09:20:26 -05:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-07-26 09:54:14 -04:00
|
|
|
|
|
|
|
/** @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);
|
|
|
|
|
|
|
|
BaseObject::setDependencyInjection($dice);
|
2019-03-03 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 16:18:27 -04:00
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
2018-10-11 15:19:38 -04:00
|
|
|
public function testNormal()
|
|
|
|
{
|
|
|
|
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
|
|
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
|
|
|
|
|
|
|
|
2018-10-11 16:18:27 -04:00
|
|
|
$curlResult = new CurlResult('https://test.local', $header . $body, [
|
|
|
|
'http_code' => 200,
|
|
|
|
'content_type' => 'text/html; charset=utf-8',
|
|
|
|
'url' => 'https://test.local'
|
|
|
|
]);
|
2018-10-11 15:19:38 -04:00
|
|
|
|
|
|
|
$this->assertTrue($curlResult->isSuccess());
|
2018-10-11 16:18:27 -04:00
|
|
|
$this->assertFalse($curlResult->isTimeout());
|
|
|
|
$this->assertFalse($curlResult->isRedirectUrl());
|
2018-10-11 15:19:38 -04:00
|
|
|
$this->assertSame($header, $curlResult->getHeader());
|
|
|
|
$this->assertSame($body, $curlResult->getBody());
|
2018-10-11 16:18:27 -04:00
|
|
|
$this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
|
|
|
$this->assertSame('https://test.local', $curlResult->getUrl());
|
|
|
|
$this->assertSame('https://test.local', $curlResult->getRedirectUrl());
|
2018-10-11 15:19:38 -04:00
|
|
|
}
|
2018-10-11 16:18:27 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 14:26:17 -05:00
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
2018-10-11 16:18:27 -04:00
|
|
|
*/
|
|
|
|
public function testRedirect()
|
|
|
|
{
|
|
|
|
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
|
|
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
|
|
|
|
|
|
|
|
|
|
|
$curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
|
|
|
|
'http_code' => 301,
|
|
|
|
'content_type' => 'text/html; charset=utf-8',
|
|
|
|
'url' => 'https://test.local/test/it',
|
|
|
|
'redirect_url' => 'https://test.other'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($curlResult->isSuccess());
|
|
|
|
$this->assertFalse($curlResult->isTimeout());
|
|
|
|
$this->assertTrue($curlResult->isRedirectUrl());
|
|
|
|
$this->assertSame($header, $curlResult->getHeader());
|
|
|
|
$this->assertSame($body, $curlResult->getBody());
|
|
|
|
$this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
|
|
|
$this->assertSame('https://test.local/test/it', $curlResult->getUrl());
|
|
|
|
$this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
|
|
|
public function testTimeout()
|
|
|
|
{
|
|
|
|
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
|
|
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
|
|
|
|
|
|
|
|
|
|
|
$curlResult = new CurlResult('https://test.local/test/it', $header . $body, [
|
|
|
|
'http_code' => 500,
|
|
|
|
'content_type' => 'text/html; charset=utf-8',
|
|
|
|
'url' => 'https://test.local/test/it',
|
|
|
|
'redirect_url' => 'https://test.other'
|
|
|
|
], CURLE_OPERATION_TIMEDOUT, 'Tested error');
|
|
|
|
|
|
|
|
$this->assertFalse($curlResult->isSuccess());
|
|
|
|
$this->assertTrue($curlResult->isTimeout());
|
|
|
|
$this->assertFalse($curlResult->isRedirectUrl());
|
|
|
|
$this->assertSame($header, $curlResult->getHeader());
|
|
|
|
$this->assertSame($body, $curlResult->getBody());
|
|
|
|
$this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
|
|
|
$this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
|
|
|
|
$this->assertSame('Tested error', $curlResult->getError());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
2019-01-30 14:26:17 -05:00
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
2018-10-11 16:18:27 -04:00
|
|
|
*/
|
|
|
|
public function testRedirectHeader()
|
|
|
|
{
|
|
|
|
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
|
|
|
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
|
|
|
|
|
|
|
|
|
|
|
$curlResult = new CurlResult('https://test.local/test/it?key=value', $header . $body, [
|
|
|
|
'http_code' => 301,
|
|
|
|
'content_type' => 'text/html; charset=utf-8',
|
|
|
|
'url' => 'https://test.local/test/it?key=value',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($curlResult->isSuccess());
|
|
|
|
$this->assertFalse($curlResult->isTimeout());
|
|
|
|
$this->assertTrue($curlResult->isRedirectUrl());
|
|
|
|
$this->assertSame($header, $curlResult->getHeader());
|
|
|
|
$this->assertSame($body, $curlResult->getBody());
|
|
|
|
$this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
|
|
|
$this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
|
|
|
|
$this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
|
|
|
|
}
|
2019-10-02 05:26:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
|
|
|
public function testInHeader()
|
|
|
|
{
|
|
|
|
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
|
|
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
|
|
|
|
|
|
|
$curlResult = new CurlResult('https://test.local', $header . $body, [
|
|
|
|
'http_code' => 200,
|
|
|
|
'content_type' => 'text/html; charset=utf-8',
|
|
|
|
'url' => 'https://test.local'
|
|
|
|
]);
|
|
|
|
$this->assertTrue($curlResult->inHeader('vary'));
|
|
|
|
$this->assertFalse($curlResult->inHeader('wrongHeader'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
|
|
|
public function testGetHeaderArray()
|
|
|
|
{
|
|
|
|
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
|
|
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
|
|
|
|
|
|
|
$curlResult = new CurlResult('https://test.local', $header . $body, [
|
|
|
|
'http_code' => 200,
|
|
|
|
'content_type' => 'text/html; charset=utf-8',
|
|
|
|
'url' => 'https://test.local'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$headers = $curlResult->getHeaderArray();
|
|
|
|
|
|
|
|
$this->assertNotEmpty($headers);
|
|
|
|
$this->assertArrayHasKey('vary', $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @small
|
|
|
|
*/
|
|
|
|
public function testGetHeaderWithParam()
|
|
|
|
{
|
|
|
|
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
|
|
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
|
|
|
|
|
|
|
$curlResult = new CurlResult('https://test.local', $header . $body, [
|
|
|
|
'http_code' => 200,
|
|
|
|
'content_type' => 'text/html; charset=utf-8',
|
|
|
|
'url' => 'https://test.local'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($curlResult->getHeader());
|
|
|
|
$this->assertEmpty('vary', $curlResult->getHeader('wrongHeader'));
|
|
|
|
}
|
2018-10-11 16:18:27 -04:00
|
|
|
}
|