/** * Performs an HTTP request to the Selenium 2 server. * * @param string $method 'GET'|'POST'|'DELETE'|... * @param string $url * @param array $params JSON parameters for POST requests */ public function curl($http_method, PHPUnit_Extensions_Selenium2TestCase_URL $url, $params = NULL) { $curl = curl_init($url->getValue()); curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_HTTPHEADER, array('application/json;charset=UTF-8')); if ($http_method === 'POST') { curl_setopt($curl, CURLOPT_POST, TRUE); if ($params && is_array($params)) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); } curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); } else { if ($http_method == 'DELETE') { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); } } $rawResponse = trim(curl_exec($curl)); $info = curl_getinfo($curl); if ($info['http_code'] == 404) { if ($this->isNotAnAndroidDriverErrorToWorkAround($info)) { throw new BadMethodCallException("The command {$url} is not recognized by the server."); } } curl_close($curl); $content = json_decode($rawResponse, TRUE); return new PHPUnit_Extensions_Selenium2TestCase_Response($content, $info); }
/** * Performs an HTTP request to the Selenium 2 server. * * @param string $method 'GET'|'POST'|'DELETE'|... * @param string $url * @param array $params JSON parameters for POST requests */ public function curl($http_method, PHPUnit_Extensions_Selenium2TestCase_URL $url, $params = NULL) { $curl = curl_init($url->getValue()); curl_setopt($curl, CURLOPT_TIMEOUT, $this->seleniumServerRequestsTimeout); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json;charset=UTF-8', 'Accept: application/json;charset=UTF-8')); if ($http_method === 'POST') { file_put_contents('E:/realpost', "####url#####" . $url->getValue() . "###param###" . json_encode($params) . "######" . PHP_EOL, FILE_APPEND); curl_setopt($curl, CURLOPT_POST, TRUE); if ($params && is_array($params)) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); } else { curl_setopt($curl, CURLOPT_POSTFIELDS, ''); } curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); } else { if ($http_method == 'DELETE') { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); } } $rawResponse = trim(curl_exec($curl)); if (curl_errno($curl)) { throw new PHPUnit_Extensions_Selenium2TestCase_NoSeleniumException('Error connection[' . curl_errno($curl) . '] to ' . $url->getValue() . ': ' . curl_error($curl)); } $info = curl_getinfo($curl); if ($info['http_code'] == 0) { throw new PHPUnit_Extensions_Selenium2TestCase_NoSeleniumException(); } if ($info['http_code'] == 404) { throw new BadMethodCallException("The command {$url} is not recognized by the server."); } if ($info['http_code'] >= 400 && $info['http_code'] < 500) { throw new BadMethodCallException("Something unexpected happened: '{$rawResponse}'"); } curl_close($curl); $content = json_decode($rawResponse, TRUE); if ($content === null && json_last_error() !== JSON_ERROR_NONE) { throw new PHPUnit_Extensions_Selenium2TestCase_Exception(sprintf("JSON decoding of remote response failed.\n" . "Error code: %d\n" . "The response: '%s'\n", json_last_error(), $rawResponse)); } $value = null; if (is_array($content) && array_key_exists('value', $content)) { $value = $content['value']; } $message = null; if (is_array($value) && array_key_exists('message', $value)) { $message = $value['message']; } $status = isset($content['status']) ? $content['status'] : 0; if ($status !== PHPUnit_Extensions_Selenium2TestCase_WebDriverException::Success) { throw new PHPUnit_Extensions_Selenium2TestCase_WebDriverException($message, $status); } return new PHPUnit_Extensions_Selenium2TestCase_Response($content, $info); }
/** * Performs an HTTP request to the Selenium 2 server. * * @param string $method 'GET'|'POST'|'DELETE'|... * @param string $url * @param array $params JSON parameters for POST requests */ public function curl($http_method, PHPUnit_Extensions_Selenium2TestCase_URL $url, $params = NULL) { $curl = curl_init($url->getValue()); curl_setopt($curl, CURLOPT_TIMEOUT, $this->seleniumServerRequestsTimeout); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json;charset=UTF-8', 'Accept: application/json;charset=UTF-8')); if ($http_method === 'POST') { curl_setopt($curl, CURLOPT_POST, TRUE); if ($params && is_array($params)) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); } else { curl_setopt($curl, CURLOPT_POSTFIELDS, ''); } curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); } else { if ($http_method == 'DELETE') { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); } } $rawResponse = trim(curl_exec($curl)); if (curl_errno($curl)) { throw new PHPUnit_Extensions_Selenium2TestCase_NoSeleniumException('Error connection[' . curl_errno($curl) . '] to ' . $url->getValue() . ': ' . curl_error($curl)); } $info = curl_getinfo($curl); if ($info['http_code'] == 0) { throw new PHPUnit_Extensions_Selenium2TestCase_NoSeleniumException(); } if ($info['http_code'] == 404) { throw new BadMethodCallException("The command {$url} is not recognized by the server."); } curl_close($curl); $content = json_decode($rawResponse, TRUE); if ($info['http_code'] == 500) { $message = ''; if (isset($content['value']['message'])) { $message .= $content['value']['message']; } else { $message .= "Internal server error while executing {$http_method} request at {$url}. Response: " . var_export($content, TRUE); } if (isset($content['value']['class'])) { $message .= PHP_EOL . $content['value']['class']; } throw new PHPUnit_Extensions_Selenium2TestCase_WebDriverException($message, isset($content['status']) ? $content['status'] : 13); } return new PHPUnit_Extensions_Selenium2TestCase_Response($content, $info); }