Skip to content

Commit 3dad325

Browse files
committed
Added VeryGenerator
1 parent df86eb5 commit 3dad325

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Diff for: spec/Generator/VaryGeneratorSpec.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common\Plugin\Generator;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
class VaryGeneratorSpec extends ObjectBehavior
9+
{
10+
public function let()
11+
{
12+
$this->beConstructedWith(['Authorization', 'Content-Type']);
13+
}
14+
15+
public function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Client\Common\Plugin\Generator\VaryGenerator');
18+
}
19+
20+
public function it_is_a_key_generator()
21+
{
22+
$this->shouldImplement('Http\Client\Common\Plugin\Generator\CacheKeyGenerator');
23+
}
24+
25+
public function it_generates_cache_from_request(RequestInterface $request)
26+
{
27+
$request->getMethod()->shouldBeCalled()->willReturn('GET');
28+
$request->getUri()->shouldBeCalled()->willReturn('http://example.com/foo');
29+
$request->getHeaderLine('Authorization')->shouldBeCalled()->willReturn('bar');
30+
$request->getHeaderLine('Content-Type')->shouldBeCalled()->willReturn('application/baz');
31+
32+
$this->generate($request)->shouldReturn('GET http://example.com/foo Authorization:"bar" Content-Type:"application/baz"');
33+
}
34+
}

Diff for: src/Generator/VaryGenerator.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Http\Client\Common\Plugin\Generator;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Generate a cache key and specify what headers you want to vary on.
9+
*
10+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class VaryGenerator implements CacheKeyGenerator
13+
{
14+
/**
15+
* The header names we should vary on.
16+
*
17+
* @var array
18+
*/
19+
private $headerNames;
20+
21+
/**
22+
* @param $headerNames
23+
*/
24+
public function __construct(array $headerNames)
25+
{
26+
$this->headerNames = $headerNames;
27+
}
28+
29+
public function generate(RequestInterface $request)
30+
{
31+
$concatenatedHeaders = [];
32+
foreach ($this->headerNames as $headerName) {
33+
$concatenatedHeaders[] = sprintf(' %s:"%s"', $headerName, $request->getHeaderLine($headerName));
34+
}
35+
36+
return $request->getMethod().' '.$request->getUri().implode('', $concatenatedHeaders);
37+
}
38+
}

0 commit comments

Comments
 (0)