-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Robatel
committed
Jun 30, 2015
1 parent
70c4e62
commit 8fe56a9
Showing
4 changed files
with
235 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Kevin | ||
* Date: 30.06.2015 | ||
* Time: 11:58 | ||
*/ | ||
|
||
namespace Kevinrob\GuzzleCache; | ||
|
||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Promise\FulfilledPromise; | ||
use GuzzleHttp\Psr7\Response; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class BaseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
protected $client; | ||
|
||
public function setUp() | ||
{ | ||
// Create default HandlerStack | ||
$stack = HandlerStack::create(function(RequestInterface $request, array $options) { | ||
return new FulfilledPromise( | ||
(new Response()) | ||
->withBody(\GuzzleHttp\Psr7\stream_for('Hello world!')) | ||
); | ||
}); | ||
|
||
// Add this middleware to the top with `push` | ||
$stack->push(CacheMiddleware::getMiddleware(), 'cache'); | ||
|
||
// Initialize the client with the handler option | ||
$this->client = new Client(['handler' => $stack]); | ||
} | ||
|
||
public function testNoBreakClient() | ||
{ | ||
$response = $this->client->get("anything"); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertEquals('Hello world!', $response->getBody()); | ||
} | ||
|
||
} |
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,71 @@ | ||
<?php | ||
namespace Kevinrob\GuzzleCache; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Promise\FulfilledPromise; | ||
use GuzzleHttp\Psr7\Response; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: Kevin | ||
* Date: 30.06.2015 | ||
* Time: 12:58 | ||
*/ | ||
class HeaderCacheControlTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
protected $client; | ||
|
||
public function setUp() | ||
{ | ||
// Create default HandlerStack | ||
$stack = HandlerStack::create(function(RequestInterface $request, array $options) { | ||
switch ($request->getUri()->getPath()) { | ||
case '/2s': | ||
return new FulfilledPromise( | ||
(new Response()) | ||
->withAddedHeader("Cache-Control", "max-age=2") | ||
); | ||
case '/no-store': | ||
return new FulfilledPromise( | ||
(new Response()) | ||
->withAddedHeader("Cache-Control", "no-store") | ||
); | ||
} | ||
|
||
throw new \InvalidArgumentException(); | ||
}); | ||
|
||
// Add this middleware to the top with `push` | ||
$stack->push(CacheMiddleware::getMiddleware(), 'cache'); | ||
|
||
// Initialize the client with the handler option | ||
$this->client = new Client(['handler' => $stack]); | ||
} | ||
|
||
public function testMaxAgeHeader() | ||
{ | ||
$this->client->get("http://test.com/2s"); | ||
|
||
$response = $this->client->get("http://test.com/2s"); | ||
$this->assertEquals("HIT", $response->getHeaderLine("X-Cache")); | ||
|
||
sleep(3); | ||
|
||
$response = $this->client->get("http://test.com/2s"); | ||
$this->assertEquals("", $response->getHeaderLine("X-Cache")); | ||
} | ||
|
||
public function testNoStoreHeader() | ||
{ | ||
$this->client->get("http://test.com/no-store"); | ||
|
||
$response = $this->client->get("http://test.com/no-store"); | ||
$this->assertEquals("", $response->getHeaderLine("X-Cache")); | ||
} | ||
|
||
} |
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,84 @@ | ||
<?php | ||
namespace Kevinrob\GuzzleCache; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Promise\FulfilledPromise; | ||
use GuzzleHttp\Psr7\Response; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: Kevin | ||
* Date: 30.06.2015 | ||
* Time: 12:58 | ||
*/ | ||
class ValidationTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
protected $client; | ||
|
||
public function setUp() | ||
{ | ||
// Create default HandlerStack | ||
$stack = HandlerStack::create(function(RequestInterface $request, array $options) { | ||
switch ($request->getUri()->getPath()) { | ||
case '/etag': | ||
if ($request->getHeaderLine("If-None-Match") == 'MyBeautifulHash') { | ||
return new FulfilledPromise(new Response(304)); | ||
} | ||
|
||
return new FulfilledPromise( | ||
(new Response()) | ||
->withHeader("Etag", 'MyBeautifulHash') | ||
->withAddedHeader("Cache-Control", 'max-age=0') | ||
); | ||
case '/etag-changed': | ||
if ($request->getHeaderLine("If-None-Match") == 'MyBeautifulHash') { | ||
return new FulfilledPromise( | ||
(new Response()) | ||
->withHeader("Etag", 'MyBeautifulHash2') | ||
->withAddedHeader("Cache-Control", 'max-age=0') | ||
); | ||
} | ||
|
||
return new FulfilledPromise( | ||
(new Response()) | ||
->withHeader("Etag", 'MyBeautifulHash') | ||
->withAddedHeader("Cache-Control", 'max-age=0') | ||
); | ||
} | ||
|
||
throw new \InvalidArgumentException(); | ||
}); | ||
|
||
// Add this middleware to the top with `push` | ||
$stack->push(CacheMiddleware::getMiddleware(), 'cache'); | ||
|
||
// Initialize the client with the handler option | ||
$this->client = new Client(['handler' => $stack]); | ||
} | ||
|
||
public function testEtagHeader() | ||
{ | ||
$this->client->get("http://test.com/etag"); | ||
|
||
sleep(1); | ||
|
||
$response = $this->client->get("http://test.com/etag"); | ||
$this->assertEquals("HIT with validation", $response->getHeaderLine("X-Cache")); | ||
} | ||
|
||
public function testEtagChangeHeader() | ||
{ | ||
$this->client->get("http://test.com/etag-changed"); | ||
|
||
sleep(1); | ||
|
||
$response = $this->client->get("http://test.com/etag-changed"); | ||
$this->assertEquals("", $response->getHeaderLine("X-Cache")); | ||
} | ||
|
||
} |