Fix specifying auth type in UrlHelper::fetch(), add a test for 403 auth retry.

This commit is contained in:
wn_ 2023-12-24 11:21:43 +00:00
parent 9a1f7c2ebf
commit 0ea9db3170
2 changed files with 15 additions and 9 deletions

View File

@ -333,7 +333,7 @@ class UrlHelper {
if ($login && $pass && in_array($auth_type, ['basic', 'digest', 'ntlm'])) { if ($login && $pass && in_array($auth_type, ['basic', 'digest', 'ntlm'])) {
// Let Guzzle handle the details for auth types it supports // 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') { } elseif ($auth_type === 'any') {
// https://docs.guzzlephp.org/en/stable/faq.html#how-can-i-add-custom-curl-options // https://docs.guzzlephp.org/en/stable/faq.html#how-can-i-add-custom-curl-options
$req_options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_ANY; $req_options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_ANY;

View File

@ -102,13 +102,19 @@ final class UrlHelperTest extends TestCase {
$this->assertFalse($result); $this->assertFalse($result);
$this->assertEquals('Successful response, but no content was received.', UrlHelper::$fetch_last_error); $this->assertEquals('Successful response, but no content was received.', UrlHelper::$fetch_last_error);
// Currently failing with `Error: Undefined constant "CURLOPT_HTTPAUTH"`. // Fake a 403 for basic auth and success with `CURLAUTH_ANY` in the retry attempt
// $mock->append( $mock->append(
// new Response(403, []), new Response(403, []),
// new Response(200, [], 'Hello, World'), new Response(200, [], 'Hello, World'),
// ); );
// $result = UrlHelper::fetch(['url' => 'https://example.com/requires-credentials', 'login' => 'some_username', 'pass' => 'some_password']); $result = UrlHelper::fetch([
// $this->assertEquals(200, UrlHelper::$fetch_last_error_code); 'url' => 'https://example.com/requires-credentials',
// $this->assertEquals('Hello, World', $result); '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);
} }
} }