From 3c171cc92c2c62167b338b9eb79bb1ff973fd356 Mon Sep 17 00:00:00 2001 From: wn_ Date: Sat, 23 Dec 2023 19:52:56 +0000 Subject: Add some tests for UrlHelper::fetch() --- tests/UrlHelperTest.php | 68 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) (limited to 'tests') 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 @@ 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); } } -- cgit v1.2.3-54-g00ecf From 9a1f7c2ebfe4470440097c403cec2da011f22d02 Mon Sep 17 00:00:00 2001 From: wn_ Date: Sat, 23 Dec 2023 19:58:39 +0000 Subject: Appease PHPStan in UrlHelperTest --- tests/UrlHelperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/UrlHelperTest.php b/tests/UrlHelperTest.php index 2170ed50b..30a0201c1 100644 --- a/tests/UrlHelperTest.php +++ b/tests/UrlHelperTest.php @@ -80,7 +80,7 @@ final class UrlHelperTest extends TestCase { $this->assertFalse($result); } - $mock->append(new Response(200, ['Content-Length' => PHP_INT_MAX])); + $mock->append(new Response(200, ['Content-Length' => (string) PHP_INT_MAX])); $result = UrlHelper::fetch('https://www.example.com/very-large-content-length'); $this->assertFalse($result); -- cgit v1.2.3-54-g00ecf From 0ea9db317038f5510a1ca875b55af770997ec148 Mon Sep 17 00:00:00 2001 From: wn_ Date: Sun, 24 Dec 2023 11:21:43 +0000 Subject: Fix specifying auth type in UrlHelper::fetch(), add a test for 403 auth retry. --- classes/UrlHelper.php | 2 +- tests/UrlHelperTest.php | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/classes/UrlHelper.php b/classes/UrlHelper.php index d088a355b..03202cff8 100644 --- a/classes/UrlHelper.php +++ b/classes/UrlHelper.php @@ -333,7 +333,7 @@ class UrlHelper { if ($login && $pass && in_array($auth_type, ['basic', 'digest', 'ntlm'])) { // Let Guzzle handle the details for auth types it supports - $req_options[GuzzleHttp\RequestOptions::AUTH] = [$login, $pass]; + $req_options[GuzzleHttp\RequestOptions::AUTH] = [$login, $pass, $auth_type]; } elseif ($auth_type === 'any') { // https://docs.guzzlephp.org/en/stable/faq.html#how-can-i-add-custom-curl-options $req_options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_ANY; diff --git a/tests/UrlHelperTest.php b/tests/UrlHelperTest.php index 30a0201c1..58960add0 100644 --- a/tests/UrlHelperTest.php +++ b/tests/UrlHelperTest.php @@ -102,13 +102,19 @@ final class UrlHelperTest extends TestCase { $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); + // Fake a 403 for basic auth and success with `CURLAUTH_ANY` in the retry attempt + $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', + 'auth_type' => 'basic', + ]); + $this->assertEquals(200, UrlHelper::$fetch_last_error_code); + $this->assertEquals('Hello, World', $result); + $this->assertEquals($mock->getLastOptions()['curl'][\CURLOPT_HTTPAUTH], \CURLAUTH_ANY); } } -- cgit v1.2.3-54-g00ecf