From 8fe56a91942cadb206d588b28ec1b8ba9caa2de4 Mon Sep 17 00:00:00 2001 From: Kevin Robatel Date: Tue, 30 Jun 2015 13:15:09 +0200 Subject: [PATCH] Add somes tests #1 --- tests/BaseTest.php | 50 +++++++++++++++++++ tests/HeaderCacheControlTest.php | 71 +++++++++++++++++++++++++++ tests/HeaderExpireTest.php | 37 +++++++++++--- tests/ValidationTest.php | 84 ++++++++++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 7 deletions(-) create mode 100644 tests/BaseTest.php create mode 100644 tests/HeaderCacheControlTest.php create mode 100644 tests/ValidationTest.php diff --git a/tests/BaseTest.php b/tests/BaseTest.php new file mode 100644 index 00000000..1f446927 --- /dev/null +++ b/tests/BaseTest.php @@ -0,0 +1,50 @@ +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()); + } + +} \ No newline at end of file diff --git a/tests/HeaderCacheControlTest.php b/tests/HeaderCacheControlTest.php new file mode 100644 index 00000000..b4989f7e --- /dev/null +++ b/tests/HeaderCacheControlTest.php @@ -0,0 +1,71 @@ +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")); + } + +} \ No newline at end of file diff --git a/tests/HeaderExpireTest.php b/tests/HeaderExpireTest.php index 98308b2a..21aa1ef5 100644 --- a/tests/HeaderExpireTest.php +++ b/tests/HeaderExpireTest.php @@ -24,10 +24,20 @@ public function setUp() { // Create default HandlerStack $stack = HandlerStack::create(function(RequestInterface $request, array $options) { - return new FulfilledPromise( - (new Response()) - ->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() + 2)) - ); + switch ($request->getUri()->getPath()) { + case '/expired': + return new FulfilledPromise( + (new Response()) + ->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() - 10)) + ); + case '/2s': + return new FulfilledPromise( + (new Response()) + ->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() + 2)) + ); + } + + throw new \InvalidArgumentException(); }); // Add this middleware to the top with `push` @@ -37,11 +47,24 @@ public function setUp() $this->client = new Client(['handler' => $stack]); } - public function testMockedResponse() + public function testAlreadyExpiredHeader() + { + $this->client->get("http://test.com/expired"); + $response = $this->client->get("http://test.com/expired"); + $this->assertEquals("", $response->getHeaderLine("X-Cache")); + } + + public function testExpiredHeader() { - $response = $this->client->get("anything"); + $this->client->get("http://test.com/2s"); + + $response = $this->client->get("http://test.com/2s"); + $this->assertEquals("HIT", $response->getHeaderLine("X-Cache")); + + sleep(3); - $this->assertEquals(200, $response->getStatusCode()); + $response = $this->client->get("http://test.com/2s"); + $this->assertEquals("", $response->getHeaderLine("X-Cache")); } } \ No newline at end of file diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php new file mode 100644 index 00000000..715bfdaf --- /dev/null +++ b/tests/ValidationTest.php @@ -0,0 +1,84 @@ +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")); + } + +} \ No newline at end of file