-
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
Changes from 7 commits
48a1228
d26041e
de5611f
c340f6b
ec0f958
4a1c4ef
ea1ac1b
04935d9
9cdf59b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin\Cache\Generator; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
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) | ||
{ | ||
$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'); | ||
|
||
$this->generate($request)->shouldReturn('GET http://example.com/foo Authorization:"bar" Content-Type:"application/baz"'); | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be consistent with the simple generator, we should also add the body. we might just extend the simple generator and concat parent::generate with the headers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe in cohesion over inheritance here. We should create some kind of chaining or use a separate class for chaining There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can also just add the body from simple generator. its just one line of code, i think we don't need to get too complicated with this. but we should have the body to not have inconsistent behaviour between the two generators There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okey, done. We do not need the if(empty()) check because it was added in the SimpleGenerator because of BC. |
||
} | ||
} |
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.
👍