Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Make request header blacklist case insensitive #202

Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/Cache/MockStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct($storagePath, array $requestHeadersBlacklist = [], a
$this->storagePath = $storagePath;

$this->namingStrategies[] = new SubfolderNamingStrategy($requestHeadersBlacklist);
$this->namingStrategies[] = new SubfolderNamingStrategy($requestHeadersBlacklist, false);
$this->namingStrategies[] = new LegacyNamingStrategy(true, $requestHeadersBlacklist);
$this->namingStrategies[] = new LegacyNamingStrategy(false, $requestHeadersBlacklist);

Expand Down
13 changes: 11 additions & 2 deletions src/Cache/NamingStrategy/AbstractNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ abstract class AbstractNamingStrategy implements NamingStrategyInterface
'Host',
CacheMiddleware::DEBUG_HEADER,
];
private $normalizeRequest;

public function __construct(array $blacklist = [])
public function __construct(array $blacklist = [], $normalizeRequest = true)
{
if (!empty($blacklist)) {
$this->blacklist = $blacklist;
}

$this->normalizeRequest = $normalizeRequest;
}

/**
Expand All @@ -38,14 +41,20 @@ public function __construct(array $blacklist = [])
*/
protected function getFingerprint(RequestInterface $request)
{
if ($this->normalizeRequest) {
$headers = array_diff_key(array_change_key_case($request->getHeaders()), array_change_key_case(array_flip($this->blacklist)));
} else {
$headers = array_diff_key($request->getHeaders(), array_flip($this->blacklist));
}

return md5(serialize([
'method' => $request->getMethod(),
'path' => $request->getUri()->getPath(),
'query' => $request->getUri()->getQuery(),
'user_info' => $request->getUri()->getUserInfo(),
'port' => $request->getUri()->getPort(),
'scheme' => $request->getUri()->getScheme(),
'headers' => array_diff_key($request->getHeaders(), array_flip($this->blacklist)),
'headers' => $headers,
]));
}
}
2 changes: 1 addition & 1 deletion src/Cache/NamingStrategy/LegacyNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct($withHost, array $blacklist = [])
{
$this->withHost = $withHost;

parent::__construct($blacklist);
parent::__construct($blacklist, false);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/Cache/MockStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public function testFetch()
public function testSave()
{
$request = $this->getRequestMock();
$mockStorage = new $this->class($this->tmpDir, [], ['X-Foo']);
Copy link
Owner

Choose a reason for hiding this comment

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

This test should not be altered.

On the other hand, we should create tests which test specifically the naming strategies.

$mockStorage->save($request, new Response(404, ['X-Foo' => 'bar'], 'Not found'));
$request2 = $request->withHeader('X-Baz', 'qux');
$mockStorage = new $this->class($this->tmpDir, ['x-baz'], ['X-Foo']);
$mockStorage->save($request2, new Response(404, ['X-Foo' => 'bar'], 'Not found'));
$response = $mockStorage->fetch($request);

$this->assertCount(1, glob($this->tmpDir.'/google.com/GET_*'));
Expand Down