<commit_before><?php
/**
 * JBZoo CrossCMS
 *
 * This file is part of the JBZoo CCK package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package   CrossCMS
 * @license   MIT
 * @copyright Copyright (C) JBZoo.com,  All rights reserved.
 * @link      https://github.com/JBZoo/CrossCMS
 * @author    <NAME> <<EMAIL>>
 */

namespace JBZoo\PHPUnit;

use JBZoo\CrossCMS\AbstractHttp;
use JBZoo\Data\JSON;

/**
 * Class HttpTest
 * @package JBZoo\PHPUnit
 */
class HttpTest extends CrossCMS
{
    protected $_url = 'http://mockbin.org/bin/5fb64e6d-d5ff-4ff6-9333-025b94d686a7';

    protected function setUp()
    {
        parent::setUp();
        sleep(1); // timeout for mockbin.org
    }

    /* Simple *********************************************************************************************************/

    public function testSimple_request()
    {
        $resp = $this->_cms['http']->request($this->_url, array('qwerty' => '123456'));
        isSame('{"key":"value"}', $resp);
    }

    public function testSimple_https()
    {
        $resp = $this->_cms['http']->request('https://mockbin.org/gzip', array(), array(
            'debug'      => 1,
            'ssl_verify' => 0, // For travis ... =(
        ));

        if ('"Hello World!"' === $resp) {
            isSame('"Hello World!"', $resp);

            $resp = $this->_cms['http']->request('https://mockbin.org/request', array(), array(
                'debug'      => 1,
                'ssl_verify' => 0, // For travis ... =(
            ));
            $data = new JSON($resp);
            isSame('https://mockbin.org/request', $data->get('url'));

        } else {
            cliMessage('CrossCMS SSL Error: ' . $resp);
        }
    }

    public function testSimple_404()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/code/404', array(), array(
            'response' => AbstractHttp::RESULT_CODE,
        ));
        isSame(404, $resp);
    }

    public function testSimple_gzip()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/gzip', array());
        isSame('"Hello World!"', $resp);
    }

    /* Methods ********************************************************************************************************/

    public function testMethod_GET()
    {
        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq));

        $data = new JSON($resp);

        isSame('GET', $data->get('method'));
        isSame($uniq, $data->find('queryString.qwerty'));
        isSame('http://mockbin.org/request?qwerty=' . $uniq, $data->get('url'));

        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq), array(
            'method' => AbstractHttp::METHOD_GET,
        ));

        $data = new JSON($resp);

        isSame('GET', $data->get('method'));
        isSame($uniq, $data->find('queryString.qwerty'));
        isSame('http://mockbin.org/request?qwerty=' . $uniq, $data->get('url'));
    }

    public function testMethod_POST()
    {
        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq), array(
            'method' => AbstractHttp::METHOD_POST,
        ));

        $data = new JSON($resp);

        isSame('POST', $data->get('method'));
        isSame($uniq, $data->find('postData.params.qwerty'));
        isSame('http://mockbin.org/request', $data->get('url'));
    }

    public function testMethod_HEAD()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method'   => AbstractHttp::METHOD_HEAD,
            'response' => AbstractHttp::RESULT_FULL,
        ));

        isSame('HEAD', $resp->find('headers.access-control-allow-methods'));
    }

    public function testMethod_PUT()
    {
        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq), array(
            'method' => AbstractHttp::METHOD_PUT,
        ));

        $data = new JSON($resp);

        isSame('PUT', $data->get('method'));
        isSame($uniq, $data->find('postData.params.qwerty'));
    }

    public function testMethod_DELETE()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method' => AbstractHttp::METHOD_DELETE,
        ));

        $data = new JSON($resp);

        isSame('DELETE', $data->get('method'));
    }

    public function testMethod_OPTIONS()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method' => AbstractHttp::METHOD_OPTIONS,
        ));

        $data = new JSON($resp);

        isSame('OPTIONS', $data->get('method'));
    }

    public function testMethod_PATCH()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method' => AbstractHttp::METHOD_PATCH,
        ));

        $data = new JSON($resp);

        isSame('PATCH', $data->get('method'));
    }

    /* Result *********************************************************************************************************/

    public function testResult_code()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_CODE,
        ));

        isSame(200, $resp);
    }

    public function testResult_headers()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_HEAD,
        ));

        isSame('foo', $resp->get('x-custom-header'));
        isSame('bar', $resp->get('x-custom-header-2'));
    }

    public function testResult_body()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_BODY,
        ));

        isSame('{"key":"value"}', $resp);
    }

    public function testResult_full()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_FULL,
        ));

        $data = new JSON($resp);

        isSame('{"key":"value"}', $data->get('body'));
        isSame('foo', $data->find('headers.x-custom-header'));
        isSame(200, $data->find('code'));
    }

    /* Options ********************************************************************************************************/

    public function testOption_cache()
    {
        $uniq = uniqid();
        $args = array('qwerty' => $uniq);

        $respBefore = $this->_cms['http']->request('http://mockbin.org/request', $args, array('cache' => true));
        sleep(1);
        $respAfter = $this->_cms['http']->request('http://mockbin.org/request', $args, array('cache' => true));

        $dataBefore = new JSON($respBefore);
        $dataAfter  = new JSON($respAfter);

        isSame($dataBefore->get('starteddatetime'), $dataAfter->get('starteddatetime'));
    }

    public function testOption_timeout()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/delay/5001', array(), array(
            'timeout' => 2,
        ));
        isNull($resp);

        $resp = $this->_cms['http']->request('http://mockbin.org/delay/5002', array(), array(
            'timeout' => 10,
        ));

        $data = new JSON($resp);
        isSame(5002, $data->get('delay'));
    }

    public function testOption_userAgent()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/agent');
        isSame('"CrossCMS HTTP Client v1.x-dev"', $resp);

        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/agent', array(), array(
            'user_agent' => ' Custom name' . $uniq . ' ',
        ));

        isSame('"Custom name' . $uniq . '"', $resp);
    }

    /* Redirects ******************************************************************************************************/
