Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1293cfe

Browse filesBrowse files
committed
[HttpClient] Add new bearer option
1 parent 29f81b0 commit 1293cfe
Copy full SHA for 1293cfe

File tree

3 files changed

+20
-2
lines changed
Filter options

3 files changed

+20
-2
lines changed

‎src/Symfony/Component/HttpClient/HttpClientTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/HttpClientTrait.php
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,22 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
7575
throw new InvalidArgumentException(sprintf('Option "auth" must be string, %s given.', \gettype($options['auth'])));
7676
}
7777

78+
if (!\is_string($options['bearer'] ?? '')) {
79+
throw new InvalidArgumentException(sprintf('Option "bearer" must be string, %s given.', \gettype($options['bearer'])));
80+
}
81+
7882
if (null !== $url) {
7983
// Merge auth with headers
8084
if (($options['auth'] ?? false) && !($headers['authorization'] ?? false)) {
8185
$rawHeaders[] = 'authorization: '.$headers['authorization'][] = 'Basic '.base64_encode($options['auth']);
8286
}
87+
// Merge bearer with headers
88+
if (($options['bearer'] ?? false) && !($headers['authorization'] ?? false)) {
89+
$rawHeaders[] = 'authorization: '.$headers['authorization'][] = 'Bearer '.$options['bearer'];
90+
}
8391

8492
$options['raw_headers'] = $rawHeaders;
85-
unset($options['auth']);
93+
unset($options['auth'], $options['bearer']);
8694

8795
// Parse base URI
8896
if (\is_string($options['base_uri'])) {

‎src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\HttpClient\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\HttpClient;
1516
use Symfony\Component\HttpClient\HttpClientTrait;
17+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1618

1719
class HttpClientTraitTest extends TestCase
1820
{
@@ -163,4 +165,11 @@ public function provideRemoveDotSegments()
163165
yield ['/a/', '/a/b/..'];
164166
yield ['/a///b', '/a///b'];
165167
}
168+
169+
public function testBearerOption()
170+
{
171+
[, $options] = self::prepareRequest('POST', 'http://example.com', ['bearer' => 'foobar'], HttpClientInterface::OPTIONS_DEFAULTS);
172+
$this->assertSame('Bearer foobar', $options['headers']['authorization'][0]);
173+
$this->assertSame('authorization: Bearer foobar', $options['raw_headers'][0]);
174+
}
166175
}

‎src/Symfony/Contracts/HttpClient/HttpClientInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/HttpClient/HttpClientInterface.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
interface HttpClientInterface
2727
{
2828
public const OPTIONS_DEFAULTS = [
29-
'auth' => null, // string - a username:password enabling HTTP Basic authentication
29+
'auth' => null, // string - a username:password enabling HTTP Basic authentication (RFC 7617)
30+
'bearer' => null, // string - a token enabling HTTP Bearer authorization (RFC 6750)
3031
'query' => [], // string[] - associative array of query string values to merge with the request's URL
3132
'headers' => [], // iterable|string[]|string[][] - headers names provided as keys or as part of values
3233
'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.