Skip to content

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 9 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions spec/Cache/Generator/HeaderCacheKeyGeneratorSpec.php
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"');
}
}
38 changes: 38 additions & 0 deletions src/Cache/Generator/HeaderCacheKeyGenerator.php
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));
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

return $request->getMethod().' '.$request->getUri().implode('', $concatenatedHeaders);
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

}
}