-
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 6 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 specify what headers you want to vary on. | ||
| * | ||
| * @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)); | ||
|
Contributor
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. 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
Member
Author
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. 👍 |
||
| } | ||
|
|
||
| return $request->getMethod().' '.$request->getUri().implode('', $concatenatedHeaders); | ||
|
||
| } | ||
| } | ||
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.
i'd like to use a different word than "vary" here to avoid confusion with the
Varyresponse header.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.
Thank you for the review