Add some tests for UrlHelper::fetch()

This commit is contained in:
wn_ 2023-12-23 19:52:56 +00:00
parent e33b0297d5
commit 3c171cc92c
2 changed files with 66 additions and 7 deletions

View File

@ -18,7 +18,7 @@ class UrlHelper {
static string $fetch_effective_url; static string $fetch_effective_url;
static string $fetch_effective_ip_addr; static string $fetch_effective_ip_addr;
private static ?GuzzleHttp\ClientInterface $client = null; public static ?GuzzleHttp\ClientInterface $client = null;
private static function get_client(): GuzzleHttp\ClientInterface { private static function get_client(): GuzzleHttp\ClientInterface {
if (self::$client == null) { if (self::$client == null) {
@ -385,8 +385,7 @@ class UrlHelper {
// If credentials were provided and we got a 403 back, retry once with auth type 'any' // If credentials were provided and we got a 403 back, retry once with auth type 'any'
// to attempt compatibility with unusual configurations. // to attempt compatibility with unusual configurations.
if ($login && $pass && self::$fetch_last_error_code === 403 if ($login && $pass && self::$fetch_last_error_code === 403 && $auth_type !== 'any') {
&& isset($options['auth_type']) && $options['auth_type'] !== 'any') {
$options['auth_type'] = 'any'; $options['auth_type'] = 'any';
$span->end(); $span->end();
return self::fetch($options); return self::fetch($options);

View File

@ -1,5 +1,11 @@
<?php <?php
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class UrlHelperTest extends TestCase { final class UrlHelperTest extends TestCase {
@ -13,16 +19,22 @@ final class UrlHelperTest extends TestCase {
// magnet allowed because it's a href attribute // magnet allowed because it's a href attribute
$this->assertEquals( $this->assertEquals(
'magnet:?xt=urn:btih:...', 'magnet:?xt=urn:btih:...',
UrlHelper::rewrite_relative('http://example.com/example/', UrlHelper::rewrite_relative(
'http://example.com/example/',
'magnet:?xt=urn:btih:...', 'magnet:?xt=urn:btih:...',
"a", "href", "") "a",
"href",
""
)
); );
// disallowed magnet // disallowed magnet
$this->assertEquals( $this->assertEquals(
'http://example.com?xt=urn:btih:...', 'http://example.com?xt=urn:btih:...',
UrlHelper::rewrite_relative('http://example.com/example/', UrlHelper::rewrite_relative(
'magnet:?xt=urn:btih:...') 'http://example.com/example/',
'magnet:?xt=urn:btih:...'
)
); );
$this->assertEquals( $this->assertEquals(
@ -49,6 +61,54 @@ final class UrlHelperTest extends TestCase {
'http://www.example.com/test', 'http://www.example.com/test',
UrlHelper::rewrite_relative('http://www.example.com/test2 ', 'http://www.example.com/test') UrlHelper::rewrite_relative('http://www.example.com/test2 ', 'http://www.example.com/test')
); );
}
public function test_fetch(): void {
$mock = new MockHandler();
UrlHelper::$client = new Client([
'handler' => HandlerStack::create($mock),
]);
$mock->append(new Response(200, [], 'Hello, World'));
$result = UrlHelper::fetch('https://www.example.com');
$this->assertEquals(200, UrlHelper::$fetch_last_error_code);
$this->assertEquals('Hello, World', $result);
foreach (['ftp://ftp.example.com', 'http://127.0.0.1', 'blah', '', 42, null] as $url) {
$result = UrlHelper::fetch($url);
$this->assertFalse($result);
}
$mock->append(new Response(200, ['Content-Length' => PHP_INT_MAX]));
$result = UrlHelper::fetch('https://www.example.com/very-large-content-length');
$this->assertFalse($result);
$mock->append(new Response(301, ['Location' => 'https://www.example.com']));
$result = UrlHelper::fetch(['url' => 'https://example.com', 'followlocation' => false]);
$this->assertFalse($result);
$mock->append(
new Response(301, ['Location' => 'http://127.0.0.1']),
new Response(200, [], 'Hello, World'),
);
$result = UrlHelper::fetch(['url' => 'https://example.com', 'followlocation' => true]);
$this->assertFalse($result);
$this->assertEquals('URL received after redirection failed extended validation.', UrlHelper::$fetch_last_error);
$this->assertEquals('http://127.0.0.1', UrlHelper::$fetch_effective_url);
$mock->append(new Response(200, [], ''));
$result = UrlHelper::fetch('https://www.example.com');
$this->assertFalse($result);
$this->assertEquals('Successful response, but no content was received.', UrlHelper::$fetch_last_error);
// Currently failing with `Error: Undefined constant "CURLOPT_HTTPAUTH"`.
// $mock->append(
// new Response(403, []),
// new Response(200, [], 'Hello, World'),
// );
// $result = UrlHelper::fetch(['url' => 'https://example.com/requires-credentials', 'login' => 'some_username', 'pass' => 'some_password']);
// $this->assertEquals(200, UrlHelper::$fetch_last_error_code);
// $this->assertEquals('Hello, World', $result);
} }
} }