summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2023-12-24 16:14:45 +0000
committerAndrew Dolgov <[email protected]>2023-12-24 16:14:45 +0000
commit51cd02fc3e476f9e64311a87e0b84dbd16f5a44f (patch)
tree1a6be722f06c261aa7e28dd2a4a9665bf5cb949e /tests
parentd4ae6c67db8c966ab4998fda6df14072b103106b (diff)
parent0ea9db317038f5510a1ca875b55af770997ec148 (diff)
Merge branch 'feature/use-guzzle' into 'master'
Use Guzzle See merge request tt-rss/tt-rss!16
Diffstat (limited to 'tests')
-rw-r--r--tests/UrlHelperTest.php74
1 files changed, 70 insertions, 4 deletions
diff --git a/tests/UrlHelperTest.php b/tests/UrlHelperTest.php
index fe4eb5db2..58960add0 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,60 @@ 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' => (string) 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);
+
+ // 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);
}
}