-
Notifications
You must be signed in to change notification settings - Fork 16
Added VaryGenerator #36
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
9 commits
Select commit
Hold shift + click to select a range
48a1228
Added VeryGenerator
Nyholm d26041e
Updated namespace
Nyholm de5611f
Renamed to HeaderHashGenerator
Nyholm c340f6b
Renamed to SharedCacheKeyGenerator
Nyholm ec0f958
Revert "Renamed to SharedCacheKeyGenerator"
Nyholm 4a1c4ef
More renaming
Nyholm ea1ac1b
Doc update
Nyholm 04935d9
Added body
Nyholm 9cdf59b
Fixed broken tests
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin\Cache\Generator; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
class HeaderCacheKeyGeneratorSpec extends ObjectBehavior | ||
{ | ||
public function let() | ||
{ | ||
$this->beConstructedWith(['Authorization', 'Content-Type']); | ||
} | ||
|
||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator'); | ||
} | ||
|
||
public function it_is_a_key_generator() | ||
{ | ||
$this->shouldImplement('Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator'); | ||
} | ||
|
||
public function it_generates_cache_from_request(RequestInterface $request, StreamInterface $body) | ||
{ | ||
$request->getMethod()->shouldBeCalled()->willReturn('GET'); | ||
$request->getUri()->shouldBeCalled()->willReturn('http://example.com/foo'); | ||
$request->getHeaderLine('Authorization')->shouldBeCalled()->willReturn('bar'); | ||
$request->getHeaderLine('Content-Type')->shouldBeCalled()->willReturn('application/baz'); | ||
$request->getBody()->shouldBeCalled()->willReturn($body); | ||
$body->__toString()->shouldBeCalled()->willReturn(''); | ||
|
||
$this->generate($request)->shouldReturn('GET http://example.com/foo Authorization:"bar" Content-Type:"application/baz" '); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin\Cache\Generator; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Generate a cache key by using HTTP headers. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class HeaderCacheKeyGenerator implements CacheKeyGenerator | ||
{ | ||
/** | ||
* The header names we should take into account when creating the cache key. | ||
* | ||
* @var array | ||
*/ | ||
private $headerNames; | ||
|
||
/** | ||
* @param $headerNames | ||
*/ | ||
public function __construct(array $headerNames) | ||
{ | ||
$this->headerNames = $headerNames; | ||
} | ||
|
||
public function generate(RequestInterface $request) | ||
{ | ||
$concatenatedHeaders = []; | ||
foreach ($this->headerNames as $headerName) { | ||
$concatenatedHeaders[] = sprintf(' %s:"%s"', $headerName, $request->getHeaderLine($headerName)); | ||
} | ||
|
||
return $request->getMethod().' '.$request->getUri().implode('', $concatenatedHeaders).' '.$request->getBody(); | ||
} | ||
} |
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.
calling getHeaderLine for a header that is not defined simply returns an empty string. just double-checked in the psr-7 spec. so this is fine
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.
👍