<commit_before><?php

class RequestHandlerTest extends \PHPUnit_Framework_TestCase
{
    public function testBaseUrlHasTrailingSlash()
    {
        $client = new Tumblr\API\Client(API_KEY);
        $rh = $client->getRequestHandler();
        $this->assertInstanceOf('Tumblr\API\RequestHandler', $rh);

        $rh->setBaseUrl('http://example.com');
        $this->assertAttributeEquals('http://example.com/', 'baseUrl', $rh);

        $rh->setBaseUrl('http://example.com/');
        $this->assertAttributeEquals('http://example.com/', 'baseUrl', $rh);
    }

     /**
     * @expectedException GuzzleHttp\Exception\ConnectException
     */
    public function testRequestThrowsErrorOnMalformedBaseUrl()
    {
        $client = new Tumblr\API\Client(API_KEY);
        $rh = $client->getRequestHandler();
        $rh->setBaseUrl('this is a malformed URL!');

        $options = array('some kinda option');

        $rh->request('GET', 'foo', $options);

    }

    /**
     * @expectedException Tumblr\API\RequestException
     */
    public function testRequestThrowsOnBadResponse()
    {
        // Response stream
        $stream = fopen('php://memory','r+');
        fwrite($stream, '{"meta": {"status": 400, "msg": "Sadface."} }');
        rewind($stream);

        // Hook up response to all requests
        $response = (new GuzzleHttp\Psr7\Response)
            ->withStatus(400, 'Bad Request')
            ->withBody(new GuzzleHttp\Psr7\Stream($stream));

        $this->guzzle = $this->getMock('\GuzzleHttp\Client', array('request'));
        $this->guzzle->expects($this->any())
                     ->method('request')
                     ->will($this->returnValue($response));

        // Attached mocked guzzle client
        $client = new Tumblr\API\Client(API_KEY);
        $rh = $client->getRequestHandler();
        $rh->client = $this->guzzle;

        // Throws because it got a 400 back
        $client->getBlogInfo('ceyko.tumblr.com');
    }

    public function testRequestGetsJsonResponseField()
    {
        // Response stream
        $stream = fopen('php://memory','r+');
        fwrite($stream, '{"meta": {"status": 200, "msg": "OK"}, "response": "Response Text"}');
        rewind($stream);

        // Hook up response to all requests
        $response = (new GuzzleHttp\Psr7\Response)
            ->withStatus(200, 'OK')
            ->withBody(new GuzzleHttp\Psr7\Stream($stream));

        $this->guzzle = $this->getMock('\GuzzleHttp\Client', array('request'));
        $this->guzzle->expects($this->any())
                     ->method('request')
                     ->will($this->returnValue($response));

        // Attached mocked guzzle client
        $client = new Tumblr\API\Client(API_KEY);
        $rh = $client->getRequestHandler();
        $rh->client = $this->guzzle;

        // Parses out the `reponse` field in json on success
        $this->assertEquals($client->getBlogInfo('ceyko.tumblr.com'), 'Response Text');
    }

}<commit_msg>RequestHandler tests use guzzle HandlerStack<commit_after><?php

class RequestHandlerTest extends \PHPUnit_Framework_TestCase
{
    public function testBaseUrlHasTrailingSlash()
    {
        $client = new Tumblr\API\Client(API_KEY);
        $rh = $client->getRequestHandler();
        $this->assertInstanceOf('Tumblr\API\RequestHandler', $rh);

        $rh->setBaseUrl('http://example.com');
        $this->assertAttributeEquals('http://example.com/', 'baseUrl', $rh);

        $rh->setBaseUrl('http://example.com/');
        $this->assertAttributeEquals('http://example.com/', 'baseUrl', $rh);
    }

     /**
     * @expectedException GuzzleHttp\Exception\ConnectException
     */
    public function testRequestThrowsErrorOnMalformedBaseUrl()
    {
        $client = new Tumblr\API\Client(API_KEY);
        $rh = $client->getRequestHandler();
        $rh->setBaseUrl('this is a malformed URL!');

        $options = array('some kinda option');

        $rh->request('GET', 'foo', $options);

    }

    /**
     * @expectedException Tumblr\API\RequestException
     * @expectedExceptionCode 400
     * @expectedExceptionMessage Sadface
     */
    public function testRequestThrowsOnBadResponse()
    {
        // Setup mock handler and response
        $mock = new GuzzleHttp\Handler\MockHandler([
            new GuzzleHttp\Psr7\Response(400, [], '{"meta": {"status": 400, "msg": "Sadface"} }'),
        ]);
        $stack = GuzzleHttp\HandlerStack::create($mock);
        $guzzle = new GuzzleHttp\Client(['handler' => $stack]);

        // Attached mocked guzzle client
        $client = new Tumblr\API\Client(API_KEY);
        $client->getRequestHandler()->client = $guzzle;

        // Throws because it got a 400 back
        $client->getBlogInfo('ceyko.tumblr.com');
    }

    public function testRequestGetsJsonResponseField()
    {
        // Setup mock handler and response
        $mock = new GuzzleHttp\Handler\MockHandler([
            new GuzzleHttp\Psr7\Response(200, [], '{"meta": {"status": 200, "msg": "OK"}, "response": "Response Text"}'),
        ]);
        $stack = GuzzleHttp\HandlerStack::create($mock);
        $guzzle = new GuzzleHttp\Client(['handler' => $stack]);

        // Attached mocked guzzle client
        $client = new Tumblr\API\Client(API_KEY);
        $client->getRequestHandler()->client = $guzzle;

        // Parses out the `reponse` field in json on success
        $this->assertEquals($client->getBlogInfo('ceyko.tumblr.com'), 'Response Text');
    }
}
{'fraction_non_alphanumeric': 0.15916134913400182, 'fraction_numerical': 0.007110300820419325, 'mean_word_length': 2.6476063829787235, 'pattern_counts': {'":': 14, '<': 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': '6019656', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1574, 'n_words': 303}