<commit_msg>skip timeout tests<commit_after><?php
/**
 * JBZoo CrossCMS
 *
 * This file is part of the JBZoo CCK package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package   CrossCMS
 * @license   MIT
 * @copyright Copyright (C) JBZoo.com,  All rights reserved.
 * @link      https://github.com/JBZoo/CrossCMS
 * @author    <NAME> <<EMAIL>>
 */

namespace JBZoo\PHPUnit;

use JBZoo\CrossCMS\AbstractHttp;
use JBZoo\Data\JSON;

/**
 * Class HttpTest
 * @package JBZoo\PHPUnit
 */
class HttpTest extends CrossCMS
{
    protected $_url = 'http://mockbin.org/bin/5fb64e6d-d5ff-4ff6-9333-025b94d686a7';

    protected function setUp()
    {
        parent::setUp();
        sleep(1); // timeout for mockbin.org
    }

    /* Simple *********************************************************************************************************/

    public function testSimple_request()
    {
        $resp = $this->_cms['http']->request($this->_url, array('qwerty' => '123456'));
        isSame('{"key":"value"}', $resp);
    }

    public function testSimple_https()
    {
        $resp = $this->_cms['http']->request('https://mockbin.org/gzip', array(), array(
            'debug'      => 1,
            'ssl_verify' => 0, // For travis ... =(
        ));

        if ('"Hello World!"' === $resp) {
            isSame('"Hello World!"', $resp);

            $resp = $this->_cms['http']->request('https://mockbin.org/request', array(), array(
                'debug'      => 1,
                'ssl_verify' => 0, // For travis ... =(
            ));
            $data = new JSON($resp);
            isSame('https://mockbin.org/request', $data->get('url'));

        } else {
            cliMessage('CrossCMS SSL Error: ' . $resp);
        }
    }

