friendica/tests/src/Network/HTTPClient/Response/CurlResultTest.php

195 lines
7 KiB
PHP
Raw Normal View History

2018-10-11 21:19:38 +02:00
<?php
2020-02-09 15:45:36 +01:00
/**
* @copyright Copyright (C) 2010-2024, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2018-10-11 21:19:38 +02:00
namespace Friendica\Test\src\Network\HTTPClient\Response;
2018-10-11 21:19:38 +02:00
use Friendica\Network\HTTPClient\Response\CurlResult;
2018-10-11 21:19:38 +02:00
use PHPUnit\Framework\TestCase;
2019-07-26 15:54:14 +02:00
use Psr\Log\NullLogger;
2018-10-11 21:19:38 +02:00
class CurlResultTest extends TestCase
{
2018-10-11 22:18:27 +02:00
/**
* @small
*/
2018-10-11 21:19:38 +02:00
public function testNormal()
{
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
2018-10-11 21:19:38 +02:00
2023-02-12 15:41:25 +01:00
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local', $header . $body, [
2018-10-11 22:18:27 +02:00
'http_code' => 200,
'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local'
]);
2018-10-11 21:19:38 +02:00
self::assertTrue($curlResult->isSuccess());
self::assertFalse($curlResult->isTimeout());
self::assertFalse($curlResult->isRedirectUrl());
self::assertSame($headerArray, $curlResult->getHeaders());
self::assertSame($body, $curlResult->getBodyString());
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
self::assertSame('https://test.local', $curlResult->getUrl());
self::assertSame('https://test.local', $curlResult->getRedirectUrl());
2018-10-11 21:19:38 +02:00
}
2018-10-11 22:18:27 +02:00
/**
* @small
* @runInSeparateProcess
* @preserveGlobalState disabled
2018-10-11 22:18:27 +02:00
*/
public function testRedirect()
{
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
2018-10-11 22:18:27 +02:00
2023-02-12 15:41:25 +01:00
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local/test/it', $header . $body, [
2018-10-11 22:18:27 +02:00
'http_code' => 301,
'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local/test/it',
'redirect_url' => 'https://test.other'
]);
self::assertTrue($curlResult->isSuccess());
self::assertFalse($curlResult->isTimeout());
self::assertTrue($curlResult->isRedirectUrl());
self::assertSame($headerArray, $curlResult->getHeaders());
self::assertSame($body, $curlResult->getBodyString());
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
self::assertSame('https://test.local/test/it', $curlResult->getUrl());
self::assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
2018-10-11 22:18:27 +02:00
}
/**
* @small
*/
public function testTimeout()
{
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.head.php');
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
2018-10-11 22:18:27 +02:00
2023-02-12 15:41:25 +01:00
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local/test/it', $header . $body, [
2018-10-11 22:18:27 +02:00
'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');
self::assertFalse($curlResult->isSuccess());
self::assertTrue($curlResult->isTimeout());
self::assertFalse($curlResult->isRedirectUrl());
self::assertSame($headerArray, $curlResult->getHeaders());
self::assertSame($body, $curlResult->getBodyString());
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
self::assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
self::assertSame('Tested error', $curlResult->getError());
2018-10-11 22:18:27 +02:00
}
/**
* @small
* @runInSeparateProcess
* @preserveGlobalState disabled
2018-10-11 22:18:27 +02:00
*/
public function testRedirectHeader()
{
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.redirect');
$headerArray = include(__DIR__ . '/../../../../datasets/curl/about.redirect.php');
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
2018-10-11 22:18:27 +02:00
2023-02-12 15:41:25 +01:00
$curlResult = new CurlResult(new NullLogger(),'https://test.local/test/it?key=value', $header . $body, [
2018-10-11 22:18:27 +02:00
'http_code' => 301,
'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local/test/it?key=value',
]);
self::assertTrue($curlResult->isSuccess());
self::assertFalse($curlResult->isTimeout());
self::assertTrue($curlResult->isRedirectUrl());
self::assertSame($headerArray, $curlResult->getHeaders());
self::assertSame($body, $curlResult->getBodyString());
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
self::assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
self::assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
2018-10-11 22:18:27 +02:00
}
2019-10-02 11:26:52 +02:00
/**
* @small
*/
public function testInHeader()
{
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
2019-10-02 11:26:52 +02:00
2023-02-12 15:41:25 +01:00
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(),'https://test.local', $header . $body, [
2019-10-02 11:26:52 +02:00
'http_code' => 200,
'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local'
]);
self::assertTrue($curlResult->inHeader('vary'));
self::assertFalse($curlResult->inHeader('wrongHeader'));
2019-10-02 11:26:52 +02:00
}
/**
* @small
*/
public function testGetHeaderArray()
{
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
2019-10-02 11:26:52 +02:00
2023-02-12 15:41:25 +01:00
$curlResult = new \Friendica\Network\HTTPClient\Response\CurlResult(new NullLogger(), 'https://test.local', $header . $body, [
2019-10-02 11:26:52 +02:00
'http_code' => 200,
'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local'
]);
$headers = $curlResult->getHeaderArray();
self::assertNotEmpty($headers);
self::assertArrayHasKey('vary', $headers);
2019-10-02 11:26:52 +02:00
}
/**
* @small
*/
public function testGetHeaderWithParam()
{
$header = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.head');
$body = file_get_contents(__DIR__ . '/../../../../datasets/curl/about.body');
2019-10-02 11:26:52 +02:00
2023-02-12 15:41:25 +01:00
$curlResult = new CurlResult(new NullLogger(),'https://test.local', $header . $body, [
2019-10-02 11:26:52 +02:00
'http_code' => 200,
'content_type' => 'text/html; charset=utf-8',
'url' => 'https://test.local'
]);
self::assertNotEmpty($curlResult->getHeaders());
self::assertEmpty($curlResult->getHeader('wrongHeader'));
2019-10-02 11:26:52 +02:00
}
2018-10-11 22:18:27 +02:00
}