Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ELPP-2452 Added cache headers to recommendations (#57)
* ELPP-2452 Added cache headers to recommendations * ELPP-2452 Added headers test * ELPP-2452 Changd status code * ELPP-2452 Added ping test * ELPP-2452 Added private response and test * ELPP-2452 Added private response and test
- Loading branch information
Showing
4 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace eLife\Recommendations\Response; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
final class PrivateResponse extends Response | ||
{ | ||
public function __construct($content = '', $status = 200, array $headers = array()) | ||
{ | ||
parent::__construct($content, $status, $headers); | ||
$this->headers->set('Cache-Control', 'must-revalidate, no-cache, no-store, private'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace tests\eLife\Web; | ||
|
||
use DateTimeImmutable; | ||
|
||
/** | ||
* @group web | ||
*/ | ||
class CacheHeadersTest extends WebTestCase | ||
{ | ||
public function testETag() | ||
{ | ||
$this->addArticlePoAWithId('1', (new DateTimeImmutable())->setDate(2017, 1, 1)); | ||
$this->addArticlePoAWithId('2', (new DateTimeImmutable())->setDate(2017, 2, 2)); | ||
|
||
$this->newClient(); | ||
$this->jsonRequest('GET', '/recommendations/research-article/1'); | ||
$response = $this->getResponse(); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$eTag = $response->headers->get('ETag'); | ||
$this->assertEquals('max-age=300, public, stale-if-error=86400, stale-while-revalidate=300', $response->headers->get('Cache-Control')); | ||
$this->assertEquals('Accept', $response->headers->get('Vary')); | ||
|
||
$this->jsonRequest('GET', '/recommendations/research-article/1', [], ['If-None-Match' => $eTag]); | ||
$response = $this->getResponse(); | ||
$this->assertEquals(304, $response->getStatusCode()); | ||
$this->assertEquals('max-age=300, public, stale-if-error=86400, stale-while-revalidate=300', $response->headers->get('Cache-Control')); | ||
$this->assertEquals('Accept', $response->headers->get('Vary')); | ||
|
||
$this->jsonRequest('GET', '/recommendations/research-article/1', [], ['If-None-Match' => 'NOT REAL ETAG']); | ||
$response = $this->getResponse(); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertEquals('max-age=300, public, stale-if-error=86400, stale-while-revalidate=300', $response->headers->get('Cache-Control')); | ||
$this->assertEquals('Accept', $response->headers->get('Vary')); | ||
} | ||
|
||
public function testPing() | ||
{ | ||
$this->newClient(); | ||
$this->jsonRequest('GET', '/ping'); | ||
$response = $this->getResponse(); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertEquals('text/plain; charset=UTF-8', $response->headers->get('Content-Type')); | ||
$this->assertEquals('must-revalidate, no-cache, no-store, private', $response->headers->get('Cache-Control')); | ||
} | ||
|
||
public function modifyConfiguration($config) | ||
{ | ||
$config['ttl'] = 300; | ||
$config['debug'] = false; | ||
|
||
return $config; | ||
} | ||
} |