summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwn_ <[email protected]>2023-12-23 19:52:56 +0000
committerwn_ <[email protected]>2023-12-23 19:52:56 +0000
commit3c171cc92c2c62167b338b9eb79bb1ff973fd356 (patch)
tree26561d34106e9ffc0ef7245eb2bb4b6fc7855c19
parente33b0297d59a47fc5819ce79bdf71d29ad5e88b5 (diff)
Add some tests for UrlHelper::fetch()
-rw-r--r--classes/UrlHelper.php5
-rw-r--r--tests/UrlHelperTest.php68
2 files changed, 66 insertions, 7 deletions
diff --git a/classes/UrlHelper.php b/classes/UrlHelper.php
index e6b3b0aad..d088a355b 100644
--- a/classes/UrlHelper.php
+++ b/classes/UrlHelper.php
@@ -18,7 +18,7 @@ class UrlHelper {
static string $fetch_effective_url;
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 {
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'
// to attempt compatibility with unusual configurations.
- if ($login && $pass && self::$fetch_last_error_code === 403
- && isset($options['auth_type']) && $options['auth_type'] !== 'any') {
+ if ($login && $pass && self::$fetch_last_error_code === 403 && $auth_type !== 'any') {
$options['auth_type'] = 'any';
$span->end();
return self::fetch($options);
diff --git a/tests/UrlHelperTest.php b/tests/UrlHelperTest.php
index fe4eb5db2..2170ed50b 100644
--- a/tests/UrlHelperTest.php
+++ b/tests/UrlHelperTest.php
@@ -1,5 +1,11 @@
<?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;
final class UrlHelperTest extends TestCase {
@@ -13,16 +19,22 @@ final class UrlHelperTest extends TestCase {
// magnet allowed because it's a href attribute
$this->assertEquals(
'magnet:?xt=urn:btih:...',
- UrlHelper::rewrite_relative('http://example.com/example/',
+ UrlHelper::rewrite_relative(
+ 'http://example.com/example/',
'magnet:?xt=urn:btih:...',
- "a", "href", "")
+ "a",
+ "href",
+ ""
+ )
);
// disallowed magnet
$this->assertEquals(
'http://example.com?xt=urn:btih:...',
- UrlHelper::rewrite_relative('http://example.com/example/',
- 'magnet:?xt=urn:btih:...')
+ UrlHelper::rewrite_relative(
+ 'http://example.com/example/',
+ 'magnet:?xt=urn:btih:...'
+ )
);
$this->assertEquals(
@@ -49,6 +61,54 @@ final class UrlHelperTest extends TestCase {
'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);
}
}