Skip to content

Commit 822dd2d

Browse files
author
Vladimir Pavlikov
committed
#640 handlers stack of provided Guzzle HTTP client should not be changed by library
1 parent 656e27b commit 822dd2d

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/Google/AdsApi/Common/AdsGuzzleHttpClientFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public function __construct(
6464
public function generateHttpClient()
6565
{
6666
$config = $this->config;
67-
if (!array_key_exists('handler', $config)
68-
|| $config['handler'] === null) {
69-
$config['handler'] = HandlerStack::create();
70-
}
67+
68+
$config['handler'] = isset($config['handler'])
69+
? clone $config['handler']
70+
: HandlerStack::create();
7171

7272
// Add a logging middleware required by this library.
7373
$config['handler']->before(

tests/Google/AdsApi/Common/AdsGuzzleHttpClientFactoryTest.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testGenerateHttpClient_userProvidedStack()
7070
);
7171
$stack->before('http_errors', Middleware::tap());
7272
$client = new Client(['handler' => $stack]);
73+
$originalStack = clone $stack;
7374

7475
$httpClientFactory = new AdsGuzzleHttpClientFactory(
7576
$logger,
@@ -80,7 +81,12 @@ public function testGenerateHttpClient_userProvidedStack()
8081

8182
$this->assertNotNull($httpClient);
8283
$this->assertInstanceOf(Client::class, $httpClient);
83-
$this->assertEquals($stack, $httpClient->getConfig()['handler']);
84+
$this->assertEquals($originalStack, $stack, 'Stack of original HTTP client should stay unchanged');
85+
$this->assertNotEquals(
86+
$stack,
87+
$httpClient->getConfig()['handler'],
88+
'Stack of factory created HTTP client should have logging middleware, thus differ from original'
89+
);
8490
}
8591

8692
/**
@@ -97,10 +103,11 @@ public function testGenerateHttpClient_userProvidedConfigs()
97103
GuzzleLogMessageHandler::log($logger, $guzzleLogMessageFormatter)
98104
);
99105
$client = new Client([
100-
'handler' => $stack,
101-
'verify' => true,
102-
'cookies' => false
106+
'handler' => $stack,
107+
'verify' => true,
108+
'cookies' => false
103109
]);
110+
$originalStack = clone $stack;
104111

105112
$httpClientFactory = new AdsGuzzleHttpClientFactory(
106113
$logger,
@@ -111,7 +118,12 @@ public function testGenerateHttpClient_userProvidedConfigs()
111118

112119
$this->assertNotNull($httpClient);
113120
$this->assertInstanceOf(Client::class, $httpClient);
114-
$this->assertEquals($stack, $httpClient->getConfig()['handler']);
121+
$this->assertEquals($originalStack, $stack, 'Stack of original HTTP client should stay unchanged');
122+
$this->assertNotEquals(
123+
$stack,
124+
$httpClient->getConfig()['handler'],
125+
'Stack of factory created HTTP Client should have logging middleware, thus differ from original'
126+
);
115127
$this->assertTrue($httpClient->getConfig()['verify']);
116128
$this->assertFalse($httpClient->getConfig()['cookies']);
117129
}

0 commit comments

Comments
 (0)