Skip to content

Commit b48809e

Browse files
authored
Merge pull request #2935 from teohhanhui/feature/http-client-default-options
Allow setting default options on the test Client
2 parents b790b2c + 68bfdd6 commit b48809e

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ protected function doTearDown(): void
3737
/**
3838
* Creates a Client.
3939
*
40-
* @param array $options An array of options to pass to the createKernel method
40+
* @param array $kernelOptions Options to pass to the createKernel method
41+
* @param array $defaultOptions Default options for the requests
4142
*/
42-
protected static function createClient(array $options = []): Client
43+
protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client
4344
{
44-
$kernel = static::bootKernel($options);
45+
$kernel = static::bootKernel($kernelOptions);
4546

4647
try {
4748
/**
@@ -55,6 +56,8 @@ protected static function createClient(array $options = []): Client
5556
throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit".');
5657
}
5758

59+
$client->setDefaultOptions($defaultOptions);
60+
5861
self::getHttpClient($client);
5962
self::getClient($client->getKernelBrowser());
6063

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@
3131
*/
3232
final class Client implements HttpClientInterface
3333
{
34-
/**
35-
* @var Response
36-
*/
37-
private $response;
34+
use HttpClientTrait;
3835

3936
/**
4037
* @see HttpClientInterface::OPTIONS_DEFAULTS
4138
*/
42-
public const OPTIONS_DEFAULT = [
39+
public const API_OPTIONS_DEFAULTS = [
4340
'auth_basic' => null,
4441
'auth_bearer' => null,
4542
'query' => [],
@@ -49,27 +46,48 @@ final class Client implements HttpClientInterface
4946
'base_uri' => 'http://example.com',
5047
];
5148

52-
use HttpClientTrait;
53-
5449
private $kernelBrowser;
5550

56-
public function __construct(KernelBrowser $kernelBrowser)
51+
private $defaultOptions = self::API_OPTIONS_DEFAULTS;
52+
53+
/**
54+
* @var Response
55+
*/
56+
private $response;
57+
58+
/**
59+
* @param array $defaultOptions Default options for the requests
60+
*
61+
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
62+
*/
63+
public function __construct(KernelBrowser $kernelBrowser, array $defaultOptions = [])
5764
{
5865
$this->kernelBrowser = $kernelBrowser;
5966
$kernelBrowser->followRedirects(false);
67+
if ($defaultOptions) {
68+
$this->setDefaultOptions($defaultOptions);
69+
}
6070
}
6171

6272
/**
63-
* {@inheritdoc}
73+
* Sets the default options for the requests.
6474
*
65-
* @see Client::OPTIONS_DEFAULTS for available options
75+
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
76+
*/
77+
public function setDefaultOptions(array $defaultOptions): void
78+
{
79+
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::API_OPTIONS_DEFAULTS);
80+
}
81+
82+
/**
83+
* {@inheritdoc}
6684
*
6785
* @return Response
6886
*/
6987
public function request(string $method, string $url, array $options = []): ResponseInterface
7088
{
7189
$basic = $options['auth_basic'] ?? null;
72-
[$url, $options] = self::prepareRequest($method, $url, $options, self::OPTIONS_DEFAULT);
90+
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
7391
$resolvedUrl = implode('', $url);
7492

7593
$server = [];

tests/Bridge/Symfony/Bundle/Test/ClientTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ public function testCustomHeader(): void
6767
$this->assertStringContainsString('<name>Kevin</name>', $response->getContent());
6868
}
6969

70+
public function testDefaultHeaders(): void
71+
{
72+
$client = self::createClient([], [
73+
'headers' => [
74+
'content-type' => 'application/json',
75+
'accept' => 'text/xml',
76+
],
77+
]);
78+
$client->disableReboot();
79+
80+
$response = $client->request('POST', '/dummies', [
81+
'body' => '{"name": "Kevin"}',
82+
]);
83+
84+
$this->assertSame('application/xml; charset=utf-8', $response->getHeaders()['content-type'][0]);
85+
$this->assertResponseHeaderSame('content-type', 'application/xml; charset=utf-8');
86+
$this->assertStringContainsString('<name>Kevin</name>', $response->getContent());
87+
}
88+
7089
/**
7190
* @dataProvider authBasicProvider
7291
*/

0 commit comments

Comments
 (0)