コード例 #1
0
 /**
  * Get user input, about what request is of interest, if set in testplan.json then ignore.
  *
  * @param string $capturelabel capture label.
  * @return array request which is saved.
  */
 private function confirm_requests_to_capture($capturelabel)
 {
     // Save har data in the temp file.
     $useridentifiedrequestsdatafile = util::get_har_file_path($capturelabel);
     $hardata = browsermobproxyclient::get_har($capturelabel);
     file_put_contents($useridentifiedrequestsdatafile, $hardata);
     $requests = util::get_info_from_har($capturelabel);
     // Get which request is being used.
     $request = $this->is_request_in_config($capturelabel, $requests);
     // Get query params to be used.
     if (!empty($request)) {
         // Update config and return.
         util::save_final_har_data($capturelabel, $request);
         return $request;
     }
     // This is only possible with interactive terminal, so ensure we have one before proceeding.
     $posixexists = function_exists('posix_isatty');
     // Make sure this step is only used with interactive terminal (if detected).
     if ($posixexists && !@posix_isatty(STDOUT)) {
         $msg = "You have some unfilled requests in testplan.json\n" . "Please run following command for interactive terminal to enter value\n" . "  vendor/bin/behat --config " . util::get_tool_dir() . DIRECTORY_SEPARATOR . 'behat.yml ';
         util::performance_exception($msg);
     }
     // Else ask user for the actual request to use.
     fwrite(STDOUT, "\n");
     if (count($requests) > 1) {
         foreach ($requests as $index => $request) {
             fwrite(STDOUT, $index . ". - " . $request['method'] . " - " . $request['path'] . PHP_EOL);
         }
         fwrite(STDOUT, "Which http request should associate with \"{$capturelabel}\": ");
         $requestindex = trim(fread(STDIN, 1024));
         // Ensure correct selection is done.
         while (!is_number($requestindex) || $requestindex <= 0 || $requestindex > count($requests)) {
             fwrite(STDOUT, "Wrong request number for step {$capturelabel}, try again: ");
             $requestindex = trim(fread(STDIN, 1024));
         }
     } else {
         $requestindex = 0;
     }
     fwrite(STDOUT, "\\Enter query values to substitute for \"" . $requests[$requestindex]['path'] . "\"\n ");
     $requests[$requestindex]['query'] = $this->get_substitute_value_from_user($requests[$requestindex]['query']);
     // Save final request.
     util::save_final_har_data($capturelabel, $requests[$requestindex]);
     return $requests[$requestindex];
 }
コード例 #2
0
 /**
  * Extract information form har data and return.
  * @param string $capturelabel Capture label.
  * @param string $requesttype (optional) data we are interested in. This should be either get|post
  * @return array.
  */
 public static function get_info_from_har($capturelabel, $requesttype = '')
 {
     global $CFG;
     $urlcomponents = parse_url($CFG->wwwroot);
     if (empty($urlcomponents['path'])) {
         $sitepath = '';
     } else {
         $sitepath = $urlcomponents['path'];
     }
     $datatoextract = array('get', 'post');
     if (!empty($requesttype) && !in_array($requesttype, $datatoextract)) {
         self::performance_exception("Wrong request type passed while getting har data");
     }
     // Get har data.
     $hardata = file_get_contents(util::get_har_file_path($capturelabel));
     $hardata = json_decode($hardata, true);
     // Everything is in log.
     $hardata = $hardata['log'];
     $entries = $hardata['entries'];
     $requests = array();
     foreach ($entries as $entrykey => $entry) {
         // Try only get text/html request. Sometimes we get size =0, so just process the request.
         if (!empty($entry['response']['content']['mimeType']) && $entry['response']['content']['mimeType'] == 'text/html; charset=utf-8' || $entry['response']['content']['size'] == 0) {
             if ($entry['request']['method'] == "POST") {
                 $postdata = array();
                 foreach ($entry['request']['postData']['params'] as $param) {
                     $postdata[$param['name']] = $param['value'];
                 }
                 $query = parse_url($entry['request']['url'], PHP_URL_QUERY);
                 if (empty($query)) {
                     $queryarray = array();
                 } else {
                     parse_str($query, $queryarray);
                 }
                 $postdata = array_merge($postdata, $queryarray);
                 $path = parse_url($entry['request']['url'], PHP_URL_PATH);
                 $path = str_replace($sitepath, '', $path);
                 $requests[] = array('path' => $path, 'method' => 'POST', 'query' => $postdata);
             } else {
                 $query = parse_url($entries[$entrykey]['request']['url'], PHP_URL_QUERY);
                 if (empty($query)) {
                     $queryarray = array();
                 } else {
                     parse_str($query, $queryarray);
                 }
                 $path = parse_url($entries[$entrykey]['request']['url'], PHP_URL_PATH);
                 $path = str_replace($sitepath, '', $path);
                 $requests[] = array('path' => $path, 'method' => 'GET', 'query' => $queryarray);
             }
         } else {
             if (empty($entry['response'])) {
                 // If no response found then it might be a get request.
                 $query = parse_url($entries[$entrykey]['request']['url'], PHP_URL_QUERY);
                 if (empty($query)) {
                     $queryarray = array();
                 } else {
                     parse_str($query, $queryarray);
                 }
                 $path = parse_url($entries[$entrykey]['request']['url'], PHP_URL_PATH);
                 $path = str_replace($sitepath, '', $path);
                 $requests[] = array('path' => $path, 'method' => 'GET', 'query' => $queryarray);
             }
         }
     }
     if (!empty($requesttype)) {
         foreach ($requests as $index => $request) {
             if ($request['method'] != $requesttype) {
                 unset($requests[$index]);
             }
         }
     }
     return $requests;
 }