<commit_before><?php

namespace Codeception\Module;

use Symfony\Component\BrowserKit\Cookie;
use Codeception\Exception\ModuleConfig as ModuleConfigException;

/**
 * Module for testing REST WebService.
 *
 * This module can be used either with frameworks or PHPBrowser.
 * It tries to guess the framework is is attached to.
 *
 * Whether framework is used it operates via standard framework modules.
 * Otherwise sends raw HTTP requests to url via PHPBrowser.
 *
 * ## Configuration
 *
 * * url *optional* - the url of api
 *
 * ## Public Properties
 *
 * * headers - array of headers going to be sent.
 * * params - array of sent data
 * * response - last response (string)
 *
 *
 */
class REST extends \Codeception\Module
{
    protected $config = array(
        'url' => '',
        'xdebug_remote' => false,
        'xdebug_codecoverage' => false,
    );

    /**
     * @var \Symfony\Component\BrowserKit\Client|\Behat\Mink\Driver\Goutte\Client
     */
    public $client = null;
    public $is_functional = false;

    public $headers = array();
    public $params = array();
    public $response = "";

    public function _before(\Codeception\TestCase $test)
    {
        if (!$this->client) {
            if (!strpos($this->config['url'], '://')) {
                // not valid url
                foreach ($this->getModules() as $module) {
                    if ($module instanceof \Codeception\Util\Framework) {
                        $this->client = $module->client;
                        $this->is_functional = true;
                        break;
                    }
                }
            } else {
                if (!$this->hasModule('PhpBrowser')) {
                    throw new ModuleConfigException(__CLASS__, "For REST testing via HTTP please enable PhpBrowser module");
                }
                $this->client = $this->getModule('PhpBrowser')->session->getDriver()->getClient();
            }
            if (!$this->client) {
                throw new ModuleConfigException(__CLASS__, "Client for REST requests not initialized.\nProvide either PhpBrowser module, or a framework module which shares FrameworkInterface");
            }
        }

        $this->headers = array();
        $this->params = array();
        $this->response = "";

        $this->client->setServerParameters(array());

        if ($this->config['xdebug_remote']
            && function_exists('xdebug_is_enabled')
            && xdebug_is_enabled()
            && ini_get('xdebug.remote_enable')
        ) {
            $cookie = new Cookie('XDEBUG_SESSION', $this->config['xdebug_remote'], null, '/');
            $this->client->getCookieJar()->set($cookie);

            $clientConfig = $this->client->getClient()->getConfig();
            $curlOptions = $clientConfig->get('curl.options');
            $curlOptions[CURLOPT_TIMEOUT] = 0;
            $clientConfig->set('curl.options', $curlOptions);
        }

        if ($this->config['xdebug_codecoverage']) {
            $this->headers['X-Codeception-CodeCoverage'] = $test->toString();
        }
    }

    public function _afterSuite($suite)
    {
        if ($this->config['xdebug_codecoverage']) {
            // Create a stream
            $options = array(
                'http' => array('header' => "X-Codeception-CodeCoverage: let me in\r\n")
            );
            $context = stream_context_create($options);
            $url = $this->config['url'] . '/c3/report/';<commit_msg>ability to set timeout for the cURL<commit_after><?php

namespace Codeception\Module;

use Symfony\Component\BrowserKit\Cookie;
use Codeception\Exception\ModuleConfig as ModuleConfigException;

/**
 * Module for testing REST WebService.
 *
 * This module can be used either with frameworks or PHPBrowser.
 * It tries to guess the framework is is attached to.
 *
 * Whether framework is used it operates via standard framework modules.
 * Otherwise sends raw HTTP requests to url via PHPBrowser.
 *
 * ## Configuration
 *
 * * url *optional* - the url of api
 * * timeout *optional* - the maximum number of seconds to allow cURL functions to execute
 *
 * ## Public Properties
 *
 * * headers - array of headers going to be sent.
 * * params - array of sent data
 * * response - last response (string)
 *
 *
 */
class REST extends \Codeception\Module
{
    protected $config = array(
        'url'                 => '',
        'timeout'             => 30,
        'xdebug_remote'       => false,
        'xdebug_codecoverage' => false,
    );

    /**
     * @var \Symfony\Component\BrowserKit\Client|\Behat\Mink\Driver\Goutte\Client
     */
    public $client = null;
    public $is_functional = false;

    public $headers = array();
    public $params = array();
    public $response = "";

    public function _before(\Codeception\TestCase $test)
    {
        if (!$this->client) {
            if (!strpos($this->config['url'], '://')) {
                // not valid url
                foreach ($this->getModules() as $module) {
                    if ($module instanceof \Codeception\Util\Framework) {
                        $this->client = $module->client;
                        $this->is_functional = true;
                        break;
                    }
                }
            } else {
                if (!$this->hasModule('PhpBrowser')) {
                    throw new ModuleConfigException(__CLASS__, "For REST testing via HTTP please enable PhpBrowser module");
                }
                $this->client = $this->getModule('PhpBrowser')->session->getDriver()->getClient();
            }
            if (!$this->client) {
                throw new ModuleConfigException(__CLASS__, "Client for REST requests not initialized.\nProvide either PhpBrowser module, or a framework module which shares FrameworkInterface");
            }
        }

        $this->headers = array();
        $this->params = array();
        $this->response = "";

        $this->client->setServerParameters(array());

        $timeout = $this->config['timeout'];

        if ($this->config['xdebug_remote']
            && function_exists('xdebug_is_enabled')
            && xdebug_is_enabled()
            && ini_get('xdebug.remote_enable')
        ) {
            $cookie = new Cookie('XDEBUG_SESSION', $this->config['xdebug_remote'], null, '/');
            $this->client->getCookieJar()->set($cookie);

            // timeout is disabled, so we can debug gently :)
            $timeout = 0;
        }

        $clientConfig = $this->client->getClient()->getConfig();
        $curlOptions = $clientConfig->get('curl.options');
        $curlOptions[CURLOPT_TIMEOUT] = $timeout;
        $clientConfig->set('curl.options', $curlOptions);

        if ($this->config['xdebug_codecoverage']) {
            $this->headers['X-Codeception-CodeCoverage'] = $test->toString();
        }
    }

    public function _afterSuite($suite)
    {
        if ($this->config['xdebug_codecoverage']) {
            // Create a stream
            $options = array(
                'http' => array('header' => "X-Codeception-CodeCoverage: let me in\r\n")
            );
            $context = stream_context_create($options);
            $url = $this->config['url'] . '/c3/report/';
{'fraction_non_alphanumeric': 0.13407898370615853, 'fraction_numerical': 0.0008285004142502071, 'mean_word_length': 2.0886993603411512, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, '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': '6516879', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1882, 'n_words': 470}