-
-
Notifications
You must be signed in to change notification settings - Fork 600
Make sure we vary on the Authorization header #558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1741eee
Make sure we vary on the Authorization header
Nyholm 379135c
Updated name
Nyholm 857ae23
Added Content-type
Nyholm bcc093e
Allow us to test unreleased versions
GrahamCampbell 2511479
Added functional tests
Nyholm 87f6895
Reduced unneeded code
Nyholm b4a116e
updateing deps
Nyholm db55a26
Do not use any php5.5+ code
Nyholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Github\Tests\Functional; | ||
|
||
use Cache\Adapter\PHPArray\ArrayCachePool; | ||
use Github\Client; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
/** | ||
* @group functional | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class CacheTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldServeCachedResponse() | ||
{ | ||
$mockClient = new \Http\Mock\Client(); | ||
$mockClient->addResponse($this->getCurrentUserResponse('nyholm')); | ||
$mockClient->addResponse($this->getCurrentUserResponse('octocat')); | ||
|
||
$github = Client::createWithHttpClient($mockClient); | ||
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]); | ||
|
||
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN); | ||
$userA = $github->currentUser()->show(); | ||
$this->assertEquals('nyholm', $userA['login']); | ||
|
||
$userB = $github->currentUser()->show(); | ||
$this->assertEquals('nyholm', $userB['login'], 'Two request following each other should be cached.'); | ||
} | ||
/** | ||
* @test | ||
*/ | ||
public function shouldVaryOnAuthorization() | ||
{ | ||
$mockClient = new \Http\Mock\Client(); | ||
$mockClient->addResponse($this->getCurrentUserResponse('nyholm')); | ||
$mockClient->addResponse($this->getCurrentUserResponse('octocat')); | ||
|
||
$github = Client::createWithHttpClient($mockClient); | ||
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]); | ||
|
||
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN); | ||
$userA = $github->currentUser()->show(); | ||
$this->assertEquals('nyholm', $userA['login']); | ||
|
||
$github->authenticate('fake_token_bbb', Client::AUTH_HTTP_TOKEN); | ||
$userB = $github->currentUser()->show(); | ||
$this->assertEquals('octocat', $userB['login'], 'We must vary on the Authorization header.'); | ||
} | ||
|
||
private function getCurrentUserResponse($username) | ||
{ | ||
$headers = [ | ||
'Content-Type' => 'application/json', | ||
]; | ||
|
||
$body = \GuzzleHttp\Psr7\stream_for(json_encode([ | ||
'login' => $username, | ||
])); | ||
|
||
return new Response(200, $headers, $body); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Client should always send a request, and then see if we get a "not modified" response. We shouldn't just randomly cache for 600 seconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, not always. If server says that it is okey to cache for X time, we do not need to send a request for that period.
But the logic you are referring to is in the php-http/cache-plugin.
I set the default ttl to arbitrary number here to force the first the request to be cached (for a time longer then the tests run). Doing that I can easily verify if we make a second request or not. (Im using the MockClient, Im not actually making any HTTP calls. )