Skip to content

Commit

Permalink
googleads#640 handlers stack of provided Guzzle HTTP client should no…
Browse files Browse the repository at this point in the history
…t be changed by library
  • Loading branch information
Vladimir Pavlikov committed Feb 6, 2020
1 parent 656e27b commit 822dd2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/Google/AdsApi/Common/AdsGuzzleHttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function __construct(
public function generateHttpClient()
{
$config = $this->config;
if (!array_key_exists('handler', $config)
|| $config['handler'] === null) {
$config['handler'] = HandlerStack::create();
}

$config['handler'] = isset($config['handler'])
? clone $config['handler']
: HandlerStack::create();

// Add a logging middleware required by this library.
$config['handler']->before(
Expand Down
22 changes: 17 additions & 5 deletions tests/Google/AdsApi/Common/AdsGuzzleHttpClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function testGenerateHttpClient_userProvidedStack()
);
$stack->before('http_errors', Middleware::tap());
$client = new Client(['handler' => $stack]);
$originalStack = clone $stack;

$httpClientFactory = new AdsGuzzleHttpClientFactory(
$logger,
Expand All @@ -80,7 +81,12 @@ public function testGenerateHttpClient_userProvidedStack()

$this->assertNotNull($httpClient);
$this->assertInstanceOf(Client::class, $httpClient);
$this->assertEquals($stack, $httpClient->getConfig()['handler']);
$this->assertEquals($originalStack, $stack, 'Stack of original HTTP client should stay unchanged');
$this->assertNotEquals(
$stack,
$httpClient->getConfig()['handler'],
'Stack of factory created HTTP client should have logging middleware, thus differ from original'
);
}

/**
Expand All @@ -97,10 +103,11 @@ public function testGenerateHttpClient_userProvidedConfigs()
GuzzleLogMessageHandler::log($logger, $guzzleLogMessageFormatter)
);
$client = new Client([
'handler' => $stack,
'verify' => true,
'cookies' => false
'handler' => $stack,
'verify' => true,
'cookies' => false
]);
$originalStack = clone $stack;

$httpClientFactory = new AdsGuzzleHttpClientFactory(
$logger,
Expand All @@ -111,7 +118,12 @@ public function testGenerateHttpClient_userProvidedConfigs()

$this->assertNotNull($httpClient);
$this->assertInstanceOf(Client::class, $httpClient);
$this->assertEquals($stack, $httpClient->getConfig()['handler']);
$this->assertEquals($originalStack, $stack, 'Stack of original HTTP client should stay unchanged');
$this->assertNotEquals(
$stack,
$httpClient->getConfig()['handler'],
'Stack of factory created HTTP Client should have logging middleware, thus differ from original'
);
$this->assertTrue($httpClient->getConfig()['verify']);
$this->assertFalse($httpClient->getConfig()['cookies']);
}
Expand Down

0 comments on commit 822dd2d

Please sign in to comment.