<commit_before><?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: 29.06.2015
 * Time: 22:48
 */
class HeaderExpireTest 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 '/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`
        $stack->push(CacheMiddleware::getMiddleware(), 'cache');

        // Initialize the client with the handler option
        $this->client = new Client(['handler' => $stack]);
    }

    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()
    {
        $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"));
    }

}<commit_msg>Add test for stale-if-error #4<commit_after><?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: 29.06.2015
 * Time: 22:48
 */
class HeaderExpireTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Client
     */
    protected $client;

    /**
     * @var bool
     */
    protected $sendError = false;

    public function setUp()
    {
        // Create default HandlerStack
        $stack = HandlerStack::create(function(RequestInterface $request, array $options) {
            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))
                    );
                case '/stale-if-error':
                    if ($this->sendError) {
                        return new FulfilledPromise(
                            new Response(500)
                        );
                    }
                    return new FulfilledPromise(
                        (new Response())
                            ->withHeader("Cache-Control", "stale-if-error=120")
                    );
            }

            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 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()
    {
        $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 testStaleIfErrorHeader()
    {
        $this->client->get("http://test.com/stale-if-error");

        $this->sendError = true;
        $response = $this->client->get("http://test.com/stale-if-error");
        $this->assertEquals("HIT stale", $response->getHeaderLine("X-Cache"));
        $this->sendError = false;
    }

}
{'fraction_non_alphanumeric': 0.15054617676266138, 'fraction_numerical': 0.009731876861966237, 'mean_word_length': 1.8975834292289988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3596897', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1380, 'n_words': 280}