Skip to content

Commit 817a4d8

Browse files
author
Till Hildebrandt
committed
Fixing Pull Request Requests
- added spec tests for blacklisted paths - it_does_not_store_responses_of_requests_to_blacklisted_paths - it_stores_responses_of_requests_not_in_blacklisted_paths
1 parent 005d609 commit 817a4d8

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

spec/CachePluginSpec.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,98 @@ function it_caches_private_responses_when_allowed(
400400
$this->handleRequest($request, $next, function () {});
401401
}
402402

403+
function it_does_not_store_responses_of_requests_to_blacklisted_paths(
404+
CacheItemPoolInterface $pool,
405+
CacheItemInterface $item,
406+
RequestInterface $request,
407+
ResponseInterface $response,
408+
StreamFactory $streamFactory,
409+
StreamInterface $stream
410+
) {
411+
$this->beConstructedThrough('clientCache', [$pool, $streamFactory, [
412+
'default_ttl' => 60,
413+
'cache_lifetime' => 1000,
414+
'blacklisted_paths' => ['\/foo']
415+
]]);
416+
417+
$httpBody = 'body';
418+
$stream->__toString()->willReturn($httpBody);
419+
$stream->isSeekable()->willReturn(true);
420+
421+
$request->getMethod()->willReturn('GET');
422+
$request->getUri()->willReturn('/foo');
423+
$request->getBody()->shouldBeCalled();
424+
425+
$response->getStatusCode()->willReturn(200);
426+
$response->getBody()->willReturn($stream);
427+
$response->getHeader('Cache-Control')->willReturn([])->shouldBeCalled();
428+
429+
$pool->getItem('231392a16d98e1cf631845c79b7d45f40bab08f3')->shouldBeCalled()->willReturn($item);
430+
$item->isHit()->willReturn(false);
431+
432+
$item->set($this->getCacheItemMatcher([
433+
'response' => $response->getWrappedObject(),
434+
'body' => $httpBody,
435+
'expiresAt' => 0,
436+
'createdAt' => 0
437+
]))->willReturn($item)->shouldNotBeCalled();
438+
$pool->save(Argument::any())->shouldNotBeCalled();
439+
440+
$next = function (RequestInterface $request) use ($response) {
441+
return new FulfilledPromise($response->getWrappedObject());
442+
};
443+
444+
$this->handleRequest($request, $next, function () {});
445+
}
446+
447+
function it_stores_responses_of_requests_not_in_blacklisted_paths(
448+
CacheItemPoolInterface $pool,
449+
CacheItemInterface $item,
450+
RequestInterface $request,
451+
ResponseInterface $response,
452+
StreamFactory $streamFactory,
453+
StreamInterface $stream
454+
) {
455+
$this->beConstructedThrough('clientCache', [$pool, $streamFactory, [
456+
'default_ttl' => 60,
457+
'cache_lifetime' => 1000,
458+
'blacklisted_paths' => ['\/foo']
459+
]]);
460+
461+
$httpBody = 'body';
462+
$stream->__toString()->willReturn($httpBody);
463+
$stream->isSeekable()->willReturn(true);
464+
$stream->rewind()->shouldBeCalled();
465+
466+
$request->getMethod()->willReturn('GET');
467+
$request->getUri()->willReturn('/');
468+
$request->getBody()->shouldBeCalled();
469+
470+
$response->getStatusCode()->willReturn(200);
471+
$response->getBody()->willReturn($stream);
472+
$response->getHeader('Cache-Control')->willReturn([])->shouldBeCalled();
473+
$response->getHeader('Expires')->willReturn([])->shouldBeCalled();
474+
$response->getHeader('ETag')->willReturn([])->shouldBeCalled();
475+
476+
$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
477+
$item->isHit()->willReturn(false);
478+
$item->expiresAfter(1060)->willReturn($item)->shouldBeCalled();
479+
480+
$item->set($this->getCacheItemMatcher([
481+
'response' => $response->getWrappedObject(),
482+
'body' => $httpBody,
483+
'expiresAt' => 0,
484+
'createdAt' => 0,
485+
'etag' => []
486+
]))->willReturn($item)->shouldBeCalled();
487+
$pool->save(Argument::any())->shouldBeCalled();
488+
489+
$next = function (RequestInterface $request) use ($response) {
490+
return new FulfilledPromise($response->getWrappedObject());
491+
};
492+
493+
$this->handleRequest($request, $next, function () {});
494+
}
403495

404496
function it_can_be_initialized_with_custom_cache_key_generator(
405497
CacheItemPoolInterface $pool,

src/CachePlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ protected function isCacheable(ResponseInterface $response)
274274
protected function isCacheableRequest(RequestInterface $request)
275275
{
276276
foreach ($this->config['blacklisted_paths'] as $not_to_cache_path) {
277-
if (1 === preg_match('/'.$not_to_cache_path.'/', $request->getRequestTarget())) {
277+
if (1 === preg_match('/'.$not_to_cache_path.'/', $request->getUri())) {
278278
return false;
279279
}
280280
}

0 commit comments

Comments
 (0)