Skip to content

Commit 4a41538

Browse files
committed
Add Basic auth support
1 parent e9eb9ab commit 4a41538

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Bridge/Symfony/Bundle/Test/Client.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ final class Client implements HttpClientInterface
3838
'json' => null, // array|\JsonSerializable - when set, implementations MUST set the "body" option to
3939
// the JSON-encoded value and set the "content-type" headers to a JSON-compatible
4040
'base_uri' => 'http://example.com', // string - the URI to resolve relative URLs, following rules in RFC 3986, section 2
41-
'bearer' => null, // string - sets a corresponding Authorization token of the Bearer type
41+
'auth' => null, // string - a username:password enabling HTTP Basic authentication (RFC 7617)
42+
'bearer' => null, // string - a token enabling HTTP Bearer authorization (RFC 6750)
4243
];
4344

4445
use HttpClientTrait;
@@ -82,16 +83,18 @@ public function request(string $method, string $url, array $options = []): Respo
8283
}
8384
}
8485

86+
// TODO: remove when https://github.com/symfony/symfony/pull/30547 will be merged
8587
if (isset($options['bearer']) && \is_string($options['bearer'])) {
8688
$options['headers']['authorization'] = ['Bearer '.$options['bearer']];
8789
}
8890

91+
$basic = $options['auth'] ?? null;
8992
[$url, $options] = $this->prepareRequest($method, $url, $options, self::OPTIONS_DEFAULT);
9093

9194
$server = [];
9295
// Convert headers to a $_SERVER-like array
9396
foreach ($options['headers'] as $key => $value) {
94-
if ('content-type' === strtolower($key)) {
97+
if ('content-type' === $key) {
9598
$server['CONTENT_TYPE'] = $value[0] ?? '';
9699

97100
continue;
@@ -101,6 +104,12 @@ public function request(string $method, string $url, array $options = []): Respo
101104
$server['HTTP_'.strtoupper(str_replace('-', '_', $key))] = $value[0] ?? '';
102105
}
103106

107+
if ($basic) {
108+
$credentials = explode(':', $basic, 2);
109+
$server['PHP_AUTH_USER'] = $credentials[0];
110+
$server['PHP_AUTH_PW'] = $credentials[1] ?? '';
111+
}
112+
104113
$info = [
105114
'redirect_count' => 0,
106115
'redirect_url' => null,

0 commit comments

Comments
 (0)