    public function testSimple_404()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/code/404', array(), array(
            'response' => AbstractHttp::RESULT_CODE,
        ));
        isSame(404, $resp);
    }

    public function testSimple_gzip()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/gzip', array());
        isSame('"Hello World!"', $resp);
    }

    /* Methods ********************************************************************************************************/

    public function testMethod_GET()
    {
        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq));

        $data = new JSON($resp);

        isSame('GET', $data->get('method'));
        isSame($uniq, $data->find('queryString.qwerty'));
        isSame('http://mockbin.org/request?qwerty=' . $uniq, $data->get('url'));

        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq), array(
            'method' => AbstractHttp::METHOD_GET,
        ));

        $data = new JSON($resp);

        isSame('GET', $data->get('method'));
        isSame($uniq, $data->find('queryString.qwerty'));
        isSame('http://mockbin.org/request?qwerty=' . $uniq, $data->get('url'));
    }

    public function testMethod_POST()
    {
        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq), array(
            'method' => AbstractHttp::METHOD_POST,
        ));

        $data = new JSON($resp);

        isSame('POST', $data->get('method'));
        isSame($uniq, $data->find('postData.params.qwerty'));
        isSame('http://mockbin.org/request', $data->get('url'));
    }

    public function testMethod_HEAD()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method'   => AbstractHttp::METHOD_HEAD,
            'response' => AbstractHttp::RESULT_FULL,
        ));

        isSame('HEAD', $resp->find('headers.access-control-allow-methods'));
    }

    public function testMethod_PUT()
    {
        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array('qwerty' => $uniq), array(
            'method' => AbstractHttp::METHOD_PUT,
        ));

        $data = new JSON($resp);

        isSame('PUT', $data->get('method'));
        isSame($uniq, $data->find('postData.params.qwerty'));
    }

    public function testMethod_DELETE()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method' => AbstractHttp::METHOD_DELETE,
        ));

        $data = new JSON($resp);

        isSame('DELETE', $data->get('method'));
    }

    public function testMethod_OPTIONS()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method' => AbstractHttp::METHOD_OPTIONS,
        ));

        $data = new JSON($resp);

        isSame('OPTIONS', $data->get('method'));
    }

    public function testMethod_PATCH()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/request', array(), array(
            'method' => AbstractHttp::METHOD_PATCH,
        ));

        $data = new JSON($resp);

        isSame('PATCH', $data->get('method'));
    }

    /* Result *********************************************************************************************************/

    public function testResult_code()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_CODE,
        ));

        isSame(200, $resp);
    }

    public function testResult_headers()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_HEAD,
        ));

        isSame('foo', $resp->get('x-custom-header'));
        isSame('bar', $resp->get('x-custom-header-2'));
    }

    public function testResult_body()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_BODY,
        ));

        isSame('{"key":"value"}', $resp);
    }

    public function testResult_full()
    {
        $resp = $this->_cms['http']->request($this->_url, array(), array(
            'response' => AbstractHttp::RESULT_FULL,
        ));

        $data = new JSON($resp);

        isSame('{"key":"value"}', $data->get('body'));
        isSame('foo', $data->find('headers.x-custom-header'));
        isSame(200, $data->find('code'));
    }

    /* Options ********************************************************************************************************/

    public function testOption_cache()
    {
        $uniq = uniqid();
        $args = array('qwerty' => $uniq);

        $respBefore = $this->_cms['http']->request('http://mockbin.org/request', $args, array('cache' => true));
        sleep(1);
        $respAfter = $this->_cms['http']->request('http://mockbin.org/request', $args, array('cache' => true));

        $dataBefore = new JSON($respBefore);
        $dataAfter  = new JSON($respAfter);

        isSame($dataBefore->get('starteddatetime'), $dataAfter->get('starteddatetime'));
    }

    public function testOption_timeout()
    {
        skip();

        $resp = $this->_cms['http']->request('http://mockbin.org/delay/5001', array(), array(
            'timeout' => 2,
        ));
        isNull($resp);

        $resp = $this->_cms['http']->request('http://mockbin.org/delay/5002', array(), array(
            'timeout' => 10,
        ));

        $data = new JSON($resp);
        isSame(5002, $data->get('delay'));
    }

    public function testOption_userAgent()
    {
        $resp = $this->_cms['http']->request('http://mockbin.org/agent');
        isSame('"CrossCMS HTTP Client v1.x-dev"', $resp);

        $uniq = uniqid();
        $resp = $this->_cms['http']->request('http://mockbin.org/agent', array(), array(
            'user_agent' => ' Custom name' . $uniq . ' ',
        ));

        isSame('"Custom name' . $uniq . '"', $resp);
    }

    /* Redirects ******************************************************************************************************/

{'fraction_non_alphanumeric': 0.2515378937007874, 'fraction_numerical': 0.007874015748031496, 'mean_word_length': 2.6296048225050233, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 215, 'https://': 8, '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': '324240', 'n_tokens_mistral': 5062, 'n_tokens_neox': 4788, 'n_words': 811}