Skip to content

Commit 6b7b0aa

Browse files
authored
Make sure we vary on the Authorization header (#558)
* Make sure we vary on the Authorization header * Updated name * Added Content-type * Allow us to test unreleased versions * Added functional tests * Reduced unneeded code * updateing deps * Do not use any php5.5+ code
1 parent a6e9b06 commit 6b7b0aa

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

composer.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@
2424
"php-http/discovery": "^1.0",
2525
"php-http/client-implementation": "^1.0",
2626
"php-http/client-common": "^1.3",
27-
"php-http/cache-plugin": "^1.3"
27+
"php-http/cache-plugin": "^1.4"
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "^4.0 || ^5.5",
3131
"php-http/guzzle6-adapter": "^1.0",
32+
"php-http/mock-client": "^1.0",
3233
"guzzlehttp/psr7": "^1.2",
33-
"sllh/php-cs-fixer-styleci-bridge": "^1.3"
34+
"sllh/php-cs-fixer-styleci-bridge": "^1.3",
35+
"cache/array-adapter": "^0.4"
3436
},
3537
"autoload": {
3638
"psr-4": { "Github\\": "lib/Github/" }
3739
},
40+
"minimum-stability": "dev",
41+
"prefer-stable": true,
3842
"extra": {
3943
"branch-alias": {
4044
"dev-master": "2.3.x-dev"

lib/Github/HttpClient/Builder.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Http\Message\RequestFactory;
1414
use Http\Message\StreamFactory;
1515
use Psr\Cache\CacheItemPoolInterface;
16+
use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator;
1617

1718
/**
1819
* A builder that builds the API client.
@@ -181,6 +182,9 @@ public function addHeaderValue($header, $headerValue)
181182
*/
182183
public function addCache(CacheItemPoolInterface $cachePool, array $config = [])
183184
{
185+
if (!isset($config['cache_key_generator'])) {
186+
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']);
187+
}
184188
$this->cachePlugin = Plugin\CachePlugin::clientCache($cachePool, $this->streamFactory, $config);
185189
$this->httpClientModified = true;
186190
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Github\Tests\Functional;
4+
5+
use Cache\Adapter\PHPArray\ArrayCachePool;
6+
use Github\Client;
7+
use GuzzleHttp\Psr7\Response;
8+
9+
/**
10+
* @group functional
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class CacheTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @test
18+
*/
19+
public function shouldServeCachedResponse()
20+
{
21+
$mockClient = new \Http\Mock\Client();
22+
$mockClient->addResponse($this->getCurrentUserResponse('nyholm'));
23+
$mockClient->addResponse($this->getCurrentUserResponse('octocat'));
24+
25+
$github = Client::createWithHttpClient($mockClient);
26+
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]);
27+
28+
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN);
29+
$userA = $github->currentUser()->show();
30+
$this->assertEquals('nyholm', $userA['login']);
31+
32+
$userB = $github->currentUser()->show();
33+
$this->assertEquals('nyholm', $userB['login'], 'Two request following each other should be cached.');
34+
}
35+
/**
36+
* @test
37+
*/
38+
public function shouldVaryOnAuthorization()
39+
{
40+
$mockClient = new \Http\Mock\Client();
41+
$mockClient->addResponse($this->getCurrentUserResponse('nyholm'));
42+
$mockClient->addResponse($this->getCurrentUserResponse('octocat'));
43+
44+
$github = Client::createWithHttpClient($mockClient);
45+
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]);
46+
47+
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN);
48+
$userA = $github->currentUser()->show();
49+
$this->assertEquals('nyholm', $userA['login']);
50+
51+
$github->authenticate('fake_token_bbb', Client::AUTH_HTTP_TOKEN);
52+
$userB = $github->currentUser()->show();
53+
$this->assertEquals('octocat', $userB['login'], 'We must vary on the Authorization header.');
54+
}
55+
56+
private function getCurrentUserResponse($username)
57+
{
58+
$headers = [
59+
'Content-Type' => 'application/json',
60+
];
61+
62+
$body = \GuzzleHttp\Psr7\stream_for(json_encode([
63+
'login' => $username,
64+
]));
65+
66+
return new Response(200, $headers, $body);
67+
}
68+
}

0 commit comments

Comments
 (0)