Skip to content

Commit 0d53ac3

Browse files
committed
[HttpClient] Allow to pass user/pw as an array
1 parent 2c58d6a commit 0d53ac3

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

HttpClientTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
7171
throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, %s given.', \is_object($onProgress) ? \get_class($onProgress) : \gettype($onProgress)));
7272
}
7373

74+
if (\is_array($options['auth_basic'] ?? null)) {
75+
$count = \count($options['auth_basic']);
76+
if ($count <= 0 || $count > 2) {
77+
throw new InvalidArgumentException(sprintf('Option "auth_basic" must contain 1 or 2 elements, %s given.', $count));
78+
}
79+
80+
$options['auth_basic'] = implode(':', $options['auth_basic']);
81+
}
82+
7483
if (!\is_string($options['auth_basic'] ?? '')) {
75-
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string, %s given.', \gettype($options['auth_basic'])));
84+
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string or an array, %s given.', \gettype($options['auth_basic'])));
7685
}
7786

7887
if (!\is_string($options['auth_bearer'] ?? '')) {

HttpOptions.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ public function toArray(): array
3434
/**
3535
* @return $this
3636
*/
37-
public function setAuthBasic(string $userinfo)
37+
public function setAuthBasic(string $user, string $password = '')
3838
{
39-
$this->options['auth'] = $userinfo;
39+
$this->options['auth_basic'] = $user;
40+
if ('' !== $password) {
41+
$this->options['auth_basic'] .= ':'.$password;
42+
}
4043

4144
return $this;
4245
}

Tests/HttpClientTraitTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,25 @@ public function testInvalidAuthBearerOption()
185185
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
186186
* @expectedExceptionMessage Define either the "auth_basic" or the "auth_bearer" option, setting both is not supported.
187187
*/
188-
public function testSetBasicAndBearerOption()
188+
public function testSetAuthBasicAndBearerOptions()
189189
{
190190
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foo', 'auth_basic' => 'foo:bar'], HttpClientInterface::OPTIONS_DEFAULTS);
191191
}
192+
193+
public function providePrepareAuthBasic()
194+
{
195+
yield ['foo:bar', 'Zm9vOmJhcg=='];
196+
yield [['foo', 'bar'], 'Zm9vOmJhcg=='];
197+
yield ['foo', 'Zm9v'];
198+
yield [['foo'], 'Zm9v'];
199+
}
200+
201+
/**
202+
* @dataProvider providePrepareAuthBasic
203+
*/
204+
public function testPrepareAuthBasic($arg, $result)
205+
{
206+
[, $options] = $this->prepareRequest('POST', 'http://example.com', ['auth_basic' => $arg], HttpClientInterface::OPTIONS_DEFAULTS);
207+
$this->assertSame('Basic '.$result, $options['headers']['authorization'][0]);
208+
}
192209
}

Tests/HttpOptionsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\HttpOptions;
16+
17+
/**
18+
* @author Kévin Dunglas <[email protected]>
19+
*/
20+
class HttpOptionsTest extends TestCase
21+
{
22+
public function provideSetAuth()
23+
{
24+
yield ['user:password', 'user', 'password'];
25+
yield ['user:password', 'user:password'];
26+
yield ['user', 'user'];
27+
yield ['user:0', 'user', '0'];
28+
}
29+
30+
/**
31+
* @dataProvider provideSetAuth
32+
*/
33+
public function testSetAuth(string $expected, string $user, string $password = '')
34+
{
35+
$this->assertSame($expected, (new HttpOptions())->setAuthBasic($user, $password)->toArray()['auth_basic']);
36+
}
37+
}

0 commit comments

Comments
 (0)